在 git 中切换分支名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch branch names in git
提问by Daniel Benamy
There may be more than one way to ask this question, so here's a desciption of the problem. I was working on master and committed some stuff and then decided I wanted to put that work on hold. I backed up a few commits and then branched from before I started my crap work. Practically this works fine, I just now have a different branch as my main development branch. I'm wondering how I could change things around so I'm working on master again but it doesn't have my junk work and said work is on a different branch.
提出这个问题的方法可能不止一种,所以这里是对问题的描述。我在 master 上工作并提交了一些东西,然后决定我想搁置这项工作。我备份了一些提交,然后在开始我的垃圾工作之前分支。实际上这很好用,我现在有一个不同的分支作为我的主要开发分支。我想知道如何改变周围的事情,所以我再次在 master 上工作,但它没有我的垃圾工作,并说工作在不同的分支上。
Some ways this could be asked/solved: How do I rename my master branch to something else and then rename something else to master? How do I back up master and then cause all commits I've backed up past to be on a different branch?
可以问/解决这个问题的一些方法:如何将我的主分支重命名为其他内容,然后将其他内容重命名为 master?我如何备份 master 然后导致我过去备份的所有提交都在不同的分支上?
Thanks for all the (quick) answers! They're all good.
感谢所有(快速)答案!他们都很好。
回答by Greg Hewgill
In addition to the other comments, you may find the -m (move) switch to git-branch helpful. You could rename your old master to something else, then rename your new branch to master:
除了其他评论之外,您可能会发现 -m(移动)切换到 git-branch 很有帮助。您可以将旧的 master 重命名为其他名称,然后将新分支重命名为 master:
git branch -m master crap_work
git branch -m previous_master master
回答by Brian Riehman
I think you should consider a different development strategy to prevent issues like this. One that seems to work best for me is to never do development directly on my master branch. Regardless of the changes I'm making, I always create a new branch for new code:
我认为您应该考虑采用不同的开发策略来防止出现此类问题。似乎最适合我的一种方法是永远不要直接在我的主分支上进行开发。不管我做了什么更改,我总是为新代码创建一个新分支:
git checkout -b topic/topic_name master
From there, I can push out the changes to public repositories:
从那里,我可以将更改推送到公共存储库:
git push pu topic/topic_name
or eventually just merge it back in with my master branch:
或者最终将它合并回我的主分支:
git checkout master && git merge topic/topic_name
If you truly need to go back to an older point in time and set that as your master, you can rename the current branch to something else and then check out an older version to be your master:
如果你真的需要回到一个较旧的时间点并将其设置为你的主节点,你可以将当前分支重命名为其他内容,然后检出一个旧版本作为你的主节点:
git branch -m master junk git co -b master old_sha1_value
回答by Ted Percival
Start on master
, create a branch called in-progress
, then reset master
to an earlier commit.
从 开始master
,创建一个名为 的分支in-progress
,然后重置master
为较早的提交。
$ git branch in-progress
$ git reset --hard HEAD^
回答by olliej
This is relatively easy:
这相对容易:
git checkout -b fake_master master # fake_master now points to the same commit as master
git branch -D master # get rid of incorrect master
git checkout -b master real_master # master now points to your actual master
git checkout master # optional -- switch on to your master branch
回答by Penghe Geng
This will set your master to any point in one step:
这会将您的主人设置为一步中的任何一点:
git checkout -B master new_point