The situation is the following :
commit_A : contains fileA and fileB
commit_B : contains fileC and fileD
I want to move the fileA from the commit_A to the commit_B.
What I am doing most of the time is a interactive rebase, I mark commit_A as e (for edit), I then reset the commit, do a commit with fileA and redo the commit with only file B, then finish the rebase and finally I fixup the new commit with only fileA to commit_B. My question is, is there any better way to do ? Because it doesn't seem optimal, Thanks
The situation is the following :
commit_A : contains fileA and fileB
commit_B : contains fileC and fileD
I want to move the fileA from the commit_A to the commit_B.
What I am doing most of the time is a interactive rebase, I mark commit_A as e (for edit), I then reset the commit, do a commit with fileA and redo the commit with only file B, then finish the rebase and finally I fixup the new commit with only fileA to commit_B. My question is, is there any better way to do ? Because it doesn't seem optimal, Thanks
Share asked Nov 19, 2024 at 18:55 Lovis XIILovis XII 576 bronze badges1 Answer
Reset to default 1is there any better way to do ?
I believe there is - it is mixed reset.
Let's imagine your head is on commit_B
. The commands to execute are the following:
If
commit_A
is not the first commit in your repo, you need to reset
your head to state preceding to it:git reset [--mixed] HEAD~~
Now all the changes you made within the last two commits are in working tree, but not in stage area.
Stage fileB:
git add fileB
and commitgit commit -m 'fileB'
Stage the rest:
git add .
and commitgit commit -m 'A,C,D files'
If your old commits are already on remote repo, you have to rewrite history there too:
git push --force
Voila, it should work.
Usually git allows to do many things in different ways, so often it is just a matter of preference or how good you own the stuff (like in git checkout
vs git switch
).
The same is for rebase/merge, rebase/reset, et cetera.
For example, git rebase
is often used for squashing commits, but it does not preserve dates (the result will have the date of the oldest commit), but git reset --soft
doesn't have such problem.
If you are curious about what git reset
does, then git-reset-demystified shines in revealing some git concepts and terminology.