git 从当前工作树创建分支并重置为 HEAD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2089882/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:02:22  来源:igfitidea点击:

Create branch from current working tree and reset to HEAD

gitbranch

提问by Nils Riedemann

I currently work on a feature that turns out to be bigger than expected, so it's propably the best to create a branch to work on it. So I need to create a new branch from my current working directory and reset the master branch to the current HEAD so that some fixes to the production environment can be done.

我目前正在研究一个比预期更大的功能,因此最好创建一个分支来处理它。所以我需要从我当前的工作目录创建一个新分支,并将 master 分支重置为当前的 HEAD,以便可以对生产环境进行一些修复。

Somehow this sounds like an easy task, yet I can't quite figure it out. Possibly due to my lack of sleep.

不知何故,这听起来像是一项简单的任务,但我无法弄清楚。可能是因为我睡眠不足。

采纳答案by Igor Zevaka

So, create a working branch:

因此,创建一个工作分支:

git checkout -b working_branch

either commit or stash your changes

提交或隐藏您的更改

git add <files>
git commit -m "message"

OR

或者

git stash

Go back to master

回到主人身边

git checkout master
git reset HEAD

回答by CB Bailey

If you haven't yet made a commit then you don't need to move master, it's already at the current HEAD. You can just checkout a new branch with checkout -b, it doesn't need your working tree to be clean.

如果你还没有提交,那么你不需要移动 master,它已经在当前HEAD. 您可以使用 签出一个新分支checkout -b,它不需要您的工作树是干净的。

E.g.

例如

git checkout -b newtopic

You are now on newtopicand can commit your working tree changes here. masterdoesn't need to move.

您现在newtopic可以在这里提交您的工作树更改。master不需要移动。