[英]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
将无济于事。 在这种情况下,最简单的方法是:
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.