简体   繁体   中英

git: I accidentally merged feature branch into master instead of develop

I'm using git and working with master/develop/feature branches.

The master only had the README file. And, unfortunately, I accidentally merged the feature branch I was working into the master instead the develop branch. And also removed the feature branch.

I know little about git and I'm confused about what to do. But I think the right thing is to merge the changes on the feature branch (already removed) into develop and revert the merge into the master. But how to do that if I already removed the feature branch?

Note that I'm assuming here that you haven't push ed the results of the merge. If you have, this still can work, but would require a "force push" which in turn impacts everyone else using the repo (see "recovering from upstream rebase" in the git rebase docs; as that's essentially the situation it would create). But if you haven't, you don't need to worry about any of that. So:

First thing is to recreate the feature branch. I'm assuming it only ever existed locally. (If it had been pushed before the merge, you might be able to recover it from the remote; but either way the approach below will work.) So

git checkout master
git checkout -b feature
git reset --hard HEAD^2

Now the feature branch is restored to the merge commit's "second parent" - which should be where it was before the merge.

Next you need to remove the merge from master .

git checkout master
git reset --hard HEAD^

And that's it; you're ready to merge the feature into the development branch.

First checkout master branch using git checkout master , and create a new branch called feature from master branch:

git checkout -b feature

Again checkout master branch, and run the following command:

git log

This should give list of commits you have on master branch. You need to scroll through commit list, to copy the commit id that you want to revert to. Suppose that id is advcf456yhn8 , then you need to write the command:

git reset --hard advcf456yhn8

This will revert all changes & revert back to actual content at specific commit id.

Now you have created a branch feature , which you can merge into develop . Simply do git checkout develop & merge feature branch by using git merge feature .

Also by running the following command, you can get your entire commit history:

git reflog

Easy way of Getting back the master to previous commit :

  1. clone you master repository
  2. If you are using any GUI on git, Use option "revert" to select the particular commit ID
  3. Once reverted, Kindly commit and push to master.

CMD:

  1. git clone master
  2. git reset --hard HEAD^
  3. git commit -m ""
  4. git push

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM