git Git从当前签出的主创建分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1453129/
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
Git create branch from current checked out master?
提问by corydoras
There is a git controlled folder on a server where the main branch is checked out and a whole pile of files have been modified and not committed. Is there a way for me to commit the changes to a separate branch so I can go back to a clean version?
服务器上有一个 git 控制的文件夹,其中检出主分支,整堆文件已被修改但未提交。有没有办法让我将更改提交到单独的分支,以便我可以回到干净的版本?
ie I want to effecitvely undo all this persons changes but store them in another chance so if that person wants their changes they can switch to that branch.
即我想有效地撤消所有这些人的更改,但将它们存储在另一个机会中,这样如果那个人想要他们的更改,他们就可以切换到那个分支。
(Yes I know this is not how git is designed to work but that is my situation!) Any ideas very much appreciated.
(是的,我知道这不是 git 的设计方式,但这就是我的情况!)任何想法都非常感谢。
回答by CB Bailey
First of all moving to a different branch based in the current HEAD is performed like this:
首先移动到基于当前 HEAD 的不同分支是这样执行的:
git checkout -b newbranch
Commit all the changes (assuming no newly added files, otherwise git add
them):
提交所有更改(假设没有新添加的文件,否则为git add
它们):
git commit -a
Go back to the master branch:
回到主分支:
git checkout master
The previously uncommitted changes will all be on the newbranch branch, and master will still be at the state it was without those changes.
之前未提交的更改都将在 newbranch 分支上,而 master 仍将处于没有这些更改的状态。
回答by kenorb
This method is useful:
这个方法很有用:
git checkout -B <new_branch> <start point>
Where:
在哪里:
<new_branch>
is your new branch (e.g.my_branch
)<start point>
is your starting branch (master
in your case)-B
creates new branch starting from<start point>
, if it already exists, then reset it to (it won't fail as-b
when branch already exists)- sometimes
-m
can useful to specify when switching branches, this will perform a three-way merge between the current branch, your working tree contents (useful for scripting).
<new_branch>
是你的新分支吗(例如my_branch
)<start point>
是你的起始分支(master
在你的情况下)-B
从 开始创建新分支<start point>
,如果它已经存在,则将其重置为(-b
当分支已经存在时它不会失败)- 有时
-m
在切换分支时指定很有用,这将在当前分支和工作树内容之间执行三向合并(对脚本很有用)。
See: man git-checkout
for more details.
请参阅:man git-checkout
了解更多详情。
回答by Michael Krelin - hacker
You can always stash your changes.
您可以随时隐藏您的更改。
git stash
git checkout -b bravenewmaster
git stash apply
Also keep in mind, that if you commit to the "wrong" branch you can always move that branch back, because branch is nothing but a pointer to a commit.
还要记住,如果你提交到“错误”的分支,你总是可以将该分支移回,因为分支只是一个指向提交的指针。