[英]How to make checked-out branch to become master
I ran command git checkout {previousCommit}
, and continue-ing working from there.我运行命令
git checkout {previousCommit}
,并从那里继续工作。 Reason is something messed up with HEAD
version and I wished to go back to earlier state and re-do.原因是
HEAD
版本搞砸了,我希望回到之前的状态并重新做。
So now that I created a branch previousCommit
, and done updating, I wish to take everything into master again.所以现在我创建了一个分支
previousCommit
并完成了更新,我希望再次将所有内容都带入 master 中。 While on branch previousCommit
, I made 2 commit
.在分支
previousCommit
,我进行了 2 次commit
。
And now when I ran command git checkout master
, I got below message and I'm not sure what to do现在当我运行命令
git checkout master
,我收到以下消息,但我不知道该怎么做
git checkout master
Warning: you are leaving 2 commits behind, not connected to
any of your branches:
70a95ef someCommitMessage
05ebd0f someCommitMessage
If you want to keep them by creating a new branch, this may be a good time
to do so with:
git branch <new-branch-name> 70a95ef
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
Probably I should give a clearer picture:也许我应该给出一个更清晰的图:
Basically I have 10 commits in total on master branch, lets call it commit 1
... commit 10
基本上我在 master 分支上总共有 10 个提交,我们称之为
commit 1
... commit 10
Now master is latest with commit 10
, and I executed a command git checkout commit 7
, and so git created a branch commit 7
for me, and I continuing my work from commit 7
, and made 2 commits on this branch.现在 master 是最新的
commit 10
,我执行了一个命令git checkout commit 7
,所以 git 为我创建了一个分支commit 7
,我从commit 7
继续我的工作,并在这个分支上进行了 2 次提交。
Now my objective is to have this 2 commits replace commit 8,9,10
on master, is that possible?现在我的目标是让这 2 个提交替换 master 上的
commit 8,9,10
,这可能吗?
I know I should've git reset hard commit 7
earlier, but I'm wondering anything that I can do on current state?我知道我应该早点
git reset hard commit 7
,但我想知道在当前状态下我能做些什么?
If you do git checkout {previousCommit}
, you are in a detached head state.如果您执行
git checkout {previousCommit}
,则您处于分离状态。 That means you are not on any branch at that time.这意味着您当时不在任何分支上。 So the commits you made are not in any branch and cannot be reached after you switched to the master branch.
因此,您所做的提交不在任何分支中,并且在您切换到主分支后无法访问。
Git actually tells you how to create a branch that includes those commits: Git 实际上会告诉你如何创建一个包含这些提交的分支:
git branch <new-branch-name> 70a95ef
Then you can use the new branch and continue working in that later, or merge it into master.然后你可以使用新的分支并在以后继续工作,或者将它合并到 master 中。
Note that commits that are not reachable at all (because they are not part of any branch/tag) are likely to be removed by garbage collection after some time.请注意,根本无法访问的提交(因为它们不是任何分支/标签的一部分)可能会在一段时间后被垃圾收集删除。 You can fix the issue today, but it may not be possible for this case in (for example) one month.
您今天可以解决该问题,但对于这种情况,可能无法在(例如)一个月内解决。 It's best to create a branch now, you could always delete the branch later if you decide you don't need it.
最好现在创建一个分支,如果您决定不需要它,您可以随时删除该分支。
on master, do:在主人身上,做:
git rebase 70a95ef
this will cherry-pick
for you the commits of master above 70a95ef这将为您
cherry-pick
超过 70a95ef 的 master 提交
that said, you may notice that you did not do what you think, you were not on the previousCommit branch when you created you two new commits, as git tells you.也就是说,你可能会注意到你没有按照你的想法去做,当你创建两个新提交时,你不在 previousCommit 分支上,正如 git 告诉你的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.