304 North Cardinal St.
Dorchester Center, MA 02124
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.
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.
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.
[su_note note_color=”#FFF9C4″] You would need to check out the remote git branch to work with the changes made by other developers.[/su_note]
Jokes apart, lets see what all commands we might need to use to checkout remote branch.
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.
pullrecently and remote might contain changes such as new branches or commits.
$ git checkout master $ git fetch
git checkoutcommand 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>
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@example.com: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?
These commands will work only if you have a git version greater than 1.6.6.
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.