Terminus
Undo A Git Pull

Undo A Git Pull

The short answer

In Git, to discard all the changes made to your local repository by a pull, including the working directory and the staging area, you need to first identify the last commit hash before the pull using the [.inline-code]git reflog[.inline-code] command:

$ git reflog

Then reset the current branch to that commit using the [.inline-code]git reset[.inline-code] command as follows:

$ git reset --hard <commit>

Where:

  • [.inline-code]commit[.inline-code] is a commit hash.

Alternatively, you can specify the commit number using the following syntax instead:

$ git reset --hard HEAD@{<number>}

Where:

  • [.inline-code]number[.inline-code] is a commit number.

For example, the following command will reset the current branch to the commit identified by the hash [.inline-code]b4173b7[.inline-code]:

$ git reflog
3a5ccb8 HEAD@{1}: pull: Fast-forward
b4173b7 HEAD@{2}: commit: update home page content
d38d82f HEAD@{3}: commit: add login feature
$ git reset --hard b4173b7

Note that the following command is equivalent to the previous one:

$ git reset --hard HEAD@{2}

You can learn more about identifying local commits by reading our other article on how to navigate the commit history in Git.

[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code]git undo pull[.inline-code] in the AI Command Suggestions will prompt a [.inline-code]git[.inline-code] command that can then be quickly inserted in your shell by doing CMD+ENTER .

Partially undoing a pull

[#discard-a-number-of-commits] Discarding a specific number of commits [#discard-a-number-of-commits]

To only discard a specific amount of commits introduced by a pull rather than all of them, you can use the [.inline-code]git reset[.inline-code] command as follows:

$ git reset --hard HEAD~N

Where:

  • [.inline-code]N[.inline-code] represents the number of commits before the [.inline-code]HEAD[.inline-code] pointer.

For example, this command will discard the last 3 commits:

$ git reset --hard HEAD~3

[#discard-commits-based-on-time] Discarding commits based on a time reference [#discard-commits-based-on-time]

To discard all the commits prior to a relative time reference, you can use the [.inline-code]git reset[.inline-code] command as follows:

$ git reset --hard HEAD@{<time>}

Where:

  • [.inline-code]time[.inline-code] is a dot-separated time reference.

For example, the following command will reset the branch to the state is was in 5 minutes ago:

$ git reset --hard HEAD@{5.minutes.ago}

[#preserve-local-changes] Preserving local changes [#preserve-local-changes]

To preserve the changes made to the working directory and the staging area before performing a reset, you can first temporarily stash them using the [.inline-code]git stash[.inline-code] command:

$ git stash

Then reset your branch using the [.inline-code]git reset[.inline-code] command:

$ git reset --hard <commit>

And finally, reapply these changes on top of the current branch using the [.inline-code]git stash apply[.inline-code] command:

$ git stash apply

[#abort-an-ongoing-pull] Aborting an ongoing pull [#abort-an-ongoing-pull]

It may sometimes happen that a pull creates multiple merge conflicts.

Rather than trying to manually fix these conflicts, you can abort the pull and reset your branch to its previous state using the following [.inline-code]git merge[.inline-code] command:

$ git merge --abort

Alternatively, to abort an in-flight pull, you can use the [.inline-code]CTRL + C[.inline-code] key combination.

[#undo-an-unmerged-pull-request] Undoing a pull request on GitHub [#undo-an-unmerged-pull-request]

Undoing an unmerged pull request

To undo or close a pull request that has not been merged:

  1. Go to the pull request page and click on the button that says "Close pull request"
  2. Go to the repository and select the "Branches".
  3. Once you've located your branch, click on the "Delete branch" button next to your branch name.

[#undo-a-merged-pull-request] Undoing a merged pull request [#undo-a-merged-pull-request]

To undo a merged pull request and revert your changes:

  1. Go to the pull request page and scroll to the bottom of the page
  2. Click on the "Revert" button

You can learn more about pull requests by reviewing GitHub’s official documentation pages on closing a pull request and reverting a merged pull request