Git checkout remote branch to local and remove it in 2 easy steps

Wondering how to git checkout remote branch to local and then remove it. You can also track it with command line examples shown in the article.

Git is a necessity if you are a de, be it frontend, backend, or full-stack.  We have many tutorials on git, feel free to go checkout.

Recently I was several questions in one of the forums where new developers were finding it difficult to work with Git. Especially with checking out remote branches and working with them. Hence the idea of posting this blog crossed my mind.

Using git to checkout remote branches is a very very common and almost everyday task for developers.

What’s the use of git checkout remote branch?

This comes handy and useful when there are multiple developers working on same codebase and are doing changes concurrently. Each of them can start their own branch and keep doing the changes.

Consider a scenario where a dev X, makes some changes and pushes them to the repository. Now you need to make some changes on top of that but wait you don’t have those changes. So what you might want to do is check out those changes and then do stuff on top of that.

In this way, Git allows the ‘n’ number of developers to work on the same repository at the same time, without conflicts or dependencies.

Also check :   GitHub Profile Readme 101: Awesome Samples & how to create one?

By the way, the command to check out the branch is not git checkout remote branch. It is just the name of the mechanism.

But the silver line remains the same.

You would need to check out the remote git branch to work with the changes made by other developers.

Examples where you might need this:

  • A dev worked on the branch and introduced a bug in code and wants you to fix it now.
  • Prioritization of task changes the dev working on a story and somebody else’s task is handed over to you.
  • A dev left the company 🙂

Jokes apart, lets see what all commands we might need to use to checkout remote branch.

Git checkout remote branch command

This is fairly straightforward to do. It Just requires 3 commands to be run on command-line. So open your favorite terminal again and let’s do it.

  • Go to the local repository folder.
  • Run a fetch command which will basically fetch all the branches from the remote repository. This is required only if you have not taken a pullrecently and remote might contain changes such as new branches or commits.
$ git checkout master
$ git fetch
  • Then to check out the remote repo branch, we can use git checkout command but with flag -t . This flag tells the git that we are trying to fetch the remote branch.
$ git checkout -t <remote-repo>/<remote-branch>

Example:

I will be using my landing Github page repo. Feel free to check out if you need a minimalistic landing page.

$ git checkout -t [email protected]:singhkshitij/My-Landing-Page.git/gh-pages

This will make a local branch with the same name as the remote repo branch name. However, if you want to rename the branch while fetching, use the below command.

$ git checkout -b <local-branch> <remote-repo>/<remote-branch>

Check this guide if you are interested in how to clone a branch?

Also check :   How to git remove file from commit after push or staging

These commands will work only if you have a git version greater than 1.6.6.

Git track branch

To track branch while checking out simply use below command and it will automatically switch to a newly fetched branch.

$ git checkout --track origin/gh-pages

Basically just add --track flag to the command.

Check this linked guide, if you are interested in how to git checkout remove branch. In fact, if your changes are in staging and you want to delete them, here is another guide to remove the file from a commit.

Let us know if you have a better suggestion to do so same stuff. I would love to hear back.

Get occasional tutorials & guides in your inbox. No spam ever.


Buy Me A Coffee

Leave a Comment