Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Squashing Commits in Git
📖 Scenario: You are working on a small project and have made several commits. Now, you want to clean up your commit history by combining these commits into one before sharing your work with your team.
🎯 Goal: Learn how to squash multiple commits into a single commit using Git interactive rebase.
📋 What You'll Learn
Create a Git repository with three commits
Set a variable for the number of commits to squash
Use Git interactive rebase to squash commits
Show the final commit log with a single combined commit
💡 Why This Matters
🌍 Real World
Squashing commits helps keep the project history clean and easier to understand before sharing code with others.
💼 Career
Many software development teams require clean commit histories for code reviews and collaboration, making this skill essential for developers.
Progress0 / 4 steps
1
Create a Git repository with three commits
Initialize a new Git repository called myproject. Create three commits with these exact commit messages in order: First commit, Second commit, and Third commit. Use the commands git init, git add, and git commit -m as needed.
Git
Hint
Use git init to start the repo. Then create or modify a file and commit three times with the exact messages.
2
Set the number of commits to squash
Create a shell variable called NUM_COMMITS and set it to 3 to represent the number of commits you want to squash.
Git
Hint
Use NUM_COMMITS=3 to set the variable.
3
Use Git interactive rebase to squash commits
Run the command git rebase -i HEAD~$NUM_COMMITS to start an interactive rebase for the last three commits. In the editor that opens, change the second and third commits from pick to squash to combine them into the first commit. Save and close the editor to complete the squash.
Git
Hint
Use git rebase -i HEAD~$NUM_COMMITS and then edit the commit list to squash.
4
Show the final commit log with a single combined commit
Run git log --oneline to display the commit history. The output should show only one commit with a combined message from the three original commits.
Git
Hint
Use git log --oneline to see the combined commit message.
Practice
(1/5)
1. What is the main purpose of squashing commits in Git?
easy
A. To revert the last commit without changing history
B. To combine multiple commits into one for a cleaner history
C. To create a new branch from the current commit
D. To delete all commits from the repository
Solution
Step 1: Understand commit history management
Squashing is used to combine several commits into a single commit to simplify the commit history.
Step 2: Identify the purpose of squashing
This helps keep the project history clean and easier to read by reducing clutter from many small commits.
Final Answer:
To combine multiple commits into one for a cleaner history -> Option B
Quick Check:
Squashing = combine commits [OK]
Hint: Squash = combine commits to clean history [OK]
Common Mistakes:
Thinking squashing deletes commits permanently
Confusing squashing with branching
Believing squashing reverts commits
2. Which Git command starts an interactive rebase to squash commits?
easy
A. git commit --squash HEAD~3
B. git merge -i HEAD~3
C. git rebase -i HEAD~3
D. git reset --soft HEAD~3
Solution
Step 1: Identify the command for interactive rebase
The command to start an interactive rebase is git rebase -i followed by the commit range.
Step 2: Confirm the correct syntax
git rebase -i HEAD~3 opens the last 3 commits for editing, allowing squashing.
Final Answer:
git rebase -i HEAD~3 -> Option C
Quick Check:
Interactive rebase = git rebase -i [OK]
Hint: Use git rebase -i to start squashing commits [OK]