如何创建新分支并在其中移动现有代码

时间:2013-10-13 08:13:23

标签: git git-branch

假设我开始处理明确的master分支 - no changes to commit。 做一些本地更改,但意识到这些修改应该在单独的branch而不是master中。

是否有办法移动此更改以分隔新的branch并将master分支重新设置为状态no changes to commit

修改

按照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 个答案:

答案 0 :(得分:5)

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

存储/弹出的优点是它为您执行“添加”,您无需指定更改的文件。但是你仍然需要在新分支上提交。

答案 1 :(得分:3)

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

答案 2 :(得分:0)

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

git stash
git stash branch testchanges

答案 3 :(得分:-1)

如果你没有提交任何内容,那么使用git stash是没有问题的。如果你已经做了一些提交(你真的应该),git stash将无济于事。在这种情况下,最简单的方法是:

  • 你结账大师。
  • 您处理代码并执行提交。 (从不进行未经修改的更改!)
  • 你意识到,你想要一个基于master的新分支。
  • 您只需重命名您的分支git branch -m feature-xy
  • 由于此分支仍然跟踪远程主控,您可以执行git pull --rebase之类的操作来整合潜在的上游更改。
  • 完成后推送:git push -u feature-xy:feature-xy(注意-u更新您的上游分支。)
  • 要获得一个本地干净的主人,只需检查一下:git checkout master(由于没有本地主人,git将从上游创建一个新主人。)