Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Git and Its Different Branch Types

DZone's Guide to

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.

· Open Source Zone ·
Free Resource

New Report Reveals Open Source Risk Is Still a Mystery to Many. Read more.

Git users, particularly the new Git users, are often easily confused by the differences between:

  • local branches
  • non-tracking local branch
  • local tracking branch
  • remote tracking branch
  • remote branch
  • 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 usinggit 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

    1. To delete a local branch, whether tracking or non-tracking, safely: git branch -d branch_name
    2. To delete a local branch, whether tracking or non-tracking, forcefully: git branch -D branch_name
    3. To delete a remote-tracking branch: git branch -rd remote/branch_name
    4. To create a new local non-tracking branch: git branch branch_name from_ branch_name
    5. To create a new local tracking branch: git branch --track branch_name branch_To_track
      Example: git branch --track feature origin/feature
    6. To delete a branch on a remote machine: git push --delete remote branch_name
    7. To delete all remote-tracking branches that are stale, that are not existed in the remote machine(server) anymore: git remote prune remote
    8. To publish a newly created local branch to the remote server:  git push -u remote branch_name 
    9. To create(checkout) a local branch of remote tracking branch: git checkout branch_name remote/branch_name
      or just as git checkout branch_name

    Software composition Analysis for DevSecOps. Start finding vulnerabilities in your open source components today.

    Topics:
    git ,git branches ,local machine ,remote machine ,open source

    Opinions expressed by DZone contributors are their own.

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}