Git Branch Creation and Merging - Branch Operations
# Git Branch Creation and Merging - Branch Operations
Documentation: Git Branching - Basic Branching and Merging (opens new window)
# Creating and Switching to a Branch
Suppose you have a new requirement and need to work on a new branch iss53:
$ git checkout -b iss53 # b stands for branch
This is shorthand for the following two commands:
$ git branch iss53
$ git checkout iss53
2
# Switching Branches
An urgent issue suddenly comes up, and you need to fix it on the original master branch:
$ git checkout master
Before switching to master, make sure the iss53 branch is in a clean state (all changes have been committed).
Note: Switching branches causes Git to reset your working directory.
The word
checkoutliterally means "to check out."checkout <branch>checks out a branch, i.e., checks out the code of the specified branch, which resets the working directory and switches the branch.
Next, you need to fix the urgent issue. Create a hotfix branch and work on it until the issue is resolved:
$ git checkout -b hotfix
# In between, modify the code on hotfix and commit
$ echo 'test' > ./hotfix.txt
$ git add .
$ git commit -m 'fixed'
2
3
4
5
6
# Merging Branches
$ git checkout master # First, switch back to the master branch
$ git merge hotfix # Merge the hotfix branch in
2
# Deleting a Branch
$ git branch -d hotfix # d stands for delete
# Then switch back to iss53 to continue working
$ git checkout iss53
2
3
4
Note that deleting a branch uses the branch command.
# Merging Branches After Multiple Commits
Suppose you have finished fixing issue #53 and want to merge it into the master branch:
$ git checkout master
$ git merga iss53
2
This looks similar to the previous merge. But now your development history has diverged from an earlier point. Because the commit on master is not a direct ancestor of the commit on iss53, Git has to do some extra work. In this case, Git uses the snapshots pointed to by the tips of both branches and their common ancestor to perform a simple three-way merge.
Unlike simply moving the branch pointer forward, Git creates a new snapshot from the result of this three-way merge and automatically creates a new commit pointing to it. This is called a merge commit, and its special feature is that it has more than one parent commit.
# Branch Merging with Conflicts
If you made different changes to the same part of the same file in two different branches, Git won't be able to merge them cleanly, resulting in a conflict.
When CONFLICT appears during the merge, it indicates a conflict:
$ git merge iss53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
2
3
4
Use git status to see the unmerged status.
Any files with merge conflicts will be marked as unmerged. Git adds standard conflict-resolution markers to the conflicting files, so you can open them and manually resolve the conflicts. Files with conflicts will contain special sections that look like this:
<<<<<<< HEAD:index.html
<div id="footer">contact : email.support@github.com</div>
=======
<div id="footer">
please contact us at support@github.com
</div>
>>>>>>> iss53:index.html
2
3
4
5
6
7
You need to manually resolve the conflicts. After resolving all conflicts in all files, use git add on each file to mark it as resolved. Once you stage the formerly conflicting files, Git considers them resolved.
If you are satisfied with the result and have confirmed that all previously conflicting files have been staged, you can run git commit to finalize the merge commit.