[英]git: I accidentally merged feature branch into master instead of develop
I'm using git and working with master/develop/feature branches. 我正在使用git并使用master / develop / feature分支。
The master only had the README file. 母版只有README文件。 And, unfortunately, I accidentally merged the feature branch I was working into the master instead the develop branch.
而且,不幸的是,我无意中将我正在使用的功能分支合并到了master分支中,而不是合并到了develop分支中。 And also removed the feature branch.
并且还删除了功能分支。
I know little about git and I'm confused about what to do. 我对git知之甚少,我对该怎么办感到困惑。 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. 请注意,我在这里假设您尚未
push
合并结果。 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). 如果有的话,这仍然可以工作,但是需要“强制推送”,这反过来会影响使用该存储库的其他所有人(请参阅
git rebase
文档中的“从上游重新存储恢复”;因为这实际上就是它会造成的情况)。 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
. 接下来,您需要从
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 master
checkout master分支,并从master分支创建一个名为feature
的新分支:
git checkout -b feature
Again checkout master branch, and run the following command: 再次检出master分支,并运行以下命令:
git log
This should give list of commits you have on master branch. 这应该列出您在master分支上的提交列表。 You need to scroll through commit list, to copy the commit id that you want to revert to.
您需要滚动查看提交列表,以复制要还原的提交ID。 Suppose that id is
advcf456yhn8
, then you need to write the command: 假设id为
advcf456yhn8
,那么您需要编写以下命令:
git reset --hard advcf456yhn8
This will revert all changes & revert back to actual content at specific commit id. 这将还原所有更改,并以特定的提交ID还原为实际内容。
Now you have created a branch feature
, which you can merge into develop
. 现在,您已经创建了一个分支
feature
,可以将其合并到develop
。 Simply do git checkout develop
& merge feature
branch by using git merge feature
. 只需使用
git merge feature
进行git checkout develop
和合并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 : 使主服务器恢复到先前提交的简单方法:
CMD: CMD:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.