Q: What is a branch in Git?
A: A branch in Git is a movable pointer to a commit. The default branch name in Git is master (or main in newer versions), and any other branch is simply a name for a different set of commits.
Q: How do you create a new branch?
A: You can create a new branch using the git branch command followed by the branch name.
git branch new-branch
Q: How do you switch to a different branch?
A: To switch to a different branch, use the git checkout command followed by the branch name.
git checkout new-branch
Alternatively, you can create and switch to a new branch in one command using git checkout -b.
git checkout -b new-branch
Q: How do you list all branches in a repository?
A: To list all branches in your repository, use the git branch command.
git branch
Q: How do you delete a branch?
A: To delete a branch, use the git branch -d command followed by the branch name.
git branch -d new-branch
Q: How do you merge one branch into another?
A: To merge a branch into the current branch, use the git merge command followed by the branch name you want to merge.
git merge new-branch
Q: How do you resolve conflicts when merging branches?
A: If there are conflicts during the merge, Git will mark the files as conflicted and stop the merge process. You can resolve the conflicts manually by editing the files, then add the resolved files using git add and complete the merge with git commit.
Q: How do you rename a branch?
A: To rename the current branch, use the git branch -m command followed by the new branch name.
git branch -m new-branch-name
Q: How do you push a new branch to a remote repository?
A: To push a new branch to a remote repository, use the git push command followed by the remote name and the branch name.
git push origin new-branch
Q: How do you track a remote branch?
A: To track a remote branch, use the git checkout command with the --track option.
git checkout --track origin/remote-branch
Example Code
# Initialize a new Git repository
git init my-repo
cd my-repo
# Create a new file and commit it to the master branch
echo "Hello, world!" > file.txt
git add file.txt
git commit -m "Initial commit"
# Create a new branch and switch to it
git checkout -b feature-branch
# Make changes in the new branch
echo "Feature work" > feature.txt
git add feature.txt
git commit -m "Add feature work"
# Switch back to the master branch
git checkout master
# Merge the feature branch into the master branch
git merge feature-branch
# Delete the feature branch
git branch -d feature-branch
# Push the master branch to the remote repository
git remote add origin https://github.com/yourusername/your-repo.git
git push -u origin master
This example covers the basics of creating, switching, merging, and deleting branches in Git, as well as pushing changes to a remote repository.
Important Interview Questions and Answers on Git Branch
Q: What is a branch in Git?
A branch in Git is a lightweight movable pointer to a commit. Branching allows you to work on different parts of a project separately. By creating a branch, you can isolate your work from the main codebase until it’s ready to be merged.
Q: How do you create a new branch in Git?
You can create a new branch in Git using the git branch command followed by the branch name.
git branch new-branch
Q: How do you switch to an existing branch in Git?
You can switch to an existing branch using the git checkout command followed by the branch name.
git checkout existing-branch
Q: How do you create and switch to a new branch in one command?
You can create and switch to a new branch in one command using git checkout -b followed by the branch name.
git checkout -b new-branch
Q: How do you list all branches in a Git repository?
You can list all branches in a Git repository using the git branch command.
git branch
Q: How do you delete a branch in Git?
You can delete a branch in Git using the git branch -d command followed by the branch name.
git branch -d branch-to-delete
If the branch has not been merged, you can force delete it using -D.
git branch -D branch-to-delete
Q: What is the difference between git merge and git rebase?
- git merge integrates changes from one branch into another. It creates a new commit that combines the changes from both branches.
- git rebase moves or combines a sequence of commits to a new base commit. It rewrites the commit history to produce a linear sequence of commits.
Q: How do you merge a branch into the current branch in Git?
You can merge a branch into the current branch using the git merge command followed by the branch name you want to merge.
git merge branch-to-merge
Q: How do you rebase a branch in Git?
You can rebase the current branch onto another branch using the git rebase command followed by the branch name.
git rebase branch-to-rebase-onto
Q: How do you resolve conflicts during a merge or rebase?
When conflicts occur, Git will pause the merge or rebase process and allow you to manually resolve the conflicts. After resolving the conflicts, you can continue the process.
To continue after resolving conflicts during a merge:
git add resolved-file
git commit
To continue after resolving conflicts during a rebase:
git add resolved-file
git rebase --continue
Example Code
- Creating and switching to a new branch:
git checkout -b feature-branch
- Listing all branches:
git branch
- Merging a branch:
git checkout main
git merge feature-branch
- Rebasing a branch:
git checkout feature-branch
git rebase main
- Resolving conflicts:
# During merge/rebase, resolve conflicts manually in the files
git add resolved-file
# Continue rebase
git rebase --continue
# Or, if merging
git commit