Create and merge git branches


new branch

First, we assume you are working on your project and already have some commits.

A simple commit history.
$ git checkout -b iss53
Switched to a new branch "iss53"

It is shorthand for the following two commands:

$ git branch iss53
$ git checkout iss53
Create a new branch pointer.
$ vim index.html
$ git commit -a -m 'added a new footer [issue 53]'
The iss53 branch moves forward as the work progresses.
$ git checkout master
Switched to branch 'master'

$ git checkout -b hotfix
Switched to a new branch 'hotfix'
$ vim index.html
$ git commit -a -m 'fixed the broken email address'
[hotfix 1fb7853] fixed the broken email address
 1 file changed, 2 insertions(+)
A hotfix branch based on the `master` branch.
$ git checkout master
$ git merge hotfix
Updating f42c576..3a0874c
Fast-forward
 index.html | 2 ++
 1 file changed, 2 insertions(+)

`master` is fast-forwarded to `hotfix`.
$ git branch -d hotfix
Deleted branch hotfix (3a0874c).

Now you can continue your work by switching back to the branch you were working on, which is the branch for issue #53 (the iss53 branch).

$ git checkout iss53
Switched to branch "iss53"
$ vim index.html
$ git commit -a -m 'finished the new footer [issue 53]'
[iss53 ad82d7a] finished the new footer [issue 53]
1 file changed, 1 insertion(+)
Continue work on the `iss53` branch.

$ git checkout master
Switched to branch 'master'
$ git merge iss53
Merge made by the 'recursive' strategy.
index.html |    1 +
1 file changed, 1 insertion(+)

 

The three snapshots used in a typical merge.
A merge commit.
$ git branch -d iss53

$ git merge iss53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

At this point Git did the merge, but did not automatically create a new merge commit. Git will pause and wait for you to resolve the merge conflicts. You can use the git statuscommand to view files that are in an unmerged state because they contain a merge conflict:

$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

    both modified:      index.html

no changes added to commit (use "git add" and/or "git commit -a")

 

<<<<<<< HEAD:index.html
<div id="footer">contact : [email protected]</div>
=======
<div id="footer">
 please contact us at [email protected]
</div>
>>>>>>> iss53:index.html

This HEADmeans that the version indicated by (that is, masterwhere your branch is, since you checked out to this branch when you ran the merge command) is in the upper half of this section ( =======the upper half), and the iss53branch indicated by version is in =======the bottom half of . To resolve conflicts, you must choose to use one of the two parts =======split by , or you can merge the content yourself. For example, you can resolve the conflict by replacing this content with the following:

<div id="footer">
please contact us at [email protected]
</div>

The conflict resolution above preserves only the modifications of one of the branches, and <<<<<<<the , ,======= and lines are completely removed. >>>>>>>After you have resolved conflicts in all files, use the git addcommand to mark it as conflict resolved. Once these otherwise conflicting files are staged, Git marks them as conflict resolved.

If you want to use a graphical tool to resolve conflicts, you can run git mergetoolthis command, which will launch a suitable visual merge tool for you and walk you through the conflict resolution step by step:

$ git mergetool

This message is displayed because 'merge.tool' is not configured.
See 'git mergetool --tool-help' or 'git help config' for more details.
'git mergetool' will now attempt to use one of the following tools:
opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse diffmerge ecmerge p4merge araxis bc3 codecompare vimdiff emerge
Merging:
index.html

Normal merge conflict for 'index.html':
  {local}: modified file
  {remote}: modified file
Hit return to start merge resolution tool (opendiff):

If you want to use a merge tool other than the default one (here Git uses opendiffas the default merge tool, since the author runs the program on a Mac), you can do this in "one of the following tools" See all supported merge tools after the sentence. Then enter the name of the tool you like.

Note

If you need more advanced tools to resolve complex merge conflicts, we 'll cover more about branch merging in Advanced Merging.

After you exit the merge tool, Git will ask if the merge was successful. If you answer yes, Git will stage those files to show that the conflicts are resolved: you can run again git statusto confirm that all merge conflicts are resolved:

$ git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

    modified:   index.html

If you are satisfied with the result, and are sure that the conflicting files have been staged, you can enter git committo complete the merge commit. By default the commit message looks like this:

Merge branch 'iss53'

Conflicts:
    index.html
#
# It looks like you may be committing a merge.
# If this is not correct, please remove the file
#	.git/MERGE_HEAD
# and try again.


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# All conflicts fixed but you are still merging.
#
# Changes to be committed:
#	modified:   index.html
#

If you feel that the above information is not sufficient to fully reflect the branch merge process, you can modify the above information to add some details to help future readers reviewing this merge, tell them how you resolved the merge conflict, and why. .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326020687&siteId=291194637