aspirinemaga
aspirinemaga

Reputation: 3947

Git - roll back all local changes after messing up

I can't find any relative answer for my problem. I have two branches at the moment:

After commited and and pushed to server all modifications (to develop branch) I wanted to update master branch with a develop branch. Accidentally I've messed up, after mergin master with develop - a master now have everything from both branches, and that's not what I want to.

How do I restore everything on my local repo ? I've tried git reset --hard HEAD but it does not seem to help.

Upvotes: 2

Views: 980

Answers (3)

Antony Hatchkins
Antony Hatchkins

Reputation: 34024

With the master branch currently checked out:

git reset --hard develop

will switch to develop branch losing all changes from master except those which have already been included in develop branch.

Upvotes: 2

LuFFy
LuFFy

Reputation: 9307

Solution 1:

You can reset your branch to the state it was in just before the merge if you find the commit it was on then.

Use git reflog, it will list all the HEADs you've had. I find that git reflog --relative-date is very useful as it shows how long ago each change happend.

Once you find that commit just do a git reset --hard <commit id> and your branch will be as it was before.


Solution 2:

you can simply git revert the merge commit like so:

git checkout BRANCH_NAME
git revert -m 1 <merge_commit_sha>

Note : It is possible that you will end up with some conflicts that you'll have to manually unmerge, just like when merging normally.

Upvotes: 4

nocodename
nocodename

Reputation: 1264

You could try to checkout specific commit:

git checkout [revision-hash] .

Pass hash of the commit from before the merge

Upvotes: 2

Related Questions