undo git pull of wrong branch onto master

11,488

Solution 1

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.

Solution 2

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.

Solution 3

You can do simply using the following commands

git fetch origin
git reset --hard origin/master

Solution 4

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.

Share:
11,488

Related videos on Youtube

OverTheEdge
Author by

OverTheEdge

Updated on November 04, 2022

Comments

  • OverTheEdge
    OverTheEdge over 1 year

    I have pulled from a different branch from the remote. The 2 branches are different yet the merge happened silently on my current branch. I am currently working on the "master" branch locally, and it has been updated with the changes of the remote branch - "FE_Changes".

    How do I remove the effects of the "FE_Changes" branch from my master branch ?

  • OverTheEdge
    OverTheEdge about 9 years
    wow this did it. I must say I did some crazy things on the Git Extensions GUI like checking out from origin/master, git reset (to HEAD not HEAD~1) but still this overwrote that and brought changes back. And whats more, I can now pull and get the recent updates to "master" again from remote. None of the changes done on "FE_Changes" branch is visible now in my local "master" branch. Thanks a lot Tim.