假设我开始处理明确的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 !!!
答案 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
将无济于事。在这种情况下,最简单的方法是:
git branch -m feature-xy
。git pull --rebase
之类的操作来整合潜在的上游更改。git push -u feature-xy:feature-xy
(注意-u
更新您的上游分支。)git checkout master
(由于没有本地主人,git将从上游创建一个新主人。)