简体   繁体   English

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

[英]switch to another branch without committing

I am working on a feature branch and have not finished the work there - Now I need to change to a different branch to fix something 我正在开发一个功能分支并且还没有完成那里的工作 - 现在我需要更改到另一个分支来修复某些东西

for example 例如

feat1 - 6+ files changed

When I checkout to feat2 branch, after git add . 当我结帐到feat2分支时,在git add . in feat1, git seems to carry over the staged yet uncommitted file changes. 在feat1中,git似乎延续了暂存但尚未提交的文件更改。

If I commit these file changes in feat1, checking out to feat2 will not carry over those changes 如果我在feat1中提交了这些文件更改,则检查到feat2将不会继承这些更改

How can I switch branches without committing file changes? 如何在不提交文件更改的情况下切换分支?

Stash them: 藏匿其中:

$ 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

When you're done, you can use git stash apply to apply the changes in your stash and keep the stash around, or git stash pop to apply the changes and remove the stash as long as there are no conflicts. 完成后,您可以使用git stash apply在您的存储中应用更改并保留存储,或者git stash pop应用更改并删除存储,只要没有冲突。

If you end up with multiple stashes at once you can apply or pop them in an arbitrary order using the stash@ string, eg git stash apply stash@{6} . 如果你最终有多个stashes,你可以使用stash@ string以任意顺序applypop它们,例如git stash apply stash@{6}

Most people recommend git stash . 大多数人推荐git stash

I prefer just doing a git commit . 我更喜欢做一个git commit You can always git commit --amend later. 您可以随时git commit --amend Just make sure not to push that particular commit. 请确保不要推送该特定提交。 If it helps—and for me, it does—just make a branch for "work in progress on feature". 如果它有帮助 - 对我来说,它确实 - 只是为“功能正在进行中”制作一个分支。 For instance: 例如:

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

At this point, I realize I need to save the work-so-far and go do something else: 在这一点上,我意识到我需要保存工作 - 到目前为止,并去做其他事情:

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

Now everything is all nicely saved away on a local branch that I have named in a way that helps me remember what I was doing, when I come back later. 现在一切都很好地保存在一个本地分支上,我以一种帮助我记住我在做什么的方式命名,当我稍后回来时。

Often, I come back later to find that origin/zorg has been updated, so: 通常,我后来回来发现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]

If the rebase does not go well (or I need to rework things), I use git rebase --abort (or just skip the rebase attempt) and then start a new zorg-stones-2 branch based on the updated zorg . 如果rebase不顺利(或者我需要重做工作),我使用git rebase --abort (或者只是跳过rebase尝试),然后根据更新的zorg启动一个新的zorg-stones-2分支。 When I'm good about it, I commit often enough and don't have to do a lot of git rebase -i zorg to fix it up a whole lot before doing git checkout zorg; git merge zorg-stones-N 当我很擅长的时候,我经常提交,并且在做git checkout zorg; git merge zorg-stones-N之前不需要做很多git rebase -i zorg来修复它git checkout zorg; git merge zorg-stones-N git checkout zorg; git merge zorg-stones-N to bring in the final version, ready to git push or whatever. git checkout zorg; git merge zorg-stones-N引入最终版本,准备git push或者其他什么。

I often have a bunch of blah-mods-N branches to (eventually) delete this way. 我经常有一堆blah-mods-N分支(最终)删除这种方式。 It's definitely true that stash ing is less branch-clutter-y. stash疑问, stash分支杂乱性更小。 But I prefer to have everything I did accessible by name up until I deliberately toss it out. 但我更喜欢把我所做的一切都用名字来表达,直到我故意把它扔掉。

You should use git stash 你应该使用git stash

http://git-scm.com/book/en/Git-Tools-Stashing explains your exact case-scenario http://git-scm.com/book/en/Git-Tools-Stashing解释了您的确切案例场景

Even if you did not finish your work I would suggest committing them. 即使你没有完成你的工作,我也建议他们提交。

Committing is always a good idea. 承诺总是一个好主意。 - However, pushing is usually only a good idea when you are finished. - 但是,当你完成时,推动通常只是一个好主意。

If your commits are not perfect you can always clean them up or combine them using something like git rebase -i before pushing them. 如果您的提交不完美,您可以随时清理它们,或者在推送之前使用git rebase -i等组合它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM