Git branching

Introduction

This page documents and gives examples of how to work with branches in git:

View all branches

Terminal:

git branch -a

Sourcetree:

All local and remote branches are visible in the sidebar. Use Fetch to update the list of remote branches.

Create new branch

Note! This can be done with uncommited chenges. All changes will still be there for you to commit on the new branch.

Terminal:

git checkout -b <branch>

Sourcetree:

First make sure that you’re currently on the branch that you want to use as base for the new branch. Click on the Branch button in the toolbar and enter a name for the new branch. Use / to create ”subdirectories”.

Switch branch

Terminal:

git checkout <branch>

Sourcetree:

To switch to another branch, right-click on it’s name in the sidebar and choose the Checkout <branchname> option from the context menu. If you do this on a remote branch you will be asked to create a local copy.

Switch branch with uncommited changes and apply changes to new branch

Terminal:

git stash
git checkout <branch>
git stash pop

Sourcetree:

  1. Click the Stash button in the toolbar.
  2. Optionally give the stash a name.

  1. Switch to the other branch (see above).
  2. Right-click on the stash in the sidebar. (The most recent stash is at the top of the list.)
  3. Choose Apply Stash in the context menu.

Git merge a branch into current branch

Terminal:

git merge <branch>

Sourcetree:

  1. First make sure that you’re currently in the branch that you want to merge into (i.e. make changes to).
  2. Right-click on the branch you want to merge from.
  3. Select Merge <other-branch> into <current-branch>.

Solving conflicts with Sourcetree and Atom

Conflicts occur when two commits make changes to the same parts of a file. Example:

These conflicts can be a bit tricky to solve. In many cases you want to keep both changes. Atom has built in support for handling merge conflicts.

  1. Open the file in Atom. (You can drag it directly from Sourcetree to Atom in your dock.)

Our changes come from the current branch and Their changes are from the other branch that you’re merging from.

  1. Click on Use me if you want to discard one of the chunks in favor of the other.
  2. To keep both changes, click on the three small dots next to one of the buttons and click Resolve as Ours Then Theirs or Resolve as Theirs Then Ours.

  1. After resolving the conflicts you can also make changes manually.
  2. Save the file and return to Sourcetree.

  1. Stage and commit.

Rename branch

On the branch

git branch -m new-name

On another branch

git branch -m old-name new-name

Delete the old-name remote branch and push the new-name local branch.

git push origin :old-name new-name

Reset the upstream branch for the new-name local branch. (when on branch)

git push origin -u new-name

Delete Branch

Local branch

git branch -d <branch_name>

Delete Remote Branch

git push origin --delete <branch_name>

alt

git push origin :<branch_name>

Cleanup your merged pull-requests

After a while you have created a bunch of branches and some have been created as a pull-request which has been successfully merged. Since your terminal will try to give hints which local branch you may checkout the suggestions may grow even if the branch is no longer present on the origin.

Make sure you are on master and is up to date

git checkout master && git pull

Look at which branches the next command will delete locally

git branch --merged | egrep -v "(^\*|master|dev|stage)"

Remove all found branches that have been successfully merged into master

git branch --merged | egrep -v "(^\*|master|dev|stage)" | xargs git branch -d