Git: move a commit "on top"
Solution 1
git rebase -i B
, and then move $
to the end of the list that shows up in your editor. It will start out as the first line in the file that opens. You could also just delete that line entirely, which will just drop that commit out of your branch's history.
Solution 2
If you want to keep you commits in the same order on feature
you should make a new branch and perform the following. Otherwise just do this on feature
git rebase -i <sha for commit B>
Move commit $ to the bottom of the list
git checkout master
git rebase feature <or the other branch name>
It wasn't clear to me on the question but if you didn't want $ at all, rather than moving it delete it after git rebase -i
Though you will want to do this on a new branch so that you don't lose it. As you are changing history.
This also assumes that the branch feature
hasn't been pushed to a remote as rewriting history is bad.
Comments
-
Adrian Panasiuk almost 2 years
Let's say in
master
I have a feature disabled. I work on that feature on branchfeature
, so I have a special commit$
there that just enables that feature. Now I want to merge the changes I did infeature
intomaster
, but keep the enabling commit out. So it's likemain: A--B--X--Y feature: A--B--$--C--D
So let's say I want to do it, by moving the
$
commit on top of feature:new feature: A--B--C--D--$
How would I go about doing that?
-
Adrian Panasiuk over 11 yearsCool thanks! It's so clear now that I should use rebase, I kept trying to solve it with cherry-picks before for some reason.
-
Carl Norum over 11 years@AdrianPanasiuk, you could use cherry-pick too, but it would be a lot more effort, probably.
-
yunzen over 4 yearsYou could also use
git rebase -i $~1
which is equivalent as$~1
is the commit before$
, which isB
-
Frank Puck over 2 yearsunclear: "and then move $ to the end of the list" -- I'm asking here on how to do this!
-
Frank Puck over 2 yearswhat does the first rebase do? Why do I have to apply rebase to B instead of to $? What is the state after the first rebase?
-
Frank Puck over 2 yearswhy do you checkout the main branch? The original question was to modify the feature branch and leave the main branch untouched.
-
Schleis over 2 years@FrankPuck The first rebase is what would allow you to reorder the commits. You use the sha for B as it is the first one that doesn't need to be touched. When you reorder the commits, the SHAs for all the other commits will change (iirc). As for the second checkout, not sure (I answered 8+ years ago). I think that it was to also get the commits onto master.
-
Carl Norum over 2 yearsJust move the line in your editor, however that happens to work for your configuration.