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.
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:
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.