Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moving a git commit to before a particular commit

Tags:

git

I have committed some changes to GIT, namely a18 and 522, then pulled and merged master, which resulted in a number of changes. I subsequently fixed an additional problem, which actually belongs to before the merge.

I have not pushed yet, so my changes are not merged to other branches yet.
Is it possible to move my fix down before pushing?

I have committed everything, but not pushed it. It's sitting in an unpushed branch called ZA

166ff44b CM 9 seconds ago   000:Test(s) after merge                    <-- MOVE THIS COMMIT
18b60812 CM 3 hours ago     Merge branch 'master' into za
efef7a58 MK 20 hours ago    without site and without post steps
fc0540a0 MK 20 hours ago    Moved Jenkins Files to root directory
888b8d5d MK 21 hours ago    Jenkinsfile online editiert mit Bitbucket
1d55252d MK 21 hours ago    Jenkinsfile online editiert mit Bitbucket
ba8fc771 MK 21 hours ago    Jenkinsfile online editiert mit Bitbucket
9e8c2e89 MK 21 hours ago    Jenkinsfile online editiert mit Bitbucket
c6cd623d MK 22 hours ago    Jenkinsfile online editiert mit Bitbucket
c3c5310e MK 22 hours ago    pom.xml online editiert mit Bitbucket
a18b79f2 CM 5 days ago      000:New
52212dbc CM 5 days ago      000:Work list speed improvements

So that the outcome is as follows:

18b60812 CM 3 hours ago     Merge branch 'master' into za
efef7a58 MK 20 hours ago    without site and without post steps
fc0540a0 MK 20 hours ago    Moved Jenkins Files to root directory
888b8d5d MK 21 hours ago    Jenkinsfile online editiert mit Bitbucket
1d55252d MK 21 hours ago    Jenkinsfile online editiert mit Bitbucket
ba8fc771 MK 21 hours ago    Jenkinsfile online editiert mit Bitbucket
9e8c2e89 MK 21 hours ago    Jenkinsfile online editiert mit Bitbucket
c6cd623d MK 22 hours ago    Jenkinsfile online editiert mit Bitbucket
c3c5310e MK 22 hours ago    pom.xml online editiert mit Bitbucket 
166ff44b CM 9 seconds ago   000:Test(s) after merge                    <-- TO HERE 
a18b79f2 CM 5 days ago      000:New
52212dbc CM 5 days ago      000:Work list speed improvements
like image 496
Jim Avatar asked Sep 25 '26 13:09

Jim


1 Answers

You may reorder the commits with a rebase in interactive mode.

This is done by entering the following command:

git rebase -i a18b79f2

Your default text editor will be opened, with the list of commits, in their current order.

Simply cut/paste the commit line where you would like it to be, and save.

You are then good to go.

For more information, check the rebase in the git manual, or this great reference article: How to undo (almost) anything with Git .

P.S. The interactive rebase may also be used to merge commits, drop commits, update commit messages...

like image 169
Mehdi Avatar answered Sep 28 '26 22:09

Mehdi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!