Track
When we need to work on a branch on a remote repository, such as a colleague's work or a new feature branch, we face the challenge of accessing and updating that branch locally.
Fortunately, Git simplifies this process by offering straightforward commands to create a local copy of the remote branch. This allows us to switch to the desired branch, contribute, and collaborate effectively with the team.
For a quick answer, to checkout a branch named <branch_name>, we run:
git fetch origin <branch_name>
git checkout -b <branch_name> origin/<branch_name>
In this article, we explore these commands and learn how they work in greater detail.
The Two-Step Process: git fetch and git checkout
To start working on a remote branch locally, we need to first make sure that our local repository is up to date with the remote repository. Then, we can create a local copy and start working on that branch.
Updating the local repository
To update the local repository, we use the git fetch command. Its simplest form uses no arguments:
git fetch
When used without arguments, the git fetch command retrieves all branches from the default remote repository. While this includes the branch we want, it will also occupy more local disk space.
If disk space is a concern, we can specify the branch we want to fetch by passing it as a parameter to the command:
git fetch origin <branch_name>
Replace <branch_name> with the name of the desired branch. The origin parameter specifies the name of the remote repository. By default, when we clone a repository, the remote is named origin.
It's important to note that updating the remote references with fetch won’t affect any of the local files or cause conflicts. Locally, there are two distinct entities:
- The current working directory
- The local repository
Our current changes and commits are located in the current working directory. These are the files we see when we open the repository directory on our computer. These don’t get updated with git fetch. Only the local repository is updated, which can be thought of as a hidden copy of the remote repository on our local machine.
Checking out a remote branch
With an up-to-date copy of the remote repository, we can now create a local copy of a remote branch using the git checkout command:
git checkout -b <branch_name> origin/<branch_name>
Replace <branch_name> with the name of the desired remote branch. The first parameter after -b is the name we want the local branch to have. Conventionally, we use the same name for both, but we could use any other name, provided no other local branch has that name:
git checkout -b <local_branch_name> origin/<branch_name>
After successfully executing this command, Git will switch the current working directory to this branch, and we’ll be ready to start working on it.
git checkout vs git switch
The git checkout command has existed since the inception of Git. Its scope extends beyond creating local copies of branches or changing from one branch to another. For instance, it can get specific files from the remote repository.
In recent versions of Git, a git switch command was created whose sole purpose is to create local copies of remote branches and switch between local branches.
I recommend using git switch instead of git checkout because the extended capabilities of git checkout can sometimes lead to errors.
To learn how to perform the same actions we learned in this article using git switch, you can read this tutorial: Git Switch Branch: A Guide With Practical Examples.
Conclusion
Creating a local copy of a remote Git branch locally is very important for collaboration. By using git fetch and git checkout, we can update our local repository with remote changes and switch to the desired branch.
While the classic git checkout provides robust functionality, the newer git switch command offers a streamlined alternative specifically for branch management, reducing the risk of errors.
FAQs
I get an error: "please commit your changes or stash them before you switch branches." How do I fix it?
Git prevents us from changing to another branch if the current branch has uncommitted changes. Resolving this issue involves either stashing our current changes or committing them.
To commit them we use the the following command:
git commit -m "Commit message"
Alternatively, if we wish to preserve the changes without committing them yet, we can use the git stash command to temporarily save our work:
git stash
Later, we can reapply them after returning to the original branch and using the git stash pop command.
What if the remote branch has the same name as a local branch?
Branch names must be unique with Git. If we get an error saying that the branch already exists, it means that there’s a local branch that has the same name. This could mean two things:
- We already checked out this branch before. In this case, we can directly switch to it without fetching using the command
git checkout <branch_name>. Note that we dropped the-bparameter which is used to indicate we want to create a new branch. Since the branch already exists, we don’t need to recreate it. - At some point, we create a different branch that happens to have the same. We can resolve this by using a different branch name with
git checkout -b <different_name> origin/<branch_name>.
Can I make changes directly to a remote branch?
No, changes on a Git branch are made by first creating a local copy of a branch as we learned above, and then creating commits representing the changes we want.
How do I update my local branch with the latest remote changes?
To update our local branch with the latest changes from its corresponding remote branch, we typically use the git pull command:
git pull origin <branch_name>
This performs both a git fetch and a git merge to update our local branch. We learned that git fetch updated the local repository with the latest remote version. The git merge command merges those changes into our local branch. For more details, check this Git push and pull tutorial.
I get an error: "remote branch not found." How do I fix it?
This error indicates that the branch doesn’t exist in our local copy of the repository. So either:
- The branch doesn’t exist, which might indicate a typo in the branch name or that it’s an old branch that was already deleted.
- The local repository isn’t up to date. As we learned above, we can update it using
git fetch.
