Git: move changes between branches without working directory change
Asked Answered
C

3

18

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?

Clump answered 7/10, 2010 at 12:13 Comment(7)
Option 3: Instruct your editor to stop getting in your way.Histiocyte
How would you like to work with editor that doesn't reflect external file changes?Clump
you should change the title to your actual questionNottage
duplicate: #3872012Nottage
Do you think that current title doesn't reflect actual question? Mentioned thread targets another problemClump
It is not a duplicate. And the title is good.Unconscious
I have the same problem, and would very much like to find a solution. Seems a limitation in git. One problem, as you mention, is the IDE. The other is, I regularly forget to switch back to the working branch, and then it will be out of sync with the database.Unconscious
N
10

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.

Nottage answered 7/10, 2010 at 21:29 Comment(1)
You are right that there could occur a conflict. However, git could try to do it anyway, and in case of a conflict it would refuse and tell you checkout the branch-to-merge-into. This would allow to do what you ask most of the time. Sounds like a good feature request.Unconscious
I
1

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.

Impasse answered 7/10, 2010 at 13:1 Comment(2)
Thanks for suggestion but as far as I understand merge will bring not only my commits but all commits between the branches. I.e. number of commits performed by me and my colleagues and I want to move only my target commits to another branch.Clump
@denis.zhdanov: I see what you need, now. I'm not sure how to automatically select your commits…Impasse
P
1

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.

Powel answered 13/11, 2020 at 19:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.