Git

How to Reorder Commits with Git?

In Git, commits keep the changes that are performed in the Git working area over time. Sometimes, users want to arrange to commit in a specified sequence and make a logical Git history. The chronological sequence of changes can be corrected by rearranging commits, which will make it simpler to comprehend the history of development and identify problems.

This tutorial will state the method for reordering the commits with Git.

How to Reorder Commits with Git?

To reorder the commit with Git, check out the below-stated procedure:

  • Redirect the Git local repository.
  • View the Git log to show commits.
  • Execute the “git rebase -i <head>” command to rebase the commits.
  • Reorder commits manually in the specified editor.
  • Verify the changes by running the “git log –oneline” command.

Step 1: Go to the Git Local Repository

Initially, go to the Git local directory by utilizing the “cd” command:

cd "C:\Users\user\Git\testrepo"

Step 2: Check Git Log

Execute the “git log –oneline” to display the commit log history within a single line:

git log --oneline

From the below-stated output, we want to reorder the most recent three commits:

Step 3: Launch Commits in Notepad

Now, run the “git rebase -i” command to rebase the last three commits in the current working branch:

git rebase -i HEAD~3

When the above-stated command is executed, the specified number of commits history will be opened with the default text editor:

Step 4: Reorder Commits

Now, reorder the commits according to your specification manually, save changes by pressing the “CTRL + S” keys, and close the file:

From the provided output, the below-highlighted displayed message indicates that we have successfully reordered commits:

Step 5: Verify Changes

To ensure that the desired commits has be reordered or not, use the “git log –oneline” command:

git log --oneline

The provided output shows that commits has been reordered successfully:

That’s all! We have compiled a way to reorder the commits with Git.

Conclusion

To reorder commits with Git, redirect to the Git local repository, and view the Git log commit history. Then, run the “git rebase -i <head>” command to rebase the most recent three commits. After that, reorder commits manually in the default editor and verify the changes by executing the “git log –oneline” command. This post demonstrated the method for reordering the commits with Git.

About the author

Hafsa Javed