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:
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.
9b076c0..b768a43 master -> origin/master
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?
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:
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:
You can use the HEAD statement to make your task easier if you have not made any changes after the pull operation.
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:
The –hard option will force the uncommitted changes to be discarded.
You can learn more:
In this article, we learned how to undo a pull operation. We also covered how the git pull operation works.