What Does Git Pull Do?

When you’re remotely working on projects, having a solid knowledge of Git is imperative as it makes such collaboration extremely easy to execute. However, the ability to collaborate on projects brings the unique challenge of keeping every collaborator’s work in sync; this is where git pull becomes necessary. Learning to pull repositories is vital for working with Git, and every developer must understand it properly.

What is Git Pull?

git pull is one of the four remote operations within Git (along with git clonegit fetch, and git push); it is a command used to fetch and merge changes from the remote repository to the local repository. When you want to use a remote repository locally, you need to use git clone to download that repository to your machine. After you clone a repository, you need git pull to constantly update the local version with others’ changes on the remote repository.

git pull is a combination of git fetch and git merge; the command first fetches the updates to the remote repository, then merges those changes to the branches in your working directory. 

git pull isn’t a command you will need to use often on a solo project unless you have your remote repository cloned on different computers. However, when other team members have read and write access to a remote repository, you should constantly perform a git pull. There is likely another member of your team making changes to the codebase that might affect the changes you’re also trying to make.

Administrators of open-source projects use forking and pull requests to prevent the chaos resulting from multiple contributors making changes to a particular repository.

How To Use Git Pull

To practically explain how to use git pull, I’ll first be creating an empty remote repository called git-pull on GitHub. Sign up or log in to your GitHub account, click the plus icon at the top-right corner of the taskbar, and select New repository; give the new repository a name and description.

Image showing how to create a new remote repository on GitHub
Create a New Remote Repository on GitHub

I’ll be creating a directory on my computer called git-pull, where I will create a file called text1.txt. Next, I will initialize Git in the git-pull directory and commit the text1.txt file locally before pushing it to GitHub. The commands to achieve the above result are as follows:

$ mkdir git-pull
$ cd git-pull
$ touch text1.txt
$ git init
$ git add .
$ git commit -m "Initial commit"
$ git remote add origin https://github.com/<username>/git-pull.git
$ git branch -M main
$ git push -u origin main

Now that I’ve pushed the repository to GitHub, I’ll update the content of the text1.txt file remotely by selecting the file and clicking the edit (pen) icon. I will add a short paragraph to the text file on GitHub, add a commit message, and commit the changes while leaving the local file untouched. 

Since git links the local repository to the remote, you can update the local file by typing git pull within the working directory. Whatever updates are on the remote repository will immediately reflect on the local repository. If you do a git log, you’ll find out that the most recent commit came from your GitHub remote repository.

Undoing A Git Pull

There are two aspects of a git pull– a git fetch, and a git merge; by undoing a git pull, you can reverse the merge effect, but not the fetch. In other words, undoing a git pull will not change the updates made to the remote tracking branches in your local repository. However, it will reverse the merge between the corresponding remote tracking branch and the local working branch, therefore undoing the changes on the local working branches.

When working with Git, you can have only one active branch at a time; this is the HEAD branch. To undo a git pull, you need to git reset to the last commit you made before the merge that occurred with your git pull; you can find this commit by using the git reflog command. git reflog pulls up a locally stored record of every place the HEAD has pointed in the last 30 to 90 days.

When you’ve located the commit you would like to revert to, copy the SHA (the hash code that looks like “d6f4c94”). Next, you should run the command that looks like this:

git reset --hard d6f4c94

The reset will restore your current branch to its state before the git pull.

Conclusion

No team of developers working on the same project can overemphasize the importance of git pull; in fact, it’s good practice to perform a git pull daily. It can prevent conflicts and duplicate changes while ensuring that every developer on a project is working in harmony.

Read More: How To Clone A Git Repository

Leave a Comment