How to checkout a remote Git branch

0
254

[ad_1]

fatmawati achmad zaenuri/Shutterstock.com

To check out a branch from a remote repository, use the ‘git fetch’ command and then ‘git branch -r’ to list the remote branches. Choose the branch you need and use a command of the form ‘git checkout -b new-branch-name origin/remote-branch-name’. If you use multiple repositories, change the ‘origin’ part of the payment command to the name of the remote you want to pay the branch from.

If your development team uses Git, you will eventually need to check out someone else’s work as a branch from a remote repository. Like most branch actions in Git, switching to a remote branch is pretty simple.

Git, branches, and remotes

Git’s philosophy is to fork often. Branches allow development to take place without altering the main code base. When you’re satisfied that your new tested code is ready, merge your new branch into another branch. This is usually the main or master branch, but you can merge any two branches.

Because of this flexibility, and the light and fast way Git handles branches and merges, branching was transformed. In earlier version control systems, branching was a big problem. Branching and merging was slow and error prone. Git gave developers a quick and easy fork that is used to support many different workflows.

If you work or volunteer as part of a development team that uses Git, you’ll have a “central” Git repository, away from each software engineer’s computer. This is known as the remote repository, or simply the “remote”. This is where commits and changes are pushed to your local repository when you do a push.

Of course, that’s what the other developers are doing too. This facilitates collaboration. If you need to access another developer’s work, simply retrieve your code from a branch in the remote repository. If they need to access your work, they’ll retrieve your code from a branch in the repository that tracks one of your local branches.

In Git, a development project can have multiple remotes. However, a local branch can only track a single remote branch. So as long as you are working with the appropriate remote, checking a remote branch with multiple remotes is the same as using a single remote.

Find your local branches

It is necessary to avoid name conflicts. If you have a local branch that has the same name as the remote branch you’re checking out, you have two options. You can change the name of your local branch and check the remote branch. That way, your local branch that tracks the remote branch has the same name as the remote branch. Or, you can check out the remote branch and tell Git to create a local tracking branch with a new name.

To find out the names of the branches in your local repository, use the git branch domain.

git branch

List local branches with the git branch command

This local repository has a master branch and three other branches. The asterisk indicates which is the current branch. Moving from one branch to another requires verifying the branch you want to work with.

git checkout new-feature
git status

Checking a local branch with the git checkout command

The first command changes the branch for us, so “new feature” is the current branch. the git status command checks that for us.

We can jump from one branch to another, make new changes, pull updates from the remote, and push local updates to the remote.

RELATED: How to update and maintain separate Git branches

Checking a remote branch

There is a branch in the remote repository that is not present on our machine. A developer named Mary has created a new feature. We want to switch to that remote branch so we can build that version of the software locally.

If we make a fetchGit will retrieve the metadata from the remote repository.

git fetch

Using the git fetch command to retrieve metadata about a remote repository

because this is the first fetch we have since Mary pushed her branch to the remote repository. We’re told there’s a new branch called “Mary’s origin/feature”. The default name for the first remote repository added to a project is “origin”.

Whether we see this message or not, we can always ask Git to list the branches in the remote repository.

the -r (remote) tells Git to report branches that are in the remote repository.

git branch -r

Using the git branch -r command to list remote branches

The point to note here is that Git is checking your local copy of the remote control metadata. That’s why we use the git fetch to ensure that the local copy of the metadata is up to date.

Once we locate the branch we want, we can go ahead and check it out. we use the git checkout command with the -b (branch), followed by the name we’ll use for the local branch, followed by the name of the remote branch.

git checkout -b mary-feature origin/mary-feature

Checking a remote branch with the git checkout -b command

We can see that we checked out the remote branch and created a local branch that will track changes to the remote branch.

git branch

Listing local branches with the git branch command, with the newly created copy of the remote branch selected as the current branch

Our new local branch is now our current working branch.

Handling name conflicts

If you have a local branch that has the same name as the remote branch, you can change the name of your local branch before checking out the remote branch, or check out the remote branch and specify a different local branch name.

To pull the remote branch into a local branch with a different name, we can use the same command we used above and choose a new local branch name.

git checkout -b mary-test origin/mary-feature

Checking a remote branch with the git checkout -b command with the local branch having a different name than the remote branch

This creates a local branch called “mary-test” which will track the local commits on that branch. Clicks will go to the remote “origin/mary-feature” branch.

This is probably the best way to handle local name conflicts. If you really want to keep the same local and remote branch name, you’ll need to change the name of your local branch before checking the remote one. Renaming a branch is trivial in Git.

git branch -m mary-feature old-mary-branch

Rename a branch with the git branch -m command

You can now check out the remote branch “origin/mary-feature”.

Managing multiple remote repositories

If you have multiple remote repositories configured, you must be careful that you are working with the appropriate repository when you check out the remote branch.

To list your remote repositories, use the remote command with the -v (see) option.

git remote -v

Listing remote repositories with the git remote -v command

To see all available branches, we need to get the metadata for all our remotes, and then list the remote branches.

git fetch --all
git branch --all

Using git fetch --all to update local metadata and using git branch --all to list all branches, local and remote

We can see that the branch we want is on the “origin” remote. The command to check it is in the same format that we have already used. We need to specify the remote name, “origin”, as well as the branch name, “mary-feature”.

git checkout -b mary-feature origin/mary-feature

Checking a remote branch with the git checkout -b command, using the remote name and the branch name

RELATED: How to change, add, and remove Git remotes

Before paying

Before you pay, keep a few things in mind and you’ll be fine.

Be sure to avoid name conflicts. If you have a local branch with the same name as the remote branch, decide whether to rename the local branch or create a branch with a different name to track the remote branch.

If you use multiple remote repositories, make sure you use the correct remote.

RELATED: Git rebase: Everything you need to know

[ad_2]