Top 20+ Git Interview Questions and Answers

1. Q: What is Git?

A: Git is a distributed version control system used for tracking changes in source code during software development. It allows multiple developers to collaborate on a project, manage different versions of files, and merge changes seamlessly.

2. Q: How does Git differ from other version control systems?

A: Unlike centralized version control systems, Git is distributed, which means every developer has a complete copy of the repository. This allows for offline work, faster operations, and greater flexibility in branching and merging.

3. Q: What is a repository in Git?

A: A repository in Git is a directory or folder that stores all the files, history, and branches of a project. It acts as a central location for developers to collaborate and manage code changes.

4. Q: How do you create a new Git repository?

A: To create a new Git repository, you navigate to the desired directory and run the

command: git init

5. Q: How do you clone an existing Git repository?

A: To clone an existing Git repository from a remote location, you run the

command: git clone [repository_url]

6. Q: How do you stage files for commit in Git?

A: To stage files for commit in Git, you use the

command: git add [file_name]

7. Q: How do you commit changes in Git?

A: To commit changes in Git, you run the

command: git commit -m "Commit message"

8. Q: How do you create a new branch in Git?

A: To create a new branch in Git, you use the command: git branch [branch_name]

9. Q: How do you switch to a different branch in Git?

A: To switch to a different branch in Git, you run the

command: git checkout [branch_name]

10. Q: How do you merge branches in Git?

A: To merge branches in Git, you use the

command: git merge [branch_name]

11. Q: How do you push changes to a remote repository in Git?

A: To push changes to a remote repository in Git, you run the

command: git push [remote_name] [branch_name]

12. Q: How do you pull changes from a remote repository in Git?

A: To pull changes from a remote repository in Git, you use the

command: git pull [remote_name] [branch_name]

13. Q: How do you view the commit history in Git?

A: To view the commit history in Git, you run the

command: git log

14. Q: How do you discard local changes in Git?

A: To discard local changes and revert to the last committed state, you use the

command: git checkout -- [file_name]

15. Q: How do you configure your username and email in Git?

A: To configure your username and email in Git, you run the

commands: git config --global user.name "Your Name"

git config --global user.email "yourname@example.com"

16. Q: How do you resolve merge conflicts in Git?

A: When encountering a merge conflict, you need to manually edit the conflicting files to resolve the differences. After making the necessary changes, you stage the files and commit the merge. Git provides markers in the files to indicate the conflicting sections, and it’s important to carefully review and resolve the conflicts to ensure a successful merge.

17. Q: What is the purpose of Git branching?

A: Git branching allows for the creation of separate lines of development, enabling developers to work on different features or bug fixes simultaneously. Branches provide isolation and flexibility, making it easier to manage changes, test new features, and merge them back into the main codebase.

18. Q: How do you create and switch to a new branch in Git in a single command?

A: To create and switch to a new branch in Git in a single

command, you use: git checkout -b [branch_name]

19. Q: How do you tag a specific commit in Git?

A: To tag a specific commit in Git, you run the

command: git tag [tag_name] [commit_id]

20. Q: How do you undo the last commit in Git without losing the changes?

A: To undo the last commit in Git while keeping the changes in your working directory, you use the

command: git reset HEAD~1