为什么在切换分支之前仅暂存进行中的工作是不够的?为什么我也必须承诺? [英] Why isn't it sufficient to just stage work-in-progress before switching branches? Why must I also commit?

查看:38
本文介绍了为什么在切换分支之前仅暂存进行中的工作是不够的?为什么我也必须承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个分支(称为big_new_feature)中处理一些文件.这些文件尚未准备好提交,但我需要切换分支以在另一个分支(称为 stable_branch)中进行快速修复.为什么我不能在 big_new_feature 中暂存文件并查看 stable_branch?为什么我必须在切换到 stable_branch 之前实际提交 big_new_feature 中的文件?

I'm working on some files in one branch (called big_new_feature). The files are not ready to commit, but I need to switch branches to make a quick fix in another branch (called stable_branch). Why can't I just stage the files in big_new_feature and check out stable_branch? Why do I have to actually commit the files in big_new_feature before switching to stable_branch?

还是我误会了?

推荐答案

您不必必须提交那些.如果另一个分支还没有更改这些文件,那么您可以让它们暂存.尽管您可能不想这样做,因为 - 好吧,因为现在它们将在另一个分支上上演,而且听起来您不想在另一个分支上提交它们.>

You don't have to commit those. If the other branch hasn't also changed those files, then you can leave them staged. Though you probably don't want to, because - well, because now they'll be staged on the other branch and it sounds like you don't want to commit them on the other branch.

% vi foo.c
% git add foo.c
% git checkout branch
M       foo.c
Switched to branch 'branch'
% git status
On branch branch
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   foo.c

所以很有可能这样做,但是现在 foo.c 将被提交到 另一个分支,您刚刚切换到该分支以修复一个错误.这几乎肯定不是您想要的.

So it's quite possible to do, but now foo.c is to be committed on the other branch, the one that you just switched over to in order to fix a bug. Which is almost certainly not what you want.

我不知道你为什么不愿意在这种情况下做出承诺.这当然是最容易应对的情况,比藏起来更容易(尽管这也是一种可能性).

I'm not sure why you're averse to committing in this situation. That is certainly the far, far easiest situation to cope with, more so than stashing (though that would also be a possibility).

如果你担心你现在有一个半生不熟的进行中"提交,那么只需 commit --amend 你的下一次提交来清理它.或者在推送之前压缩提交.

If you're concerned that you now have a half-baked "in progress" commit, then simply commit --amend your next commit to clean that up. Or squash the commits before you push.

这篇关于为什么在切换分支之前仅暂存进行中的工作是不够的?为什么我也必须承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆