A Branch is a pointer to a commit. Branches provide developers an independent space where they can create and test new features.
A Branch can exist in the local repository or the remote repository. To view the changes present in a Remote Branch, we must clone the branch to our local system.
If we initialize a local repository using Git Clone, then all the remote branches are fetched, and we will be "checked out" on the default branch(master, in most cases). Let's learn how to clone a single specific branch in Git.
The Git Remote command helps us to maintain and manage remote connections. With the help of this command, we can clone a specific branch to our local repository.
The first step is to initialize an empty local Git repository. Use the Git Init command to do so.
git init
Next, we need to add a remote using the Git Remote command. Pass the remote name(origin, in most cases) and the remote repository URL.
git remote add <remote-name> <remote-repo-URL>
Next, we can fetch the required branch using the Git Fetch command.
git fetch <remote-name> <branch-name>
Finally, we can check out that branch. The Git Checkout command will also create a tracking relationship between the local and remote branches. We can create new commits and push and pull changes.
git checkout <branch-name>
Another alternative is to use the -t and -f flags with the Git Remote command. We don't need the Git Fetch command if we use these flags.
git init
git remote add -t <branch-name> -f <remote-name> <remote-repo-URL>
git checkout <branch-name>
Consider a remote repository with three branches - master, feature, and development. We want to clone the feature branch into a Local Repository.
First, let's initialize a Git repository using the Git Init command.
git init
Output:
Initialized empty Git repository in C:/Users/Lenovo/Desktop/Demo/.git/
Next, let's use the Git Remote command to add a new connection to the remote repository. Let's call this remote origin.
git remote add origin https://github.com/alice/Demo
Now, let's fetch the feature branch.
git fetch origin feature
Output:
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 17 (delta 5), reused 13 (delta 3), pack-reused 0
Unpacking objects: 100% (17/17), 1.69 KiB | 0 bytes/s, done.
From https://github.com/alice/Demo
* branch feature -> FETCH_HEAD
* [new branch] feature -> origin/feature
We can replace the above two commands(Git Remote and Git Fetch) with a single command.
git remote add -t feature -f origin https://github.com/alice/Demo
Finally, let's create a new local branch called feature. This feature branch is a clone of the remote feature branch. The Git Checkout also creates a tracking relationship between the two branches.
git checkout feature
Output:
Switched to a new branch 'feature'
Branch 'feature' set up to track remote branch 'feature' from 'origin'.
Let's verify the branches by running the Git Branch command with the -a flag(short for --all).
git branch -a
Output:
* feature
remotes/origin/feature
The Git Clone command clones a remote repository to our local machine. We can also use it to clone specific branches. There are two methods to clone a branch using Git Clone.
The first method uses the --branch(-b in short) option with Git Clone. The --branch option with the Git Clone command will fetch all the remote branches.
However, this command will check out only the mentioned branch. The tracking relationship between the Local Branch and the Remote Branch is also set up.
git clone --branch <branch-name> <remote-repo-URL>
A better alternative is to use the --single-branch option with the above command. This will only fetch the mentioned branch. All the other branches of the remote repository are not downloaded. This also helps in saving memory. Note that we still need to use the --branch(or -b) option with --single-branch.
This command will also create a tracking connection between the local and remote branches.
git clone --branch <branch-name> --single-branch <remote-repo-URL>
Consider a remote repository with three branches - master, feature, development. Let's try to clone the feature branch using the above methods.
First, let's use the only --branch option.
git clone --branch feature https://github.com/alice/Demo
Output:
Cloning into 'Demo'...
remote: Enumerating objects: 23, done.
remote: Counting objects: 100% (23/23), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 23 (delta 9), reused 17 (delta 5), pack-reused 0
Receiving objects: 100% (23/23), done.
Resolving deltas: 100% (9/9), done.
Let's run the Git Branch -a command to view the fetched branches. We can see that all three remote branches were downloaded.
However, the command only creates a single Local Branch(feature). All the other branches exist as Remote-Tracking Branches.
git branch -a
Output:
* feature
remotes/origin/HEAD -> origin/master
remotes/origin/development
remotes/origin/feature
remotes/origin/master
Now, let's use the --single-branch option.
git clone --branch feature --single-branch https://github.com/alice/Demo
Output:
Cloning into 'Demo'...
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 17 (delta 5), reused 13 (delta 3), pack-reused 0
Receiving objects: 100% (17/17), done.
Resolving deltas: 100% (5/5), done.
Let's view the output of the Git Branch -a command. We can see that only the feature branch was downloaded. We have a Local Branch called feature and a Remote-Tracking branch called origin/feature.
git branch -a
* feature
remotes/origin/feature
Cloning an entire repository doesn't make sense when we need to view the changes of a single remote branch. Git provides a few different methods to clone a single branch. We can either use the Git Remote command or the Git Clone command. With the Git Remote command, we need to use a few other commands like Git Fetch and Git Checkout. The best method is to use git clone --branch [branch-name] --single-branch [remote-repo-URL] as it is the easiest method to clone a branch.