Git and Its Different Branch Types
Git and Its Different Branch Types
For any new Git users, take a look at the different types of branches that run on local and remote machines and their differences.
Join the DZone community and get the full member experience.Join For Free
Sensu is an open source monitoring event pipeline. Try it today.
Git users, particularly the new Git users, are often easily confused by the differences between:
In this article, I will explain each of them in details with specific examples.
Based on the location of the branch, branches can be categorized into two groups: branches on a local machine and branches on a remote machine. The local branch, local tracking branch, and remote tracking branch belong to the branches on the local machine, while the remote branch belongs to branches on the remote machine.
Branches on Local Machine
1- Local branch can split into two types of branches: non-tracking local branch and local tracking branch. You can view your local branches by running command
git branch, and you will get the output as below:
The output shows that there only two local branches, "feature" and "master", on my laptop.
1.1- Non-tracking local branches are standalone branches that are not associated with any other branches, you can create a non-tracking branch by running
git branch newBranchName
as shown below to create a new branch called “new branch”.
1.2 - Local tracking branches are associated with other branches, and the associated branch can be non-tracking local branches and remote tracking branches. Usually it does not make any sense just to track a non-tracking local branch, so it usually is associated with remote tracking.
You can view which one of your local branches are tracking branches and non-tracking local branches using
git branch -vv:
From this command’s output, you can see that the local branch "master" is tracking the remote-tracking branch "origin/master", and the local branch “feature” and “new branch” are not tracking anything, just sitting there alone. You can run the command
git branch --track localbranch branch-To-track
to create one local tracking branch where "localbranch" is your local branch, and "branch-to-track" is the branch you want to associate with (to track). The effect of this command is that it creates a new branch and in the meantime the new branch tracks a branch “branch-to-track”.
It has to be noted that a non-tracking local branch (like the “feature” branch) can turn into a local tracking branch by running the command:
git branch --set-upstream-to= remote/branch localbranch. For example, we can set up the “feature” branch to track the “origin/master” remote tracking branch as below:
From this output of command, you can see that the “feature” branch starts tracking the “origin/master”.
1.3 - Remote tracking branches are references (points) to the state of branches (remote branches) in your remote machines (repos). They are local branches that you can not move; you can update your remote-track branches using
git fetch. The remote branch has the form of (remote)/(branch). For instance, we have seen in the previous the "origin/master".
you can view a list of all the remote-tracking branches on your machine by running
git branch -r:
Branches on Remote machine
Remote branch is a branch located on a remote machine or server. You can view all the remote branches (that is, the branches on the remote machine), by running
git remote show:
You can push the newly-created local branch myNewBranch to origin by running
git push -u origin myNewBranch. Now other users can track it. For example, we push the “feature” branch to the remote server as below shown:
Other Frequently-Used Branch-Related Commands
- To delete a local branch, whether tracking or non-tracking, safely:
git branch -d branch_name
- To delete a local branch, whether tracking or non-tracking, forcefully:
git branch -D branch_name
- To delete a remote-tracking branch:
git branch -rd remote/branch_name
- To create a new local non-tracking branch:
git branch branch_name from_ branch_name
- To create a new local tracking branch:
git branch --track branch_name branch_To_track
git branch --track feature origin/feature
- To delete a branch on a remote machine:
git push --delete remote branch_name
- To delete all remote-tracking branches that are stale, that are not existed in the remote machine/server anymore:
git remote prune remote
- To publish a newly created branch to the remote server:
git push -u remote branch_name
- To create(checkout) a local branch of remote tracking branch:
git checkout branch_name remote/branch_name
or just as
git checkout branch_name
Opinions expressed by DZone contributors are their own.