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:
Then reset the current branch to that commit using the [.inline-code]git reset[.inline-code] command as follows:
Where:
- [.inline-code]commit[.inline-code] is a commit hash.
Alternatively, you can specify the commit number using the following syntax instead:
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]:
Note that the following command is equivalent to the previous one:
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:
![](https://cdn.prod.website-files.com/64b7506ad75bbfcf43a51e90/6671e668f5572700e75cf9ac_AD_4nXcTq9exsGI9v5lnva1KlunDE7fCPLMPD6g1qpNEemONiTTNd85Iu2Fe1qvGG5C6TbhHksi__gR3AXpL9o94xmdN6iql-Kbgs2xqQH8omUbHVHp5cSyNK8GbhwmnQ7BnvGqfuP-RIbktulgtzKX0fywRdmfA.gif)
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:
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:
[#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:
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:
[#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:
Then reset your branch using the [.inline-code]git reset[.inline-code] command:
And finally, reapply these changes on top of the current branch using the [.inline-code]git stash apply[.inline-code] command:
[#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:
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:
- Go to the pull request page and click on the button that says "Close pull request"
- Go to the repository and select the "Branches".
- 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:
- Go to the pull request page and scroll to the bottom of the page
- 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.