A branch is an independent line of development, separate from the master branch.
####List of branch commands
Display current branch
$ git branch
Compare branches
$ git diff master..other_branch
Create a new branch
$ git branch 'new_branch'
Switch branches
$ git checkout 'branch_name'
Make sure your Working Directory is clean, before switching branches.
Create and switch into a new branch, simultaneously
$ git checkout -b 'new_branch'
Rename a branches
$ git branch -m 'branch_1'
Delete a branches
$ git branch -d 'new_branch'
Branches are cheap-- they are easy to create and delete; which makes them easy to work with. They are a powerful feature on git, because they allow you to collaborate on projects without making unnecessary changes to the Master branch.
Furthermore, collaborators can view your changes, makes comments, and commit additional changes to the branch. Once the work is finished, the branch is merged with master.
According to git-scm.com, merging joins two or more development histories together.
The Master branch should contain finished material.
-
Checkout the branch that is receiving changes
-
If in master branch, and want to merge file_one, e.g.:
$ git merge file_one
Merges can get complicated. To avoid complications, run
$ git merge
with a clean Working Directory.
According to git-scm, If you changed the same part of the same file differently in the two branches you're merging together, Git won't be able to merge them properly. As a result, you'll get a merge conflict that looks like this:
$ git merge new_file
Auto-merging index.html
CONFLICT (conflict): Merge conflicts in index.html
Automatic merge failed; fix conflicts and then commit the results.
-
Abort Merge (easiest)
$ git merge --abort
-
Resolve the Conflict Manually (common)
$ git status
- open file and resolve changes.
- add file
- commit changes
$ git merge file_name
-
Use Merge Tool
-
$ git merge tool
-
This method helps you resolve merge conflicts, but manual editing may be required afterwards. The downside is, you become too confortable with this command, and lack the appropriate knowledge necessary to resove merge conflicts manually.
-
Git will makes changes to your file, in order to show you where the conflict occured. Stack Overflow, provides a guide for reading git merge syntax:
- The part between
<<<<<<
and======
comes fromt theHEAD
revision, which is the committed state prior to the merge operation. - The part between
======
and>>>>>>
comes from the verion being merged. - The part after the
>>>>>>
is the comment of the commit that introduced the conflicting change.
-
Keep commits small and focused.
-
Beware of stray edits to whitespace e.g.
- spaces
- tabs
- lines
-
Merge often.
-
Track changes to master often.
-
Make sure branches stay in sync with master.