Difference Between Git Checkout --Track Origin/Branch and Git Checkout -B Branch Origin/Branch
This article outlines the difference between the git checkout -b branch origin/branch
and git checkout --track origin/branch
commands. We use the two commands to checkout remote branches in our Git repositories.
The two commands have the same result, but the difference comes in practical use, as we will see shortly.
Difference Between git checkout -b branch origin/branch
and git checkout --track origin/branch
To understand the difference between the two commands, we will explore what each command does when run on the terminal. Let’s jump right in.
the git checkout -b branch origin/branch
Command
If you are well versed with Git, you must know that we use the command below to checkout a remote branch.
$ git checkout <remotebranch>
The command above will create a remotebranch
in our local repo and start tracking the remote branch called remotebranch
if your origin
has the remotebranch
.
What if our local repository already has a remotebranch
and we want to create and track a remote with the same name? How do we go about this?
This is where our git checkout -b branch origin/branch
command comes to play. We will run:
$ git checkout -b remote-branch1 origin/remotebranch
The command above will create a remote-branch1
in our local repository, tracking the remotebranch
in our origin
. Keep in mind that origin
is our remote.
the git checkout --track origin/branch
Command
The git checkout --track origin/branch
command will set up a branch called branch
and track it against branch
in our remote. Sounds like the previous command, right?
The question is, when should you use it?
It would be best to use the command above if our local repository contains several remotes with the same branch name.
Let’s look at an example.
Suppose our local repository has four remotes
with a branch called remotebranch
. As shown below, we can set up a branch to track one of our remotes, in this case, origin
.
$ git checkout --track origin/remotebranch
The command above will create a local branch called remotebranch
and track it against remotebranch
in our origin
.
In a nutshell, the git checkout -b branch origin/branch
and the git checkout --track branch origin/branch
commands have the same result, i.e., create a branch to track a remote.
The difference comes in the practical use of the two, as we discussed.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn