undo git pull of wrong branch onto master

You can do simply using the following commands

git fetch origin
git reset --hard origin/master

In addition to Tim's answer: If you want to reset to a specific commit:

git reflog

will show you ids of all recent commits

enter image description here

Then you can perform:

git reset --hard <specific id>

to reset to that specific commit.


git reset --hard HEAD~1

This will nuke the most recent commit on your local branch. Assuming your pull strategy is merge, then there should only be one rogue commit on your local master branch. You mentioned that "the merge happened silently," so this should work in your case. Once you have fixed the master branch, you may pull again, this time making sure you pull from the correct remote branch.


To expand on the accepted answer;

git reset --hard HEAD~1

  1. git reset

Git reset is a powerful command that is used to undo local changes to the state of a Git repo. Git reset operates on "The Three Trees of Git". These trees are the Commit History ( HEAD ), the Staging Index, and the Working Directory.

  1. --hard

This is the most direct, DANGEROUS, and frequently used option. When passed, this command causes the commit history ref pointers to be updated to the specified commit. Then, the Staging Index and Working Directory are reset to match that of the specified commit. Any previously pending changes to the Staging Index and the Working Directory gets reset to match the state of the Commit Tree. This means any pending work that was hanging out in the Staging Index and Working Directory will be lost.

  1. HEAD~1 or HEAD~2 or HEAD~3 etc...

Use '~' to go back a number of commits. HEAD~1 moves the current branch backward by one commit, effectively removing the 1 snapshot that was just created from the project history. Remember that this kind of reset should only be used on unpublished commits. Never perform the above operation if you’ve already pushed your commits to a shared repository.