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 these two commands to check out remote branches in a Git repository.
These two commands have the same result, but the difference lies in actual usage, as we will see shortly.
Difference between git checkout -b branch origin/branch and git checkout --track origin/branch
To understand the difference between these two commands, we’ll explore what each command does when run on the terminal. Let’s jump right in.
git checkout -b branch origin/branch command
If you are familiar with Git, you must know that we use the following command to check out the remote branch.
$ git checkout <remotebranch>
The above command will create a remotebranch in our local repo and start tracking a remote branch named remotebranch (if your origin has remotebranch ).
What if our local repository already has a remotebranch and we want to create and track a remote branch with the same name? How do we do that?
This is where our git checkout -b branch origin/branch
command comes into play. We will run:
$ git checkout -b remote-branch1 origin/remotebranch
The command above will create a remote-branch1 in our local repository , tracking remotebranch in our origin . Remember, origin is our remote.
git checkout --track origin/branch command
git checkout --track origin/branch
The command will create a branch called branch and track it in our remote branch. Sounds like the previous command, right?
The question is, when should you use it?
It is better to use the command above if our local repository contains multiple remotes with the same branch name.
Let’s look at an example.
Assume our local repository has four remote repositories and 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 based on remotebranch in our source .
In short, git checkout -b branch origin/branch
the and git checkout --track branch origin/branch
commands have the same result, i.e. creating a branch to track the remote.
As we discussed, the difference lies in the actual use of the two.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Git shows remote tracking branches
Publish Date:2025/04/03 Views:125 Category:Git
-
Branches on remote Git repositories are called remote branches. These are pointers to our remote repositories, including branches, tags, etc. Local branches only exist on each developer's local personal computer, but there is only one remot
View merged and unmerged branches in Git
Publish Date:2025/04/03 Views:96 Category:Git
-
This article discusses how to list merged and unmerged branches in Git. Git branches encourage convergent evolution of code. This is where we create a branch as a temporary space to work on a feature, and then merge the branch with its orig
The difference between Fork and Branch on GitHub
Publish Date:2025/04/03 Views:157 Category:Git
-
This article discusses the difference between Form and Branch on GitHub. In the context of coding, the current era relies more on collaboration. GitHub is one of the most commonly used collaboration tools. Forking and branching on GitHub ar
How to determine the current branch in Git
Publish Date:2025/04/03 Views:164 Category:Git
-
Git is a unique and popular version control system that is used by most of the software developers to keep an eye on the changes made in various applications and stay connected with other teams on the running projects. It helps large teams
Difference between Git Merge Origin/Master and Git Pull
Publish Date:2025/04/03 Views:195 Category:Git
-
This article outlines the differences between the git merge origin/master and git pull commands. These two commands integrate changes from a remote repository into the current local branch. However, each command's operation is unique and ha
Git Pull Origin branch overwrites Master branch
Publish Date:2025/04/03 Views:143 Category:Git
-
This article explains how we can revert the changes made to the master git pull origin branch branch after running the command . Assume that you have a master branch and a feature branch in your local and remote repositories . You pull chan
Git Pull and Merge Commands
Publish Date:2025/04/03 Views:56 Category:Git
-
In the vast world of version control systems, Git is the only fast, well-organized, in-demand, and easily accessible version control system that enables users to work on various projects simultaneously in an efficient manner without any col
Copy files from another branch in Git
Publish Date:2025/04/03 Views:52 Category:Git
-
In Git, merging various files can lead to a lot of conflicts. Through these merge conflicts, our files may be compromised, so we have to copy these files or folders from one branch to another to keep them safe. A popular approach is cherry
Push to a specific branch in Git
Publish Date:2025/04/02 Views:54 Category:Git
-
In Git, we use branches to develop independent features directly from the main workflow of the project. Since Git is considered to be the best version control system so far, we have local and remote branches in our repository for different