切换到另一个分支而不提交

时间:2014-02-02 20:55:30

标签: git

我正在开发一个功能分支并且尚未完成工作 - 现在我需要更改为另一个分支来修复某些内容

例如

feat1 - 6+ files changed

当我签到feat2分支时,在feat1中的git add .之后,git似乎会继续进行已暂存但尚未提交的文件更改。

如果我在feat1中提交了这些文件更改,则检查到feat2将不会带来这些更改

如何在不提交文件更改的情况下切换分支?

4 个答案:

答案 0 :(得分:8)

Stash他们:

$ git stash save -u "Some logical description of the changes"
$ git stash list
stash@{0}: Some logical description of the changes
$ git checkout other-branch

完成后,您可以使用git stash apply在存储中应用更改并保留存档,或git stash pop应用更改并删除存储,只要没有冲突。

如果您一次最终获得多个藏匿处,则可以使用apply字符串以任意顺序popstash@这些字符串,例如git stash apply stash@{6}

答案 1 :(得分:4)

大多数人推荐git stash

我更喜欢做git commit。您可以随时git commit --amend。请确保不要推送该特定提交。如果它有帮助 - 对我来说,它确实 - 只是为“功能正在进行中”制作一个分支。例如:

$ git checkout zorg
Branch zorg set up to track remote branch zorg from origin.
Switched to a new branch 'zorg'
... work ...

此时,我意识到我需要保存工作,所以要做其他事情:

$ git checkout -b zorg-stones-1
Switched to a new branch 'zorg-stones-1'
$ git commit

现在一切都很好地保存在一个本地分支上,我以一种帮助我记住我在做什么的方式命名,当我稍后回来时。

通常,我稍后回来发现origin/zorg已更新,所以:

$ git fetch
[shows that origin/zorg is updated]
$ git checkout zorg && git merge --ff-only origin/zorg
[now local branch zorg is updated too; --ff-only is just for paranoia]
$ git checkout zorg-stones-1
$ git rebase zorg
[rebase messages here]

如果rebase不顺利(或者我需要重做工作),我使用git rebase --abort(或者只是跳过rebase尝试),然后根据更新后的{zorg-stones-2分支启动{1}}。当我很擅长的时候,我经常提交并且不需要做很多zorg来修复它,然后再做git rebase -i zorg来引入最终版本,准备好git checkout zorg; git merge zorg-stones-N或其他什么。

我经常有一堆git push分支(最终)删除这种方式。 blah-mods-N更少分支杂乱,这绝对是正确的。但我更喜欢把我所做的一切都用名字来表达,直到我故意把它扔掉。

答案 2 :(得分:3)

您应该使用git stash

http://git-scm.com/book/en/Git-Tools-Stashing解释了您确切的案例情景

答案 3 :(得分:1)

即使你没有完成你的工作,我也建议他们提交。

承诺总是一个好主意。 - 但是,当你完成时,推动通常只是一个好主意。

如果您的提交不完美,您可以随时清理它们,或者在推送它们之前使用类似git rebase -i的内容进行组合。

相关问题