0
0
Gitdevops~30 mins

When to rebase vs when to merge in Git - Hands-On Comparison

Choose your learning style9 modes available
When to Rebase vs When to Merge in Git
📖 Scenario: You are working on a team project using Git. You have a main branch called main and a feature branch called feature. You want to learn how to keep your feature branch up to date with the latest changes from main using either git rebase or git merge. This will help you avoid conflicts and keep your project history clean.
🎯 Goal: Learn when to use git rebase and when to use git merge to update your feature branch with changes from main. Practice commands to rebase and merge, and see the difference in commit history.
📋 What You'll Learn
Use git checkout to switch branches
Use git merge to merge branches
Use git rebase to rebase branches
Understand the difference in commit history after merge vs rebase
💡 Why This Matters
🌍 Real World
Teams use Git to collaborate on code. Knowing when to merge or rebase helps keep the project history clean and understandable.
💼 Career
Software developers and DevOps engineers often need to update feature branches with main branch changes. Understanding merge vs rebase is essential for smooth teamwork and code integration.
Progress0 / 4 steps
1
Setup branches and initial commits
Create a new Git repository. Then create a file called file.txt with the text Initial content. Commit this change on the main branch. Next, create and switch to a branch called feature. On feature, change file.txt to have the text Feature work and commit this change.
Git
Need a hint?

Use git init to start the repo. Use git checkout -b feature to create and switch to the feature branch.

2
Add a commit on main branch
Switch back to the main branch. Create a new file called main.txt with the text Main branch update. Commit this change on main.
Git
Need a hint?

Use git checkout main to switch back. Then edit and commit the file.

3
Update feature branch using git merge
Switch to the feature branch. Use git merge main to bring the changes from main into feature. This will create a merge commit.
Git
Need a hint?

Use git checkout feature to switch. Then run git merge main.

4
Update feature branch using git rebase
Reset the feature branch to before the merge. Then switch to feature branch again. Use git rebase main to replay your feature commits on top of main. Finally, print the commit log with git log --oneline --graph --all to see the difference in history.
Git
Need a hint?

Use git reset --hard HEAD~1 to undo the merge commit. Then git rebase main. Finally, run git log --oneline --graph --all to see the commit history.