Git

How to Undo Git Pull

Git is the most widely used independent version control system used to track project files. In Git, we can perform multiple operations, such as creating or updating files and new branches in the repository. To save all changes to the remote redo made in the local repository, you can commit and pull them. You can also revert or undo the changes by utilizing the “$ git reset –hard HEAD^” command.

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:

$ CD "C:\Users\hazmat\Git\Linux_1\Linux-redo"

Step 2: Create New File
Next, execute the “touch” command to create a new file in Git repository:

$ touch file2

Step 3: Add File
Now, add the backtracked file to staging area from working area:

$ git add file2

Step 4: Commit Changes
Save the changes in the Git repository with commit message utilizing provided command:

$ git commit -m "file2 added"

Step 5: Git Pull
Execute the “git pull” command to pull all commit changes to the remote repository:

$ git pull

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:

$ git log --lifeline --graph

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:

$ git reset --hard HEAD^

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:

$ git log --lifeline --graph

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:

$ git reset --hard HEAD~1

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.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.