Branching and merging are critical aspects of Git that every developer must master. These operations form the foundation of collaborative development efforts, often becoming important topics in technical interviews.
Here’s a guide to the top Git branching and merging questions, paired with clear answers to help you ace your next Git-related interview.
Also, check PART 1, Part 3 & Part 4 of this article.
9. How Do You Rebase a Feature Branch Onto the Main Branch?
Rebasing integrates changes from the main branch into your feature branch by rewriting the commit history. Here’s how you can do it:
- Switch to your feature branch:# git checkout <feature-branch>
- Rebase the feature branch onto the main branch:# git rebase main
During the process, Git reapplies all commits from the feature branch onto the latest state of the main branch. In some cases, you may encounter conflicts, which you’ll need to resolve manually. After resolving, continue the rebase process with:
# git rebase --continue
Rebasing ensures that your feature branch has a clean history and is based on the latest updates from the main branch.
10. What is the Difference Between git merge and git rebase? When Would You Use Each?
git merge
- Combines changes from one branch into another.
- Produces a merge commit, preserving the history as-is.
- Commonly used when you want to keep the entire commit history intact.
git rebase
- Rewrites the commit history by integrating changes from the base branch before your branch’s commits.
- Does not create a merge commit unless there are conflicts.
- Used to create a linear and clean history.
When to Use Each?
- Use git merge for collaborative projects where preserving commit history is important (i.e., shared repositories).
- Use git rebase in personal or feature branches for cleaner history before merging into the main branch.
In general, merge is safer for shared workflows, while rebase is best suited for cleaning up history in private branches.
11. How Do You Resolve Merge Conflict When Merging a Feature Branch Into Main?
Merge conflicts occur when Git cannot automatically reconcile changes between branches. To resolve merge conflict during a merge:
1- Start merging the feature branch into the main branch:
# git checkout main
# git merge <feature-branch>
2- When conflicts are detected, Git will pause the process and display the files with conflicts. Open the conflicting files in your code editor and manually resolve the conflicts. Git marks conflicts with:
<<<<<<< HEAD
your changes
=======
feature branch changes
>>>>>>> feature-branch
3- After resolving the conflicts, mark the files as resolved:
# git add <file>
4- Finally, complete the merge:
# git commit
Pro Tip: Tools like VS Code, KDiff3, or Meld make handling merge conflicts easier.
12. How Do You Merge Only a Specific Commit from Another Branch?
If you need to merge only one commit (rather than the entire branch), you can use the git cherry-pick command:
- Identify the commit hash you want to merge:# git log <source-branch>
- Use cherry-pick to bring the specific commit into your current branch:# git cherry-pick <commit-hash>
Cherry-picking applies the changes from the specified commit to your branch without merging the rest of the branch’s history.
13. How Do You Delete a Remote Branch That Is No Longer Needed?
To delete a remote branch, follow these steps:
- Ensure the branch has been merged locally or is no longer required.
- Delete the remote branch using:# git push origin --delete <branch-name>
This removes the branch from the remote repository. To clean up your local branches, you can delete the corresponding local branch as well:
# git branch -d <branch-name>
14. How Do You Update Your Local Branch When the Main Branch Is Ahead, Without Losing Your Commits?
If your main branch has been updated and is ahead of your local branch, you can sync it efficiently without overwriting your commits:
- Fetch the latest changes from the remote repository:# git fetch origin
- Rebase your local branch on top of the updated main branch:# git rebase origin/main
Alternatively, if you want to avoid rebasing:
# git pull origin main
This integrates the latest changes from the remote main branch while preserving your commits.
15. What Happens If You Try to Merge a Branch That Has Already Been Merged?
If you attempt to merge a branch that has already been merged into your current branch, Git recognizes this action and efficiently skips the merge. You’ll see a message like:
Already up-to-date.
This ensures that no duplicate changes or unnecessary merge commits are added.
Conclusion
Understanding concepts like branching, merging & to resolve merge conflict in Git are some essential aspects for handling collaborative development and resolving common version control challenges. Whether it’s rebasing for a clean history, resolving merge conflicts, or working with specific commits, these techniques demonstrate your expertise with Git during technical interviews.
By equipping yourself with these answers, you’ll be able to confidently handle both theoretical and practical Git scenarios.
We are giving you exclusive deals to try Linux Servers for free with 100$ credit, check these links to claim your 100$,
DigitalOcean - 100$ free credit & Linode - 100$ free credit
Check some Exclusive Deals, HERE.
Also, check out DevOps Book You should read section.