如何创建新的分支并将现有代码移至该分支

6
假设我开始在干净的 master 分支上工作,没有要提交的更改。我做了一些本地更改,但意识到这些修改应该在一个单独的 branch 中而不是 master 中。 有没有办法将这些更改“移动”到一个新的分离的branch中,并将master分支重新设为“没有要提交的更改”状态? 编辑:根据git branching - how to make current master a branch and then revert master back to previous version?中所接受的答案,当遵循这些步骤时,我的主分支仍将具有修改后的文件。请参见最后评论7。 我是否漏掉了什么?
$ git branch                                   # 1. starting on master                                     
# On branch master
  nothing to commit, working directory clean


# On branch master                             # 2.modifying a file 
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")



$ git stash                                    # 3. stashing changes
Saved working directory and index state WIP on master: 393bfad initial commit
HEAD is now at 393bfad initial commit
$ git status
# On branch master
nothing to commit, working directory clean


$ git checkout -b experiment                   # 4. creating new branch experiment 
Switched to a new branch 'experiment'


$ git stash pop                                # 5. pop staged changes in exper.
# On branch experiment
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (16b6871d43f367edd03f59558eca4163cd1a2b2f)


$ git checkout master                           #6. going back to master
M   test.txt
Switched to branch 'master'
git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   test.txt                #7. in master test.txt is still modified !!!

4个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
6

在执行git stash pop命令后,你需要提交到新的分支。在切换到master之前先进行提交。

$ git stash pop
$ git commit -m "Committing changes to branch experiment"
$ git checkout master

考虑以下序列。从一开始(即,您在主分支上,工作目录中有本地未暂存更改),您可以执行以下操作:

$ git checkout -b experiment
$ git add test.txt
$ git commit -m 'Commit message'
$ git checkout master
使用stash/pop的好处是它会为您执行“add”操作,您不需要指定更改的文件。但是您仍然需要在新分支上提交。

3

使用 git stash 和 git stash pop 在新分支上。


0

我没有测试过这个(而且我不是一个应该被盲目信任的git大师),但在阅读了关于存储(http://git-scm.com/book/en/Git-Tools-Stashing)的文章后,似乎以下命令可能是最好的。

git stash
git stash branch testchanges

-1

如果您没有提交任何内容,使用git stash是没有问题的。如果您已经提交了一些更改(而且您真的应该这样做),git stash将无法帮助您。在这种情况下,最简单的方法是:

  • 您切换到主分支。
  • 您处理代码并进行提交。(永远不要有未提交的更改!)
  • 您意识到,您想要一个基于主分支的新分支。
  • 您只需重命名您的分支git branch -m feature-xy
  • 由于此分支仍跟踪远程主分支,因此您可以执行诸如git pull --rebase之类的操作,以集成潜在的上游更改。
  • 完成后,请推送它:git push -u feature-xy:feature-xy(请注意-u以更新上游分支。)
  • 要获取本地干净的主分支,只需检出它:git checkout master(由于没有本地主分支,git将从上游创建一个新的主分支。)

这是一个过于复杂/令人困惑的回答——唯一必要的步骤是创建一个新分支,并提交到它。事实上,重命名主分支(什么?)应该被评为-1分。 - AD7six
如果您已经提交了一些内容,那么重命名该分支比重置指针要容易得多。这样想:您正在开发一个功能,因此提交属于该功能。因此,按照这种方式命名您的分支是很自然的。 - michas
即便是针对复杂性的情况:git branch -mgit stash;git checkout -b;git stash pop 的结果是相同的。 - michas
你所回答的是一个不同的问题。相似的命令包括git checkout -b foo; git commit -v whatever。当你重命名一个分支时,实际上重命名了整个分支配置;如果有远程分支的话,新的分支将推送到master;任何期望存在 master 的 Git 操作在重新创建之前都将失败;如果0个或2个以上的远程主机有 master 分支,则git checkout master会失败;因为 master 将 === origin/master,所以未推送的提交将会“丢失”;master 的任何配置更改都将丢失。简而言之:造成问题的余地很大。 - AD7six

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,