This manual will discuss how to undo Git pull.
How to Undo Git Pull?
Git enables users to undo changes that they made before. For this purpose, we will take an example; first, we will create and add a file in the Git repository. Then, commit changes and pull them to the Git remote repository. Lastly, undo the pull operation utilizing the command.
Now, let’s head toward instructions!
Step 1: Navigate to Git Repository
First, move to the Git repository utilizing the “CD” command:
Step 2: Create New File
Next, execute the “touch” command to create a new file in Git repository:
Step 3: Add File
Now, add the backtracked file to staging area from working area:
Step 4: Commit Changes
Save the changes in the Git repository with commit message utilizing provided command:
Step 5: Git Pull
Execute the “git pull” command to pull all commit changes to the remote repository:
Here, the default editor will open, add a comment, save changes and exit it:
As you can see, we have performed the pull action to the remote repository. Our local and remote repository branches are merged successfully:
Note: Let’s move to the next steps to undo the Git pull.
Step 6: Check Git Log
Now, check the log history of all commit changes utilizing the “git log” command with the “–lifeline” flag and “–graph” option:
It can be seen, we have made five commits to the Git repository, and the most recent commit is the “*4e4d7a8”. Now, we will copy the reference of the previous commit to the clipboard:
Step 7: Undo Pull
Next, execute the “git reset” command with the “–hard” flag:
Here, we have specified the “HEAD^” which will move the HEAD to the previous commit:
Step 8: Check Log
To verify the undo Git pull action, execute the “git log” command:
Below output indicates that, we have successfully reverted the performed action:
You can also specify the “HEAD~1” to return to the commit before HEAD:
As you can see, we have successfully reverted to the previous commit:
That’s all! We have provided the easiest way to undo Git Pull.
Conclusion
To undo Git pull, first, open the Git terminal on your system and move to the Git repository. Next, create and add a file to the redo. Then, commit changes using the “$ git commit -m <message>” command and execute the “$ git pull” command to pull them to the Git remote repository. Lastly, run the “$ git reset –hard HEAD^” command to undo the pull operation. This manual elaborated on the procedure to undo Git pull.