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

时间:2009-08-29 14:29:01

标签: git branch master

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

4 个答案:

答案 0 :(得分:195)

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

git checkout -b new_branch
git add .

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

答案 1 :(得分:150)

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

假设您在主分公司:

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

我不确定已删除的文件,但我猜您在使用git add .

时不会包含这些文件

答案 2 :(得分:35)

为什么不使用git stash。我认为它比复制粘贴更直观。

$ git branch
  develop
* master
  feature1
  TEST
$

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

$ 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
$

转到另一个分支。

$ git checkout TEST

并申请

$ 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
#

我也喜欢git stash,因为我使用了git flow,当您想要完成功能分支同时仍在工作目录中进行更改时会抱怨。

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

答案 3 :(得分:5)

git checkout TEST
git add file1 file2
git commit
相关问题