使用当前更改创建 Git 分支

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

Create Git branch with current changes

gitgit-branch

提问by willcodejavaforfood

I started working on my masterbranch thinking that my task would be easy. After a while I realized it would take more work and I want to do all this work in a new branch.

我开始在我的分支上工作,认为我的任务很容易。一段时间后,我意识到这需要更多的工作,我想在一个新的分支中完成所有这些工作。

How can I create a new branch and take all these changes with me without dirtying master?

如何在不弄脏master 的情况下创建一个新分支并随身携带所有这些更改?

采纳答案by VonC

If you hadn't made any commit yet, only (1: branch) and (3: checkout) would be enough.
Or, in one command: git checkout -b newBranch

如果您还没有进行任何提交,那么只有 (1: branch) 和 (3: checkout) 就足够了。
或者,在一个命令中:git checkout -b newBranch

As mentioned in the git resetman page:

git reset手册页中所述

$ git branch topic/wip     # (1)
$ git reset --hard HEAD~3  # (2)  NOTE: use $git reset --soft HEAD~3 (explanation below)
$ git checkout topic/wip   # (3)
  1. You have made some commits, but realize they were premature to be in the "master" branch. You want to continue polishing them in a topic branch, so create "topic/wip" branch off of the current HEAD.
  2. Rewind the masterbranch to get rid of those three commits.
  3. Switch to "topic/wip" branch and keep working.
  1. 您已经进行了一些提交,但意识到它们进入“ master”分支还为时过早。您想在主题分支中继续完善它们,因此topic/wip在当前HEAD.
  2. 回滚master分支以摆脱这三个提交。
  3. 切换到“ topic/wip”分支并继续工作。


Note: due to the "destructive" effect of a git reset --hardcommand (it does resets the index and working tree. Any changes to tracked files in the working tree since <commit>are discarded), I would rather go with:

注意:由于命令的“破坏性”效果git reset --hard(它确实会重置索引和工作树。工作树中跟踪文件的任何更改<commit>都将被丢弃),我宁愿选择:

$ git reset --soft HEAD~3  # (2)

This would make sure I'm not losing any private file (not added to the index).
The --softoption won't touch the index file nor the working tree at all (but resets the head to <commit>, just like all modes do).

这将确保我不会丢失任何私人文件(未添加到索引中)。
--soft选项根本不会触及索引文件或工作树(但将头部重置为<commit>,就像所有模式一样)。



With Git 2.23+, the new command git switchwould create the branch in one line (with the same kind of reset --hard, so beware of its effect):

使用Git 2.23+新命令git switch将在一行中创建分支(使用相同类型的reset --hard,因此请注意其效果):

git switch -f -c topic/wip HEAD~3

回答by EeKay

Like stated in this question: Git: Create a branch from unstagged/uncommited changes on master: stash is not necessary.

就像这个问题中所述:Git: Create a branch from unstagged/uncommited changes on master: stash is not必需。

Just use:

只需使用:

git checkout -b topic/newbranch

git checkout -b topic/newbranch

Any uncommitted work will be taken along to the new branch.

任何未提交的工作都将被带到新的分支。

If you try to push you will get the following message

如果您尝试推送,您将收到以下消息

fatal: The current branch feature/NEWBRANCH has no upstream branch. To push the current branch and set the remote as upstream, use

git push --set-upstream origin feature/feature/NEWBRANCH

致命:当前分支功能/NEWBRANCH 没有上游分支。要推送当前分支并将远程设置为上游,请使用

git push --set-upstream origin feature/feature/NEWBRANCH

Just do as suggested to create the branch remotely:

只需按照建议远程创建分支:

git push --set-upstream origin feature/feature/NEWBRANCH

git push --set-upstream origin feature/feature/NEWBRANCH

回答by AvadhP

Follow these steps:

按着这些次序:

  1. Create a new branch:

    git branch newfeature
    
  2. Checkout new branch: (this will not reset your work.)

    git checkout newfeature
    
  3. Now commit your work on this new branch:

    git commit -s
    
  1. 创建一个新分支:

    git branch newfeature
    
  2. 结帐新分支:(这不会重置您的工作。)

    git checkout newfeature
    
  3. 现在在这个新分支上提交你的工作:

    git commit -s
    

Using above steps will keep your original branch clean and you dont have to do any 'git reset --hard'.

使用上述步骤将使您的原始分支保持干净,并且您不必执行任何 'git reset --hard'。

回答by Ether

Since you haven't made any commits yet, you can save all your changes to the stash, create and switch to a new branch, then pop those changes back into your working tree:

由于您还没有进行任何提交,您可以将所有更改保存到存储中,创建并切换到一个新分支,然后将这些更改弹回您的工作树:

git stash  # save local modifications to new stash
git checkout -b topic/newbranch
git stash pop  # apply stash and remove it from the stash list

回答by Patrick Schaefer

To add new changes to a new branch and push to remote:

将新更改添加到新分支并推送到远程:

git branch branch/name
git checkout branch/name
git push origin branch/name

Often times I forget to add the origin part to push and get confused why I don't see the new branch/commit in bitbucket

很多时候我忘记添加原始部分来推送并感到困惑为什么我没有在 bitbucket 中看到新的分支/提交