6

假设我开始在明确的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 !!!
4

4 回答 4

6

在 you 之后git stash pop,您需要提交到新分支。只需在签出之前提交master

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

考虑以下顺序。从一开始(即,您在工作目录中具有本地未暂存更改的分支 master 中),您可以这样做:

$ git checkout -b experiment
$ git add test.txt
$ git commit -m 'Commit message'
$ git checkout master

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

于 2013-10-13T12:21:35.560 回答
3

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

于 2013-10-13T08:17:08.707 回答
0

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

git stash
git stash branch testchanges
于 2014-02-02T16:13:32.493 回答
-1

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

  • 你收银员。
  • 您处理代码并进行提交。(永远不要有未提交的更改!)
  • 你意识到,你想要一个基于 master 的新分支。
  • 您只需重命名您的分支git branch -m feature-xy
  • 由于这个分支仍然跟踪远程主控,你可以做一些事情,比如git pull --rebase整合潜在的上游变化。
  • 一旦你完成推送它:(git push -u feature-xy:feature-xy注意-u更新你的上游分支。)
  • 要获得本地干净的 master,只需检查一下:(git checkout master由于不再有本地 master,git 将从上游创建一个新的 master。)
于 2013-10-13T08:26:55.297 回答