DEV Community

Benji 🍙
Benji 🍙

Posted on

TIL you can re-order your commit history through rebase -i

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)