Question

Steps i performed:

I have two branches branch1 and branch2,

$git branch --Initial state
$branch1

$git checkout branch2
$git pull origin branch1 --Step1

I resolve the conflicts and did a

$git commit -m "Merge resolved"

then

$git checkout branch1
$git merge branch2
$git push origin branch1

Now i realised that while being at step1, the auto merging removed some code and the change code was pushed, now i want to go back to my initial state in order to revert any changes.looking for some immediate help?

Était-ce utile?

La solution

You can revert the merge following the official guide, however this leaves Git with the erroneous belief that the merged commits are still on the target branch.

Basically you have to :

git revert -m 1 (Commit id of the merge commit)

Autres conseils

Try using git reflog <branch> to find out where your branch was before the merge and git reset --hard <commit number> to restore the old revision.

Reflog will show you older states of the branch, so you can return it to any change set you like.

Make sure you are in correct branch when you use git reset

To change remote repository history, you can do git push -f, however this is not recommended because someone can alredy have downloaded changes, pushed by you.

The first option is the use of git revert.

git revert -m 1 [sha-commit-before-merge]

The git revert will revert the changes but will keep the history. Therefore you will not be able to continue working in the same branch since you cannot see the actual difference between the merged branch and your feature branch anymore. Use the following way to remove history as well. Do this very carefully if and only if you are the only one pushing changes to the branch at the moment.

git reset --hard [sha-commit-before-merge]
git push [origin] [branch] --force

In my case, I merged my branch (say: my-branch) with another feature branch (feature-branch) but not master. So my branch history was like this:

my-branch (before merge)

---master----m1----m2----m3---m4

After merging it with another feature-branch which had commits f1, f2 on top of master, it became like this:

my-branch (after merge)

---master----m1----m2----f1----f2----m3---m4----mergecommit

This might have happened because while working on my branch I did a merge from master after 2 commits, or one of 2 branches might not have been up to date with master. So in this case git revert -m 1 was not working as it was leaving those f1 and f2 commits in between.

The solution was simple, which will work in case of normal scenarios, where we don't have in-between commits:

git rebase -i HEAD~6

Instead of 6 use appropriate number based on how many past commits you want to change. Now Vim editor is opened, just mark undesired commits as drop and same and quit using :wq verify log:

git log --oneline 

force push

git push -f

Now the remote branch should be in previous state.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top