Skip to main content
X

Explore your training options in 10 minutes

Undo Git Pull: A Guide

James Gallagher - January 04, 2021


The git pull command lets you retrieve changes made to a project from a remote repository and download those changes to your local machine. This operation can be undone using the git reset command. The reset command reverts a repository to a previous point in its history.

This guide discusses how to use the git reset command to undo a git pull operation. It walks through an example to help you learn how to use this command.

Git Pull: A Breakdown

The git pull command brings your local repository up to date with its remote counterpart.

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses










By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

When you run the git pull command, Git will check if any changes have been made to a remote repository by running the git fetch command. Then, if changes have been made, the fetch command will retrieve the metadata for those changes.

Next, the git pull command runs git merge . This process merges any changes discovered by the git fetch command onto your local machine. This means when you run git pull your local version of a repository will be changed to match the remote repository.

Undo Git Pull

There is no command to explicitly undo the git pull command. The alternative is to use git reset, which reverts a repository back to a previous commit.

We’re working on a project called ck-git. A collaborator has just pushed a commit to the remote version of the project that is stored on GitHub. We want to retrieve these changes.

To retrieve these changes, we’re going to use the git pull command:

git pull

This command returns a message informing us it has pulled the metadata and downloaded the changes from the remote repository to our local machine:

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/ck-git
   77e7fc0..a8336fa  master 	-> origin/master
Updating 77e7fc0..a8336fa
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)

The only file that has changed on the master branch is README.md. This file has one additional line of text that did not exist in the previous commit.

Now, let’s say we want to undo this operation. We are not ready to accept this change into our local repository. To undo the operation, we need to run git reset.

Before you run the git reset command, you should know that this command will remove any uncommitted changes you have made to a repository. Make sure you have committed any changes you want to save before you run the reset command.

Let’s run the git reset command to move back to the previous state of the repository at the last commit in Git:

git reset 77e7fc0 --hard

This command reverts our repository to its state on the last commit:

Venus, a software engineer at Rockbot

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

Unstaged changes after reset:
M    README.md

The –hard flag tells Git we want to change our working directory with the contents of the code at a particular commit.

Alternatively, we could have used the HEAD statement to specify which commit we want to revert back to :

git reset HEAD~1 --hard

This statement moves our repository back one commit. We can change the number 1 to move back to further commits.

Our README.md file is now in the state it was in during the last commit. If you made any changes to the file before you run the git pull command that were not committed, those changes will be inaccessible. This is because git only saves changes that have been committed.

Conclusion

You can use the git reset command to undo a git pull operation. The git reset command resets your repository to a particular point in its history. If you made changes to files before running git pull that you did not commit, those changes will be gone.

Before running the git reset command, make sure you have committed any changes. The git reset command will cause you to lose any uncommitted changes.

Now you have the knowledge you need to undo the git pull command like a pro!

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

James Gallagher

About the author: James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.

Skip to main content