通过 Git 将 Master 上未提交的更改放到新分支

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

Putting uncommitted changes at Master to a new branch by Git

gitbranchmaster

提问by Léo Léopold Hertz ??

How can you put uncommitted changes to a branch TEST when I am at the branch master?

当我在分支时,如何将未提交的更改放入分支 TEST master

采纳答案by Samuel Carrijo

You can just checkout to the test branch and then commit. You don't lose your uncommited changes when moving to another branch.

您可以只检出到测试分支,然后提交。移动到另一个分支时,您不会丢失未提交的更改。

Supposing you are at the master branch:

假设你在 master 分支:

git checkout test
git add .
git add deletedFile1
git add deletedFile2
...
git commit -m "My Custom Message"

I am not really sure about the deleted files, but I guess they aren't included when you use git add .

我不太确定删除的文件,但我想当您使用时它们不包括在内 git add .

回答by

Also you can create a new branch and switch to it by doing:

您也可以创建一个新分支并通过执行以下操作切换到它:

git checkout -b new_branch
git add .

I use this all the time because I always forget to start a new branch before I start editing code.

我一直在使用它,因为我总是忘记在开始编辑代码之前启动一个新分支。

回答by HeyWatchThis

Why not just use git stash. I think it's more intuitive like a copy-and-paste.

为什么不直接使用 git stash。我认为它更直观,就像复制粘贴一样。

$ git branch
  develop
* master
  feature1
  TEST
$

You have some files in your current branch that you want to move.

您当前的分支中有一些要移动的文件。

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#      modified:   awesome.py
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#      modified:   linez.py
#
$
$ git stash
Saved working directory and index state \
  "WIP on master: 934beef added the index file"
HEAD is now at 934beef added the index file
(To restore them type "git stash apply")
$
$ git status
# On branch master
nothing to commit (working directory clean)
$
$
$ git stash list
stash@{0}: WIP on master: 934beef ...great changes
$

Move to the other branch.

移动到另一个分支。

$ git checkout TEST

And apply

并申请

$ git stash apply
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#      modified:   awesome.py
#      modified:   linez.py
#

I also like git stashbecause I use git flow, which complains when you want to finisha feature branch whilst having changes still in your working directory.

我也喜欢git stash因为我使用git flow,当你想完成一个功能分支同时在你的工作目录中仍然有更改时它会抱怨。

Just like @Mike Bethany, this happens to me all the time because I work on a new problem while forgetting I am still on another branch. So you can stashyour work, git flow feature finish..., and git stash applyto new git flow feature start ...branch.

就像@Mike Bethany 一样,这种情况一直发生在我身上,因为我在处理一个新问题的同时忘记了我还在另一个分支上。因此,您可以您的工作git flow feature finish...、 和存储git stash apply到新git flow feature start ...分支。

回答by Bombe

git checkout TEST
git add file1 file2
git commit