Junior — Middle
When do you choose to use rebase instead of merge in Git, and why?
sobes.tech AI
Answer from AI
I choose the git rebase command when I want to get a cleaner and linear commit history. Rebase moves my local changes on top of the current branch, which helps avoid unnecessary merge commits.
I use rebase, for example, before pushing changes to the shared branch, to incorporate the latest updates from it and make the history clearer. However, rebase should not be applied to public branches to avoid conflicts for other developers.
Example:
git checkout feature
git fetch origin
git rebase origin/main
This will move the commits from the feature branch on top of the latest state of main.