Rebase
- Git course
- Advanced
- Lesson 53/56
- 6 min
Squash and reorder commits with interactive rebase.
Interactive Rebase
Interactive rebase is used to reorganize a branch's commit history. With git rebase -i, you can list, combine, or reorder commits. It is especially useful for editing commit history that has not been shared yet.
git rebase -i HEAD~3The command opens an editing list for the last three commits.
Practice Squash Commits
Start an interactive rebase for the last three commits. Leave the first commit as pick and change the next two commits to squash. This combines the three commits into one.
Starter code
# Son üç commit'i düzenleSample solution
git rebase -i HEAD~3
# Düzenleme listesi:
pick a1b2c3d İlk değişiklik
squash d4e5f6a İkinci değişiklik
squash 7a8b9c0 Üçüncü değişiklikExpected output
Üç commit tek commit olarak birleştirildi.
Practice Reorder Commit History
Start an interactive rebase for the last four commits. Arrange the commits in this order: a1b2c3d, then 7a8b9c0, followed by d4e5f6a, and finally e1f2a3b. Keep the operation as pick on every line.
Starter code
# Son dört commit'i düzenleSample solution
git rebase -i HEAD~4
# Düzenleme listesi:
pick a1b2c3d İlk commit
pick 7a8b9c0 Üçüncü commit
pick d4e5f6a İkinci commit
pick e1f2a3b Son commitExpected output
Commit sırası yeniden düzenlendi.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which operation is used to combine a commit with the previous commit during an interactive rebase?
- edit
- squash Correct answer
- reword
- drop
Explanation: <code>squash</code> combines the selected commit's changes with the previous commit.
2 Reorder Commits
git rebase -i HEAD~4
# Düzenleme listesinde commit satırlarını yeniden sırala
# Kaydet ve kapat____
git rebase -i HEAD~4
# Düzenleme listesinde commit satırlarını yeniden sırala
# Kaydet ve kapatyeniden sırala
Explanation: Reordering commit lines in the interactive rebase list changes the order of commits in history.
3 Interactive Rebase Operations
| Term | Meaning |
|---|---|
pick |
Applies the commit as it is |
squash |
Combines commits |
reword |
Changes the commit message |
drop |
Removes a commit from history |