How to undo a git pull command
Alexandre Calaça
Posted on November 8, 2024
Situation
Sometimes in Git
, we may want to go back to a previous state in our repository. This could be due to an unintended change, a deleted branch, or simply the need to return to an earlier version.
In my situation, I wanted to revert a git pull
command.
Git’s reflog
command is really valuable for this, as it allows us to see the recent history of the repository, including actions like checkout, reset, and other state changes that wouldn’t normally appear in a git log
command.
Let's move on.
View the history of changes
The first step is to view the reflog
. This shows a history of all the changes and commits you’ve made, even those that might not be reflected in the regular git log
.
git reflog
Expected output
Each entry in the reflog
output displays:
- The commit hash, which uniquely identifies each commit.
- The type of action taken (e.g., checkout, commit, reset).
- A brief description of the action.
The reflog
provides an easy way to find the exact commit we want to revert to or reset.
In this example, we’ll identify the commit hash for our target commit.
Identify the commit
After running git reflog, look for the commit hash you want to reset to.
git reflog
Expected output
I want to remove the most recent commit, so, I need to identify one commit before that.
Reset the head
To move the state of your repository back to this specific commit, you’ll use git reset
with the --hard
option.
The --hard
option tells Git to reset the staging area and working directory to match the specified commit.
Pattern
git reset --hard <commit-hash>
IN my situation, the code would look something like this
git reset --hard 5fp725df74
Expected output
After running this command, Git should confirm the reset, and your repository will be in the exact state it was at the specified commit:
Confirm the reset
git reflog
Expected output
The reset will be visible, showing that your HEAD
has been moved to the selected commit
Check your log
To verify that your repository has been reset correctly, use the git log
command. This command shows the commit history, which should now reflect the state of your repository after the reset.
git log
The git log
output should now show the recent history up to the commit you reset to, with any subsequent commits excluded:
Done
Posted on November 8, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.