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.