TIL you can re-order your commit history through rebase -i
Benji 🍙
Posted on June 13, 2023
Suppose you have a commit history like this:
commit 5: Refactor code for better performance
commit 4: Add new feature: user authentication
commit 3: Fix typo in README file
commit 2: Update documentation with usage instructions
commit 1: Initial commit with project setup
And then you have a new commit "Another README for user authentication" that should go after commit 4 and before commit 5:
commit 6: Another README for user authentication
commit 5: Refactor code for better performance
commit 4: Add new feature: user authentication
commit 3: Fix typo in README file
commit 2: Update documentation with usage instructions
commit 1: Initial commit with project setup
Then using git rebase -i HEAD~x
(where x represents the number of commits you want to include in the rebase) you can re-order the commit history with relative ease, like this:
git rebase -i HEAD~6
This opens up your editor as usual
pick 6: Another README for user authentication
pick 5: Refactor code for better performance
pick 4: Add new feature: user authentication
pick 3: Fix typo in README file
pick 2: Update documentation with usage instructions
pick 1: Initial commit with project setup
And moving the commit to where you want
pick 5: Refactor code for better performance
pick 6: Another README for user authentication
pick 4: Add new feature: user authentication
pick 3: Fix typo in README file
pick 2: Update documentation with usage instructions
pick 1: Initial commit with project setup
Save then close the editor and it's done. Confirm in your git log before pushing remotely
commit 6: Refactor code for better performance
commit 5: Another README for user authentication
commit 4: Add new feature: user authentication
commit 3: Fix typo in README file
commit 2: Update documentation with usage instructions
commit 1: Initial commit with project setup
💖 💪 🙅 🚩
Benji 🍙
Posted on June 13, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.