Git:在分支之间移动更改而不更改工作目录

发布于 2024-09-26 15:42:42 字数 749 浏览 6 评论 0原文

用例:每次我想将提交从一个 git 分支移动到另一个分支时,我都会执行以下一系列操作:

  1. [提交到工作分支]
  2. git checkout branch-to-merge-into
  3. gitcherry-pick 目标提交
  4. git推送
  5. git checkout 工作分支< /em>

唯一的例外是工作正常 - 每次我执行“git checkout”时,git 工作目录内容都会更改(预期),这会导致我的 IDE (IntelliJ IDEA) 执行内部状态更新(因为受监视的文件系统子树)被外部修改)。这真的很烦人,尤其是在大量小提交的情况下。

我看到有两种方法:

  1. 执行“大量樱桃选择”,即执行大量提交;例如,在工作日结束时将它们转移到另一个分支机构;
  2. 有第二个本地 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:

  1. [commit into working branch]
  2. git checkout branch-to-merge-into
  3. git cherry-pick target-commit
  4. git push
  5. 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:

  1. perform 'mass cherry picks', i.e. perform big number of commits; move them to another branch, say, at working day end;
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春花秋月 2024-10-03 15:42:42

不,在不更改工作目录的情况下不可能在分支之间移动提交。这是因为您最终会遇到冲突,此时 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.

烦人精 2024-10-03 15:42:42

如果您不需要经常合并更改,那么不进行 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 a git 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.

单身情人 2024-10-03 15:42:42

一种选择是使用 git-worktree。例如,您可以:

worktree_dir=$(mktemp -d)
git worktree add "$worktree_dir" branch-to-merge-into
git -C "$worktree_dir" cherry-pick target-commit
git -C "$worktree_dir" push
git worktree remove "$worktree_dir"

只要没有其他工作树签出branch-to-merge-into,这应该可以工作,而不会中断您的本地签出。

One option for this is to use git-worktree. For example you can:

worktree_dir=$(mktemp -d)
git worktree add "$worktree_dir" branch-to-merge-into
git -C "$worktree_dir" cherry-pick target-commit
git -C "$worktree_dir" push
git worktree remove "$worktree_dir"

As long as no other worktree has branch-to-merge-into checked out, this should work without disrupting your local checkout at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文