繁体   English   中英

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

[英]How to create a new branch and move existing code there

假设我开始研究清除master分支 - no changes to commit 做一些本地更改,但意识到这些修改应该在一个单独的branch而不是master

有没有办法如何将此更改移动到单独的新branch并将master分支重新设置为状态no changes to commit

编辑

接下来接受git分支的答案- 如何让当前的master成为分支,然后将master恢复到以前的版本? ......按照步骤操作时,我的主人仍会修改文件。查看最后的评论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 !!!

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

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

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

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

git stash
git stash branch testchanges

如果你没有提交任何东西,那么使用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将从上游创建一个新的。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM