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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay