Home Linux & Systems Cybersecurity Cloud & DevOps Networks & Infrastructure SIEM & Monitoring DFIR & Threat Intel Development & Other All categories Projects About Tools

How to undo a git pull and revert to a previous commit in Git

Leer en espanol
How to undo a git pull and revert to a previous commit in Git

Table of contents

Sometimes after doing a git pull, you realize that something went wrong: maybe an important folder was accidentally deleted or unwanted changes were made. Fortunately ===

Introduction

Sometimes, after making a git pull, you realize that something went wrong: perhaps an important folder was accidentally deleted or unwanted changes were made. Fortunately, Git allows you to undo a git pull and go back to a previous commit to restore the state of the repository. In this article, I'll walk you through a practical step-by-step example of how to do it.

Prerequisites

  • Git installed on your machine.
  • Access to the repository in question.
  • Basic familiarity with Git commands.

Step 1: Check Git history with reflog

The first thing you should do is identify the commit before the git pull that you want to undo. For this, we will use the command git reflog, which shows a detailed history of recent actions in the repository, including references to commits.

Open a terminal or command line and navigate to your repository:

CODE
cd Repo

Then, run:

CODE
2f10b05 (HEAD -> master, origin/master) HEAD@{0}: pull: Fast-forward
414b196 HEAD@{1}: commit (merge): Resolved merge conflicts
6efd60a HEAD@{2}: commit: feat: setup pre-production environment
a4012f2 HEAD@{3}: commit: rokitoh
ff101f9 (tag: 0.0.0-KO, origin/release, origin/hotfix, origin/develop, origin/HEAD, develop) HEAD@{4}: checkout: moving from develop to master
ff101f9 (tag: 0.0.0-KO, origin/release, origin/hotfix, origin/develop, origin/HEAD, develop) HEAD@{5}: clone: from https://github.com/redorbita/redorbita__iac.git

In this case, the commit a4012f2 is the one we want to restore, just before the git pull what we did

Step 2: Reset the repository to the previous commit with git reset

Once you have identified the desired commit (in this example, a4012f2), you can use git reset to return to that point in history.

If you want completely return to the previous state, without preserving any changes introduced by the git pull, use the following command:

CODE
git reset --hard a4012f2

What are you doing git reset --hard?

This command restores your repository exactly to the state it was in when you committed a4012f2. The modifier --hard indicates that all uncommitted changes will be deleted, including changes to files in your workspace.

Step 3: Check the repository status

After running the command reset, it is advisable to check the status of your repository to make sure everything is as you expect.

Run the following command:

CODE
git status

You should see a message indicating that the repository is in the branch master and that there are no pending changes:

CODE
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

Step 4: Verify that the deleted folder has been restored

Browse the repository or use file browsing commands (ls, dir, etc.) to verify that the folder that had been deleted by the git pull Now it is present again.

Step 5 (Optional): Undo the pull but keep the changes local

If, instead of losing all local changes, you want to keep the modified files in your workspace but undo the git pull, you can use git reset --soft rather --hard.

CODE
git reset --soft a4012f2

With this option, the pull, but the files will still be marked as modified (uncommitted local changes will not be removed).

Conclusion

Restore your repository to a state before a git pull It is a useful action when unwanted changes are introduced to your code. With git reset, you can return to the previous state quickly and efficiently. Just remember that if you use the option --hard, any uncommitted changes will be lost, so use with caution.

Comments