Computer >> Computer tutorials >  >> Programming >> Programming

Git: Pull All Branches

Git lets you maintain multiple separate lines of development for a project. These lines of development are called branches. You can retrieve the latest version of a branch from a remote repository independently or you can retrieve the latest version of all branches at once.

In this guide, we talk about how to use the git fetch –all and git pull –all command to retrieve changes from a remote repository.

What is Branching?

Let’s say we are working on a blog website. We’re going to add a feature to the blog that lets users comment. We don’t want this feature to be part of the main version of our project because we are still working on it.

We can use a Git branch for this. We can create a branch called “comments” to store all the code for our commenting feature. This will let us work on our commenting feature without changing the main version of our codebase that is deployed on a website.

Branches can be stored locally or remotely. If you are working on a local version of a project, a branch will be local. Remote branches are stored with the main version of a project.

Git: Fetch All Branches

We’re working on a project called blog-site. This project contains two branches: origin master and origin dev.

The dev branch contains all the experimental features we are working with. We think that another collaborator has pushed changes to both branches. We want to make sure and retrieve the metadata for any changes if they have been made.

We can do this using the fetch command. The fetch command tells Git to retrieve metadata from a remote branch on the latest updates. The fetch command does not update the files stored in a local version of a repository.

81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.

The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.

To track all remote branches and fetch the metadata for those branches, we can use the git fetch command with the –all flag:

git fetch --all

This command returns:

Fetching origin
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/career-karma-tutorials/blog-site
   3fcea0c..da74d68  dev    	-> origin/dev

The fetch command has fetched all of the changes we’ve made to our remote repository. The fetch command knows our remote dev branch contains changes we do not have on our local machine. We have just retrieved the metadata for those commits.

We can retrieve the metadata for an individual branch using the git fetch origin <branch-name> command.

Git: Pull All Branches

What if you want to update your local working copy as well as retrieve metadata? That’s where the git pull command comes in handy.

We now know that changes have been made to our repository. We are happy with merging these changes with our local repository. To download the changes to our local machine, we need to use the git pull command:

git pull --all

We’ve used the –all flag to indicate that we want to retrieve changes from every branch. Our command returns:

Fetching origin
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/career-karma-tutorials/blog-site
   3fcea0c..da74d68  dev    	-> origin/dev
Updating 3fcea0c..da74d68
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)

The git pull command first runs a git fetch command to check for changes. The fetch operation returns the metadata for our commits. Then, the git pull command retrieves all the changes we have made to our remote repository and changes our local files.

We can see the README.md file was changed on our remote repository. Now that we’ve run a pull operation, we have the change on our local machine.

To retrieve the code from one branch, we could use the git pull origin <branch-name> command.

Conclusion

The git fetch –all command retrieves metadata on each change made to all the branches in a repository. The git pull –all command downloads all of the changes made across all branches to your local machine.
Now you have the knowledge you need to pull all branches from Git like a pro!