Course Tonight

Course Tonight

Did You Know?

We design Docy for the readers, optimizing not for page views or engagement

Chapter 14: Branching

Estimated reading: 7 minutes 5 views
Parameter Details
-d, --delete Delete a branch. The branch must be fully merged in its upstream branch, or in HEAD if no upstream 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.
--list Activate the list mode. git branch <pattern> would try to create a branch, use git branch --list <pattern> to list matching branches.
--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:

git branch <name>

To switch to an existing branch, use:

git checkout <name>

To create a new branch and switch to it, use:

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:

git branch <name> [<start-point>]
git checkout -b <name> [<start-point>]

The <start-point> can be any revision known to Git, such as another branch name, commit SHA, or a symbolic reference like HEAD or a tag name.

Examples:

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), use:

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:

git checkout -b <branch_name>

which is equivalent to:

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” using the following commands:

git branch <new_name>
git reset --hard HEAD~2 # Go back 2 commits, you will lose uncommitted work.
git checkout <new_name>

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 certain branches depending on the options used. 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 the following commands:

For Git version 1.5.0 and newer:

git push origin :<branchName>

As of Git version 1.7.0:

git push origin --delete <branchName>

To delete a local remote-tracking branch:

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):

git branch -d <branchName>

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

git branch -D <branchName>

Section 14.4: Quick switch to the previous branch

You can quickly switch to the previous branch using:

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:

  • 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:

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 the user.
  • <branch> is the user’s branch to track on the remote.

To verify which remote branches your local branches are tracking, use:

git branch -vv

Section 14.6: Delete a branch locally

To delete a branch named dev if its changes are merged with another branch and will not be lost, use the following command:

$ git branch -d dev

If the dev branch contains changes that have not yet been merged and would be lost, the git branch -d command will fail. In that case, you can force delete the branch (and lose any unmerged changes) by using the -D flag:

$ git branch -D dev

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

To create an orphan branch named new-orphan-branch with no parent commit, use the following command:

git checkout --orphan new-orphan-branch

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

Section 14.8: Rename a branch

To rename the branch you have checked out, use the following command:

git branch -m new_branch_name

To rename another branch, use the following command:

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, use the following command:

git branch --contains <commit>

To list local and remote branches that contain a specific commit or tag, use the following command:

git branch -a --contains <commit>

Section 14.10: Push branch to remote

To push commits made on your local branch to a remote repository, use the following command:

git push <REMOTENAME> <BRANCHNAME>

For example, you can run git push origin master to push your local changes to your online repository. Using the -u flag will set up the tracking information during the push.

git push -u <REMOTENAME> <BRANCHNAME>

By default, git push pushes the local branch to a remote branch with the same name. If you want to use a different name for the remote branch, append the remote name after the local branch name, separated by ::

git push <REMOTENAME> <LOCALBRANCHNAME>:<REMOTEBRANCHNAME>

Section 14.11: Move current branch HEAD to an arbitrary commit

To make a branch refer to the commit aabbcc, use the following command:

git reset --hard aabbcc

Please note that this will overwrite your branch’s current commit and its entire history. You might lose some work by issuing this command. If that’s the case, you can use the reflog to recover the lost commits. It is advised to perform this command on a new branch instead of your current one. However, this command can be particularly useful when rebasing or making other large history modifications.

Leave a Comment

Share this Doc

Chapter 14: Branching

Or copy link

CONTENTS