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

Nic*_*nto 5 git

我正在开发一个功能分支并且还没有完成那里的工作 - 现在我需要更改到另一个分支来修复某些东西

例如

feat1 - 6+ files changed
Run Code Online (Sandbox Code Playgroud)

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

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

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

Chr*_*ris 8

藏匿其中:

$ 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
Run Code Online (Sandbox Code Playgroud)

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

如果你最终有多个stashes,你可以applypop使用stash@字符串以任意顺序,例如git stash apply stash@{6}.


tor*_*rek 5

大多数人推荐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 ...
Run Code Online (Sandbox Code Playgroud)

在这一点上,我意识到我需要保存到目前为止的工作并去做其他事情:

$ git checkout -b zorg-stones-1
Switched to a new branch 'zorg-stones-1'
$ git commit
Run Code Online (Sandbox Code Playgroud)

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

通常,我稍后回来发现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]
Run Code Online (Sandbox Code Playgroud)

如果 rebase 不顺利(或者我需要返工),我使用git rebase --abort(或只是跳过 rebase 尝试)然后zorg-stones-2基于更新的zorg. 当我擅长它时,我经常提交足够多的内容git rebase -i zorg,并且在git checkout zorg; git merge zorg-stones-N引入最终版本之前不必做很多事情来修复它,准备好git push或其他什么。

我经常有一堆blah-mods-N分支(最终)以这种方式删除。这绝对是真的,stashING是分支杂波-Y少。但我更喜欢把我所做的一切都以名字命名,直到我故意把它扔掉。