Git:在分支之间移动更改而不更改工作目录
用例:每次我想将提交从一个 git 分支移动到另一个分支时,我都会执行以下一系列操作:
- [提交到工作分支]
git checkout
branch-to-merge-intogitcherry-pick
目标提交git推送
git checkout
工作分支< /em>
唯一的例外是工作正常 - 每次我执行“git checkout”时,git 工作目录内容都会更改(预期),这会导致我的 IDE (IntelliJ IDEA) 执行内部状态更新(因为受监视的文件系统子树)被外部修改)。这真的很烦人,尤其是在大量小提交的情况下。
我看到有两种方法:
- 执行“大量樱桃选择”,即执行大量提交;例如,在工作日结束时将它们转移到另一个分支机构;
- 有第二个本地 git 存储库并对其执行cherry pick,即每次对工作分支执行实际提交和推送时,转到第二个存储库,拉取更改并在那里执行cherry pick;
我不喜欢第一种方法,因为它可能会忘记移动特定的提交。第二个看起来有点……不自然。
基本上,如果我可以说 git '将此提交从名为branchX的分支移动到分支branchX+1'而不更新工作目录,那就完美了。
问题:是否可以执行上述操作?
Use-case: every time I want to move commit from one git branch to another I perform the following sequence of actions:
- [commit into working branch]
git checkout
branch-to-merge-intogit cherry-pick
target-commitgit push
git checkout
working-branch
That works fine with the only exception - every time I perform 'git checkout' git working directory content is changed (expected) and that causes my IDE (IntelliJ IDEA) to perform inner state update (because monitored file system sub-tree is modified externally). That really annoys especially in case of big number of small commits.
I see two ways to go:
- perform 'mass cherry picks', i.e. perform big number of commits; move them to another branch, say, at working day end;
- have a second local git repository and perform cherry picks on it, i.e. every time actual commit and push is performed to the working branch, go to that second repository, pull the changes and perform cherry pick there;
I don't like the first approach because its possible to forget to move particular commit. The second one looks a bit... unnatural.
Basically, it would be perfect if I could say git 'move this commit from branch with name branchX to the branch branchX+1' without working directory update.
Question: is it possible to perform the above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,在不更改工作目录的情况下不可能在分支之间移动提交。这是因为您最终会遇到冲突,此时 git 会暂停,以便您可以修复冲突。如果你的直接工作不代表这种状态,那么你将无法正确解决冲突。
如果你环顾四周,你会发现很多其他可能的解决方案来解决这个问题,但潜在的问题听起来像是你的编辑器不处理从它下面更改的文件。这基本上就是使用 git 的一个事实。因此,要么更新编辑器,要么转向更适合 git 工作流程的东西。
No, it is not possible to move a commit between branches without changing the working directory. This is because you will eventually run into a conflict, at which point git pauses so you can fix the conflict. If your working directly did not represent that state, then you would not be able to fix the conflicts correctly.
If you look around, you will find a lot of other possible solutions to this problem on SO, but the underlying issue sounds like that your editor does not handle the files being changed out from underneath it. This is basically a fact of using git. So, either update the editor or move to something more suited for a git workflow.
如果您不需要经常合并更改,那么不进行
cherry-pick
操作,不如偶尔从您的git merge
操作一下,怎么样? code><要合并到的分支>?如果我没有记错的话,这相当于对自上次合并以来的所有更改进行樱桃腌制(使用这种方法不会有忘记提交的风险)。这样,“编辑器问题”就会减少发生。If you do not need to often merge your changes, instead of doing
cherry-pick
, how about doing once in a while agit merge <working branch>
from your<branch to merge into>
? This would be the equivalent of cherry-pickling all the changes since the last time your merged, if I'm not mistaken (there is no risk for forgetting a commit, with this approach). This way, the "editor problem" would happen less often.一种选择是使用
git-worktree
。例如,您可以:只要没有其他工作树签出
branch-to-merge-into
,这应该可以工作,而不会中断您的本地签出。One option for this is to use
git-worktree
. For example you can:As long as no other worktree has
branch-to-merge-into
checked out, this should work without disrupting your local checkout at all.