OverTheEdge
OverTheEdge

Reputation: 759

undo git pull of wrong branch onto master

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 ?

Upvotes: 28

Views: 22978

Answers (4)

Jdslk
Jdslk

Reputation: 97

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.

Upvotes: 3

mukesh prajapati
mukesh prajapati

Reputation: 117

You can do simply using the following commands

git fetch origin
git reset --hard origin/master

Upvotes: 7

saurabh
saurabh

Reputation: 6775

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.

Upvotes: 23

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520898

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.

Upvotes: 33

Related Questions