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:
Step 2: Check Git Log
Execute the βgit log –onelineβ to display the commit log history within a single line:
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:
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:
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.