Resolving Merge Conflicts, Cherry-Picking, and Key Interview Questions
Understanding Merge Conflicts in Git
Merge conflicts occur when Git attempts to combine changes from different branches that modify the same file in different ways. Resolving conflicts requires manual intervention. Here's a brief guide on resolving conflicts:
Identify Conflicts:
- When merging branches with conflicting changes, Git marks the conflicted areas in the affected files.
Manually Resolve:
Open the conflicted file(s) in your code editor.
Locate and resolve the conflicting sections, keeping the changes you want.
Mark as Resolved:
After manual resolution, mark the conflicts as resolved using:
git add <conflicted_file>
Complete Merge:
Continue the merge process:
git merge <branch_name>
Cherry-Picking Specific Commits
Cherry-picking allows you to apply specific commits from one branch to another. It is useful when you want to merge specific changes without merging an entire branch. Use the following command:
git cherry-pick <commit_id>
This applies the changes introduced by the specified commit to your current branch.
Git Show and Commit History
The git show
command helps you inspect the details of a specific commit or the changes introduced by it. For example:
git show <commit_id>
To view the commit history, you can use:
git log --patch # Display changes for each commit
git log --stat # Display concise statistics for each commit
Reverting Merges
If you need to undo a merge, you can use the git revert
command. It creates a new commit that undoes the changes introduced by the specified merge commit:
git revert <merge_commit_id>
Interview Questions
What is Git and why use it?
- Git is a distributed version control system used to track changes in source code during software development, facilitating collaboration among multiple contributors.
Explain the stages of Git.
- The three stages are Working Directory, Staging Area, and Repository. Changes progress from the working directory to the staging area and then to the repository.
Difference between CVCS and DVCS?
- Centralized Version Control System (CVCS) has a central repository, while Distributed Version Control System (DVCS) allows each user to have a complete copy of the repository.
How to check commits in Git?
- Use
git log
to view commit history.
- Use
Explain branches.
- Branches in Git are independent lines of development that allow for parallel work on different features or fixes.
What is .gitignore?
.gitignore
is a file specifying patterns of untracked files that Git should ignore.
Difference between git pull vs git push?
git pull
fetches changes from a remote repository and merges them into the current branch, whilegit push
sends local changes to a remote repository.
Difference between git pull vs git fetch?
git pull
fetches changes and merges them, whereasgit fetch
fetches changes but does not merge them.
Difference between git merge vs git rebase?
git merge
combines changes from different branches, creating a new commit, whilegit rebase
moves or combines a sequence of commits to a new base commit.
Difference between git merge vs git cherry-pick?
git merge
combines all changes from one branch into another, whilegit cherry-pick
applies specific commits from one branch to another.
Difference between git clone vs git pull?
git clone
copies a repository to create a local copy, whilegit pull
fetches changes from a remote repository and merges them.
Difference between git clone vs git fork?
git clone
creates a local copy of a repository, whilegit fork
creates a personal copy of a repository on a platform like GitHub.
Difference between git revert vs git restore?
git revert
creates a new commit that undoes changes, whilegit restore
discards changes in the working directory.
What is a merge conflict, and how do you resolve it?
- A merge conflict occurs when Git cannot automatically merge changes. To resolve, manually edit the conflicted file, mark as resolved with
git add
, and complete the merge.
- A merge conflict occurs when Git cannot automatically merge changes. To resolve, manually edit the conflicted file, mark as resolved with
What is git stash?
- Git stash is a feature to temporarily hide changes that are not yet committed, allowing you to switch branches or perform other operations without committing incomplete changes.