Undo a Git Pull
In a nutshell
Using the [.inline-code]git reset --hard[.inline-code] command will reset the current branch to the specified commit and undo the merge that was performed by the [.inline-code]git pull[.inline-code] command.
The above commands list potential ways to revert to a previous commit
- [.inline-code][<commit>][.inline-code] references a commit hash: [.inline-code]git reset --hard a248db2[.inline-code]
- [.inline-code][<remote branch>][.inline-code] references a branch name: [.inline-code]git reset --hard test/foo[.inline-code]
- [.inline-code][<time reference>][.inline-code] references a previous point in time: [.inline-code]git reset --hard HEAD@{"1 minute ago"}[.inline-code]
To learn how to identify local commits, read our post about viewing git commit history using [.inline-code]git reflog[.inline-code].
[#easily-recall-with-ai]Remind yourself of the syntax using AI Command Search[#easily-recall-with-ai]
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:
Entering [.inline-code]git undo pull[.inline-code] in the AI Command Search will suggest you the correct [.inline-code]git[.inline-code] command, which you can then quickly insert into your shell by doing [.inline-code]CMD+ENTER[.inline-code].
[#using-git-reset]Using git-reset to undo a git pull[#using-git-reset]
The [.inline-code]git reset[.inline-code] command is particularly useful when you want to start with a clean slate and undo the changes that were merged with a [.inline-code]git pull[.inline-code].
Effectively, [.inline-code]git pull[.inline-code] is the combination of [.inline-code]git fetch[.inline-code] and [.inline-code]git merge[.inline-code]. [.inline-code]git fetch[.inline-code] grabs the updates from the remote repository, and [.inline-code]git merge[.inline-code] merges any changes into the current branch.
To undo the merge effects of a git pull,[.inline-code]git reset[.inline-code] will revert the repository to a previous history which achieves the same goal.
Here are some examples below:
- If you know the specific commit you want to revert to [.inline-code]git reset --hard <commit-sha>[.inline-code] will bring your repository back to that specific commit.
- If [.inline-code]git pull[.inline-code] fetched only one commit, you can undo that single commit using [.inline-code]git reset --hard HEAD^[.inline-code].
- To undo N commits, [.inline-code]git reset --hard HEAD~N[.inline-code].
- If you don't remember the commit, you can also leverage a time reference - [.inline-code]git reset --hard HEAD@{"10 minutes ago"}[.inline-code].
Learn more about [.inline-code]git reset[.inline-code] in this article about undoing git commits.
[#preserve-local-changes]Preserving your local changes[#preserve-local-changes]
Using [.inline-code]git reset --hard[.inline-code] will remove all the local changes. To hold onto local changes but still undo a [.inline-code]git pull[.inline-code], you can leverage [.inline-code]git stash[.inline-code] as follows:
1. [.inline-code]git stash[.inline-code] to stash local changes
2. [.inline-code]git reset --hard HEAD^[.inline-code] to undo a git pull
3. [.inline-code]git stash apply[.inline-code] to reapply your local changes
[#abort-git-pull]Cancel or abort an ongoing [.inline-code]git pull[.inline-code][#abort-git-pull]
In some cases, you will be looking to “undo” a [.inline-code]git pull[.inline-code] that is not yet completed. This may happen when you try to [.inline-code]git pull[.inline-code], and are met with merge conflicts.
In this situation, you can use the [.inline-code]git merge --abort[.inline-code] command to abort the merge in progress. This command will bring your repository to the state it was prior to the merge.
To abort an in-flight [.inline-code]git pull[.inline-code], pressing [.inline-code]ctrl[.inline-code]+[.inline-code]c[.inline-code] in the terminal window will cancel the operation. But if the operation had already completed, you will want to refer to the steps above to revert the [.inline-code]git pull[.inline-code].
[#github]Undo a GitHub pull request[#github]
[#undo-unmerged-pr]Undo an unmerged pull request[#undo-unmerged-pr]
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-merged-pr]Undo a merged pull request[#undo-merged-pr]
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.