简体   繁体   English

如何使签出的分支成为主分支

[英]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.

相关问题 git从已签出的存储到分支 - git from checked-out stash to branch 为什么Git允许推送到添加的工作树中的已签出分支? 我应该如何恢复? - Why does Git allow pushing to a checked-out branch in an added worktree? How shall I recover? 检查是否将提取分支并允许将其检出 - Check whether a branch will be fetched and allowed to be checked-out 存储在refs / head之外的ref是否可以被视为分支并签出并作为普通分支进行处理? - Can a ref stored outside refs/heads be considered a branch and checked-out and worked-on as a normal branch? 从检出的 Git 分支中找到“父”分支的可靠方法 - Reliable way of finding 'parent' branch from a checked-out Git branch TeamCity:如何将未版本控制的配置文件放入已签出的存储库中? - TeamCity: How to put unversioned configuration files in a checked-out repository? 显示所有最近的 git 提交,无论它们是否属于本地签出分支 - Show all recent git commits, whether they belong to a locally checked-out branch or not 文档在哪里解释了为什么“git log” output 根据签出的分支而有所不同? - Where do docs explain why "git log" output differs based on checked-out branch? ! [远程拒绝] master - > master(当前已检出分支)git中出错 - ! [remote rejected] master -> master (branch is currently checked out) Error in git 使用git进行Web部署-如何仅在检出的实时站点中拥有大型二进制文件,而不是git repo中的blob - Web deployment with git - how to have large binaries only in checked-out live site, and not as blobs in git repo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM