Skip to main content
Chapter 14 of 61 · 11 sections · Page 83

Git: Branching

Create, switch, list, delete, rename, search, and track local and remote branches.

Original chapter reference: Chapter 14: Branching. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Parameter Details Delete a branch. The branch must be fully merged in its upstream branch, or in HEAD if no upstream -d, --delete was set with --track or --set-upstream -D Shortcut for --delete --force -m, --move Move/rename a branch and the corresponding reflog -M Shortcut for --move --force -r, --remotes List or delete (if used with -d) the remote-tracking branches -a, --all List both remote-tracking branches and local branches Activate the list mode. git branch <pattern> would try to create a branch, use git branch -- --list list <pattern> to list matching branches If specified branch does not exist yet or if --force has been given, acts exactly like --track. --set-upstream Otherwise sets up configuration like --track would when creating the branch, except that where branch points to is not changed

Section 14.1: Creating and checking out new branches

To create a new branch, while staying on the current branch, use:

Source example · PDF page 83
git branch <name>

Generally, the branch name must not contain spaces and is subject to other specifications listed here. To switch to an existing branch :

Source example · PDF page 83
git checkout <name>

To create a new branch and switch to it:

Source example · PDF page 83
git checkout -b <name>

To create a branch at a point other than the last commit of the current branch (also known as HEAD), use either of these commands:

Source example · PDF page 83
git branch <name> [<start-point>]
git checkout -b <name> [<start-point>]

The <start-point> can be any revision known to git (e.g. another branch name, commit SHA, or a symbolic reference such as HEAD or a tag name):

Source example · PDF page 83
git checkout -b <name> some_other_branch
git checkout -b <name> af295
git checkout -b <name> HEAD~5
git checkout -b <name> v1.0.5

To create a branch from a remote branch (the default <remote_name> is origin):

Source example · PDF page 83
git branch <name> <remote_name>/<branch_name>
git checkout -b <name> <remote_name>/<branch_name>

If a given branch name is only found on one remote, you can simply use

Source example · PDF page 84
git checkout -b <branch_name>

which is equivalent to

Source example · PDF page 84
git checkout -b <branch_name> <remote_name>/<branch_name>

Sometimes you may need to move several of your recent commits to a new branch. This can be achieved by branching and "rolling back", like so:

Source example · PDF page 84
git branch <new_name>
git reset --hard HEAD~2 # Go back 2 commits, you will lose uncommitted work.
git checkout <new_name>

Here is an illustrative explanation of this technique:

Source example · PDF page 84
 Initial state       After git branch <new_name>    After git reset --hard HEAD~2
                             newBranch                        newBranch

↓ ↓

Source example · PDF page 84
A-B-C-D-E (HEAD)         A-B-C-D-E (HEAD)                 A-B-C-D-E (HEAD)

↑ ↑ ↑

Source example · PDF page 84
      master                   master                       master

Section 14.2: Listing branches

Git provides multiple commands for listing branches. All commands use the function of git branch, which will provide a list of a certain branches, depending on which options are put on the command line. Git will if possible, indicate the currently selected branch with a star next to it.

Goal Command List local branches git branch List local branches verbose git branch -v List remote and local branches git branch -a OR git branch --all List remote and local branches (verbose) git branch -av List remote branches git branch -r List remote branches with latest commit git branch -rv List merged branches git branch --merged List unmerged branches git branch --no-merged List branches containing commit git branch --contains [<commit>]

Notes:

Adding an additional v to -v e.g. $ git branch -avv or $ git branch -vv will print the name of the upstream branch as well. Branches shown in red color are remote branches

Section 14.3: Delete a remote branch

To delete a branch on the origin remote repository, you can use for Git version 1.5.0 and newer

Source example · PDF page 84
git push origin :<branchName>

and as of Git version 1.7.0, you can delete a remote branch using

Source example · PDF page 85
git push origin --delete <branchName>

To delete a local remote-tracking branch:

Source example · PDF page 85
git branch --delete --remotes <remote>/<branch>
git branch -dr <remote>/<branch> # Shorter

git fetch <remote> --prune # Delete multiple obsolete tracking branches
git fetch <remote> -p      # Shorter

To delete a branch locally. Note that this will not delete the branch if it has any unmerged changes:

Source example · PDF page 85
git branch -d <branchName>

To delete a branch, even if it has unmerged changes:

Source example · PDF page 85
git branch -D <branchName>

Section 14.4: Quick switch to the previous branch

You can quickly switch to the previous branch using

Source example · PDF page 85
git checkout -

Section 14.5: Check out a new branch tracking a remote branch

There are three ways of creating a new branch feature which tracks the remote branch origin/feature:

Source example · PDF page 85
     git checkout --track -b feature origin/feature,
     git checkout -t origin/feature,

git checkout feature - assuming that there is no local feature branch and there is only one remote with the feature branch.

To set upstream to track the remote branch - type:

Source example · PDF page 85
     git branch --set-upstream-to=<remote>/<branch> <branch>
     git branch -u <remote>/<branch> <branch>

where:

<remote> can be: origin, develop or the one created by user, <branch> is user's branch to track on remote.

To verify which remote branches your local branches are tracking:

Source example · PDF page 85
     git branch -vv

Section 14.6: Delete a branch locally

Source example · PDF page 85
$ git branch -d dev

Deletes the branch named dev if its changes are merged with another branch and will not be lost. If the dev branch does contain changes that have not yet been merged that would be lost, git branch -d will fail:

Source example · PDF page 86
$ git branch -d dev
error: The branch 'dev' is not fully merged.
If you are sure you want to delete it, run 'git branch -D dev'.

Per the warning message, you can force delete the branch (and lose any unmerged changes in that branch) by using the -D flag:

Source example · PDF page 86
$ git branch -D dev

Section 14.7: Create an orphan branch (i.e. branch with no parent commit)

Source example · PDF page 86
git checkout --orphan new-orphan-branch

The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.

source

Section 14.8: Rename a branch

Rename the branch you have checked out:

Source example · PDF page 86
git branch -m new_branch_name

Rename another branch:

Source example · PDF page 86
git branch -m branch_you_want_to_rename new_branch_name

Section 14.9: Searching in branches

To list local branches that contain a specific commit or tag

Source example · PDF page 86
git branch --contains <commit>

To list local and remote branches that contain a specific commit or tag

Source example · PDF page 86
git branch -a --contains <commit>

Section 14.10: Push branch to remote

Use to push commits made on your local branch to a remote repository.

The git push command takes two arguments:

A remote name, for example, origin A branch name, for example, master

For example:

Source example · PDF page 86
git push  <REMOTENAME> <BRANCHNAME>

As an example, you usually run git push origin master to push your local changes to your online repository.

Using -u (short for --set-upstream) will set up the tracking information during the push.

Source example · PDF page 87
git push -u <REMOTENAME> <BRANCHNAME>

By default, git pushes the local branch to a remote branch with the same name. For example, if you have a local called new-feature, if you push the local branch it will create a remote branch new-feature as well. If you want to use a different name for the remote branch, append the remote name after the local branch name, separated by ::

Source example · PDF page 87
git push <REMOTENAME> <LOCALBRANCHNAME>:<REMOTEBRANCHNAME>

Section 14.11: Move current branch HEAD to an arbitrary commit

A branch is just a pointer to a commit, so you can freely move it around. To make it so that the branch is referring to the commit aabbcc, issue the command

Source example · PDF page 87
git reset --hard aabbcc

Please note that this will overwrite your branch's current commit, and as so, its entire history. You might loose some work by issuing this command. If that's the case, you can use the reflog to recover the lost commits. It can be advised to perform this command on a new branch instead of your current one.

However, this command can be particularly useful when rebasing or doing such other large history modifications.

Buy me a coffee
Daily Practice
0/0