Branches are copies of a repository where you can create changes without modifying the parent or master file. The master branch is insulated from changes, so you don’t have to worry if you break your code while branching.
Branching is useful for experimental changes, fixing bugs and changes you aren’t sure you want to commit yet. Branching is especially useful when working on a project with multiple developers.
Branching should be used for discreet, singular issues. Once the particular issue is resolved, the branch can be merged back into its parent or master.
Status & Log
git status Shows you the status of the working directory, allows you to confirm files have been merged, and shows you which files are unmerged after a merge conflict.
git log Lets you explore previous revisions of a project.
Creating, Deleting & Renaming Branches
git branch Provides a list of all branches in repository. Current branch will be noted with an asterisk.
git branch <name> Creates a new branch named <name>. It’s also possible to create a new branch with checkout, as discussed in the next section.
You will want to delete branches once they have been successfully merged (i.e. without conflicts) back into their parent or master branch, as discussed in a section below:
git branch -d <name> Deletes the branch. Using -d is the safe option, as GitHub will prevent you from deleting this branch if there are unmerged changes. To force delete, use -D:
git branch -D <name> Force deletes a branch.
git branch -m <newname> Renames the branch.
checkout is the term used for navigating, or moving between, branches. Simply enter git checkout followed by the branch name to navigate to a branch or back to master:
git checkout master Returns to the master branch
git checkout <branch> Checks out a previously created branch.
It’s also possible to create a branch and navigate to that branch with a single command:
git checkout -b <newbranch> Creates and checks out a new branch simultaneously.
git checkout -b <newbranch><branch> Creates and checks out a new branch based on an existing branch and not the current branch.
Merging is the process by which a branch is merged back into its parent or master branch, or into another branch. You will want to be in the destination branch when you execute the merge command.
git merge To integrate changes from divergent branches.
git merge <branch> Merges specified branch into current branch (or master if you’re in master).
git merge –no-ff <branch> Merges the specified branch into the current branch (but always generate a merge commit).
Once you merge a branch back into the master, you’ll want to delete that branch using the delete commands above.
Merge conflicts occur when you change the same part of the same file in two branches that are merging, and git can’t merge them cleanly. In this case, git will pause the merge process while you resolve the conflict.
When a merge conflict occurs, your terminal will alert you to this withCONFLICT and let you know the merge has failed. You will have to resolve the conflicts, recommit the result and repush to your repository.
To resolve a conflict, open the file in which the conflict exists. You should see <<<<<<< HEAD and >>>>>>> <branch name>. Your conflict lies between these. It’s up to you how you want to resolve the conflict. Once resolved, commit and push.
Rebase is used in case the master or parent is modified while you are working on a branch. It’s a way of making sure the master or parent branch from which you pulled is consistent when you merge your branch. This is important, as you want to work out any merge issues in this regard in the branch and not in the master or parent.
If you are the only person working on a project, rebase won’t be necessary. However, when multiple developers are working on a project, there is a chance the file from which you created your branch has been modified.
When it’s time to rebase, checkout your branch and use git rebase <branch> where <branch> is the branch that is being rebased on to. This means if you want to update your new branch with any changes that may have taken place in master, you would git checkout <newbranch> and git rebase <master>. If there are errors following rebase, this will be noted in terminal.
Step by Step
Say you are working on a master branch git-todo and you want to create a branch called newproduct which will subsequently be merged into the master branch, you would follow these steps:
(1) Create branch. git branch newproduct
(2) Navigate to the new branch. git checkout newproduct
(3) Make local changes to branch.
(4) Commit your branch changes and push to GitHub. git add . followed by git commit -m “message re new product”
(5) Move back to destination branch, or master. git checkout master
(6) Merge branch to the destination. git merge newproduct
(7) Once there are no merge conflicts (if there are, they will need to be resolved), delete the branch. git branch -d newproduct
(8) Commit the revised destination branch or master file. git add . followed by git commit -m “message re commit”
(9) Push to GitHub repo. git push origin master
If at any time you’re confused as to where you are in the process, git status and git log are your friends.
- PM Career Story - April 28, 2022
- How to Transition into Product Management - December 26, 2017
- What I’ve Learned in My First Few Months as a Product Manager - October 14, 2015