Git

Git Undo Pull

Git is an incredible tool that gives developers a lot of power. However, unlike other systems, git does not provide you with a reset switch if anything goes wrong.

However, git will do its best to provide unwanted and dangerous operations in a repository.

This article will discuss how we can undo a git pull operation. This can happen if the changes on the remote repository are messing up with your local changes. Or if you want to go back to a state before you did the pull operation.

What is a Git Pull?

The git pull command lets you fetch the changes made to the remote repository and merge them with your local repository.

Git will start by checking if there are any changes in the remote repository that are not available in the local repo when you run the git pull command.

If there is, git will perform a fetch to download the remote changes to the local system. Finally, git will perform a merge operation to sync the changes into the local repository.

In simple terms, the girt pull command allows you to have the latest changes made to the remote repository.

If you run the git pull command, you should see the metadata and information about the files that have been modified.

An example output is shown:

$ git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 1.45 KiB | 93.00 KiB/s, done.
From https://github.com/username/reponame
   9b076c0..b768a43  master     -> origin/master
Updating 9b076c0..b768a43
Fast-forward
 readme.md | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

We can see from the output above that only the README,.md file has been modified.

Let us assume that we wish to undo the above pull operation. How would we do that?

Git Reset

The git reset command allows us to revert to a specific point in a repo. So, for example, we can use this command to undo a git operation.

NOTE that it is good to keep in mind that running the git reset command will discard any changes that have not been committed in the specific repository. Hence, to prevent losing any changes, perform a git commit for the changes you wish to preserve.

To undo a git pull operation, start by fetching the specific point. For example, you can do this with git reflog command:

$ git reflog

The command should return the commits logs and changes made in the repo. Locate the one adjacent to the pull operation as:

In this case, we are looking to revert to the commit 9b076c0

We can now run the git reset command as:

git reset 9b076c0 –hard

You can use the HEAD statement to make your task easier if you have not made any changes after the pull operation.

$ git reset HEAD~1 –hard

NOTE that to use the HEAD statement ONLY if you have not made any changes to the repository after the pull operation.

This is because the HEAD~1 will move the repository back to one commit, whether it’s the pull operation or not.

NOTE that depending on the version of git, you can substitute the –hard flag as:

$ git reset –keep HEAD~1

The –hard option will force the uncommitted changes to be discarded.

You can learn more:

https://git-scm.com/docs/git-reset

Conclusion

In this article, we learned how to undo a pull operation. We also covered how the git pull operation works.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list