A branch is a pointer to a commit, and Remote Branches are branches present in the remote repository.
When we clone a remote repository to our local machine, Git will create a default master branch. However, all the other Remote Branches appear as remote-tracking branches.
A remote-tracking branch is a copy of a remote branch and tracks the changes of its corresponding remote branch.
For example, consider a remote repository with three branches - master, feature, and development.
Remote Repository
G <- development
/
/
A -- B -- C -- D <- master <- HEAD
\
\
E -- F <- feature
When we clone this repo to our local machine(using Git Clone), we will get a master branch. Also, we will have three remote-tracking Branches in our local repository - origin/master, origin/feature, origin/development.
Local Repository
G <- origin/development
/
/
A -- B -- C -- D <- master <- HEAD, origin/master
\
\
E -- F <- origin/feature
Let's learn how to clone all the remote branches. In other words, we are trying to create a Local Branch that is a replica of a Remote Branch.
As we saw in the example above, when we clone a repository, we get a remote-tracking branch for each remote branch.
If we only wish to view the commits present on the remote branches, we can directly check out the Remote-Tracking Branches. Note that this will lead to a Detached-HEAD state. We can navigate and observe the changes and even make experimental commits. However, these commits will be lost when we switch to a different branch. Use the Git Checkout command and pass the remote-tracking branch name to switch to it.
git checkout <remote-tracking-branch>
The output for the above command is shown below.
git checkout feature
Output
Note: switching to 'origin/feature'.
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 switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 09013cb new
If we wish to work on a remote branch and add new changes, we need to create a new branch. The new branch points to the same commit as the remote-tracking branch. Again, we can use the Git Checkout command to create and check out a new branch.
git checkout <new-branch-name>
This new branch will also track the corresponding remote branch. And we can easily push changes from our local repo to the remote.
Note that the above command only works if we give the same name as the remote branch name. For example, if the remote branch is called feature, but you want a local copy which is called local-feature, then the above command will not work. Use the following command if you wish to give a different name to the local branch.
git checkout -b <new-branch-name> <remote-tracking-branch-name>
git checkout -b local-feature origin/feature # for our example case
Consider a remote repository with three branches - master, feature, and development.
Remote Repository
G <- development
/
/
A -- B -- C -- D <- master <- HEAD
\
\
E -- F <- feature
Let's clone this repo to our local machine. We will have a master branch and three remote-tracking branches - origin/master, origin/development, and origin/feature.
Local Repository
G <- origin/development
/
/
A -- B -- C -- D <- master <- HEAD, origin/master
\
\
E -- F <- origin/feature
It is a good idea to run the Git Fetch command if you cloned the repository a while ago. It will download any new changes that we are not aware of.
git fetch --all
Let's verify the existence of the remote-tracking branches by using the Git Branch -r command. Note that all remote-tracking branches have their names of the format remote-name/remote-branch-name.
git branch -r
Output
origin/HEAD -> origin/master
origin/development
origin/feature
origin/master
Now, let's say that we want to work on the feature branch. Let's create and check out a new branch. This new branch will be our local clone for the remote feature branch.
git checkout feature
Output
Switched to a new branch 'feature'
Branch 'feature' set up to track remote branch 'feature' from 'origin'.
Local Repository
G <- origin/development
/
/
A -- B -- C -- D <- master, origin/master
\
\
E -- F <- feature <- HEAD, origin/feature
We can directly push our local changes to the remote feature branch by running the Git Push command.
git push
We will follow the same steps if we want a local clone of the development branch(or any other remote branch).
When we clone a Git repository, we will only have the default master(or main, in some cases) branch in our local repo. All the other remote branches will be present as remote-tracking branches. We can verify this by running the Git Branch command with the -r flag.
To create a local clone of a remote branch, we need to create a new branch pointing to the remote-tracking branch. Use the Git Checkout command to achieve this. To just view the changes without adding anything new, directly checkout the remote-tracking branch.