Git is a wonderful tool for code repository management. But sometimes it is little confusing to work over remote branches. Here is a small guided example regarding how to work with git remote branch. The following details covers only the basics to start working with remote branches.
Let’s take an actual git repository and do some testing of various git commands. I am using the RouteFlow repository here. You can go ahead and use any other repository you want.
First let us checkout the git repo. We can do this using "git clone"
command which asks the [url]
of the repo as an argument to clone locally.
ubuntu@ubuntu-vobx:~/test$ git clone https://github.com/routeflow/RouteFlow.git Cloning into 'RouteFlow'... remote: Counting objects: 4053, done. remote: Compressing objects: 100% (13/13), done. remote: Total 4053 (delta 15), reused 5 (delta 5), pack-reused 4035 Receiving objects: 100% (4053/4053), 4.08 MiB | 1.32 MiB/s, done. Resolving deltas: 100% (1879/1879), done. ubuntu@ubuntu-vobx:~/test$ cd RouteFlow/
Now we will use "git branch"
command to see the list of locally available branches. The output of this command also put a star mark in front of the currently active branch.
ubuntu@ubuntu-vobx:~/test/RouteFlow$ git branch * master
To list down all the local branches and remote branches available for this repo, we can use the flag "-a"
. We can also use flag "-v"
to get a detailed output here.
ubuntu@ubuntu-vobx:~/test/RouteFlow$ git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/cardigan remotes/origin/gh-pages remotes/origin/ha remotes/origin/master remotes/origin/of13 remotes/origin/vandervecken ubuntu@ubuntu-vobx:~/test/RouteFlow$ git branch -a -v * master a066b41 Update README.md remotes/origin/HEAD -> origin/master remotes/origin/cardigan 2c8f0f2 Disable IPC debugging. remotes/origin/gh-pages 3bf0ca1 Updated links for the new organization. remotes/origin/ha 529e819 * Simplified README. * Fixed start order for rfvm1 in rftest1. remotes/origin/master a066b41 Update README.md remotes/origin/of13 257deb5 Update README.md remotes/origin/vandervecken fd9f76d Merge pull request #23 from solidgoldbomb/fix-corsa-group-mod-vlan
Now we know what are the remote branches available. Let’s try to checkout the remote branch "vandervecken"
. The most common mistake we do is as below:
ubuntu@ubuntu-vobx:~/test/RouteFlow$ git checkout remotes/origin/vandervecken Note: checking out 'remotes/origin/vandervecken'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b new_branch_name HEAD is now at fd9f76d... Merge pull request #23 from solidgoldbomb/fix-corsa-group-mod-vlan ubuntu@ubuntu-vobx:~/test/RouteFlow$ git branch * (no branch) master
We should note that after the command the "git branch"
command says that "no branch"
is our currently active branch which is not what we want. So let’s go back to the master branch again.
ubuntu@ubuntu-vobx:~/test/RouteFlow$ git checkout master Previous HEAD position was fd9f76d... Merge pull request #23 from solidgoldbomb/fix-corsa-group-mod-vlan Switched to branch 'master' ubuntu@ubuntu-vobx:~/test/RouteFlow$ git branch * master
To chekout the remote vandervecken branch we can use the below command:
ubuntu@ubuntu-vobx:~/test/RouteFlow$ git checkout vandervecken Branch vandervecken set up to track remote branch vandervecken from origin. Switched to a new branch 'vandervecken' ubuntu@ubuntu-vobx:~/test/RouteFlow$ git branch -a master * vandervecken remotes/origin/HEAD -> origin/master remotes/origin/cardigan remotes/origin/gh-pages remotes/origin/ha remotes/origin/master remotes/origin/of13 remotes/origin/vandervecken
You can notice that "git branch -a"
has two listing of vandervecken. The first one with "*"
mark is the local branch we just created and the other listing is the remote branch. Always confirm your branches using the "git branch -a"
command.
Sometimes we need to delete the locally created branch. Git provides easy way to delete local branches. We just need to use "-d"
flag with the already known "git branch"
command and the branch name.
ubuntu@ubuntu-vobx:~/test/RouteFlow$ git branch -d vandervecken warning: deleting branch 'vandervecken' that has been merged to 'refs/remotes/origin/vandervecken', but not yet merged to HEAD. Deleted branch vandervecken (was fd9f76d). ubuntu@ubuntu-vobx:~/test/RouteFlow$ git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/cardigan remotes/origin/gh-pages remotes/origin/ha remotes/origin/master remotes/origin/of13 remotes/origin/vandervecken ubuntu@ubuntu-vobx:~/test/RouteFlow$
We can see that only the local branch got deleted where as the remote branch is still in the listing of branches.
We can also checkout a remote branch with a different name than the remote name.
ubuntu@ubuntu-vobx:~/test/RouteFlow$ git checkout -b vv remotes/origin/vandervecken Branch vv set up to track remote branch vandervecken from origin. Switched to a new branch 'vv' ubuntu@ubuntu-vobx:~/test/RouteFlow$ git branch -a master * vv remotes/origin/HEAD -> origin/master remotes/origin/cardigan remotes/origin/gh-pages remotes/origin/ha remotes/origin/master remotes/origin/of13 remotes/origin/vandervecken
Note:
It is possible that some one has created a new remote branch after you cloned the repository. In that case git repo can be synchronised using the "git fetch"
command. This will allow us to view all the new remote branches in the "git branch -a"
command output and to checkout the new remote branches.
Hope this information will help you all. Provide your valuable feedback.