31 Git Interview Questions and Answers

With the rise of distributed and remote teams in software development, version control systems like Git have become essential for collaboration and code management.

As such, having a solid grasp of Git and knowing Git interview questions is crucial for landing developer, SRE, or DevOps jobs and acing technical interviews.

Interviewers frequently ask Git-related questions to assess candidates’ proficiency with basic Git commands, branching and merging workflows, debugging common issues, and best practices.

Git Interview Questions

Table of Contents

Top 25 Git Interview Questions

Q1. What is Git?


A1. Git is an open-source distributed version control system that tracks changes in any set of files, coordinates work on those files among multiple people and machines, and helps developers collaborate efficiently on software projects.

Q2. What are some basic Git commands?


A2. git init, git clone, git add, git commit, git push, git pull, git branch, git checkout, git merge, git rebase, git log, git status, git diff.

Q3. How do you initialize a Git repository?


A3. Use the “git init” command in the directory you want to make into a repository. This creates a .git subdirectory that contains all the Git metadata for tracking changes.

Q4. What is the difference between git fetch and git pull?


A4. “git fetch” downloads remote changes but doesn’t integrate them with your local repository. “git pull” fetches remote changes and immediately tries to merge/rebase them into your local branch.

Q5. How do you create a branch in Git?


A5. Use the “git branch ” command. This creates a new branch locally based on the currently checked-out commit. To switch to the new branch, use “git checkout “.

You can also use “git checkout -b <new-branch>” to create a new branch and switch to that new branch.

Q6. How do you rename a branch in Git?


A6. Use the “git branch -m” command to rename a branch.

Q7. Explain the difference between Git and SVN.

A7. Git is distributed, allowing developers to work offline and independently. SVN, on the other hand, is centralized, requiring constant connection to a server.

Q8. How do you squash multiple commits into one commit in Git?


A8. Use an interactive rebase (“git rebase -i“) to squash commits by replacing “pick” with “squash” or “s” next to the commits you want to combine.

Q9. What is the difference between ‘git merge’ and ‘git rebase’?


A9. “git merge” creates a merge commit each time changes from different branches are integrated. This can lead to a branching/merging commit history over time.

git rebase” moves a feature branch onto a new base commit, creating a linear commit history by essentially replaying the changes in the feature branch.

Q10. How do you resolve merge conflicts in Git?


A10. Manually edit the conflicted files to select your desired changes, then add/commit the resolved merge. You can use a merge tool like KDiff3 to simplify conflict resolution.

Q11. What is a Git stash? How do you stash uncommitted changes?


A11. Git stash temporarily shelves uncommitted changes in your working directory so you can switch branches/contexts without having to commit the changes.

Use “git stash save” to stash changes, and “git stash pop” to restore them later.

Q12. How do you undo the last commit in Git?


A12. Use “git reset HEAD1” to undo the last commit but keep changes made. To completely erase the commit as if it never happened, use “git reset –hard HEAD1”.

Q13. What is the difference between ‘git reset’ and ‘git revert’?


A13. “git reset” removes commits from the current branch, altering history. “git revert” creates a new commit that inverts an earlier commit – the original commits remain part of the branch’s history.

Q14. How do you undo changes made to your working directory in Git?


A14. To discard unstaged changes: “git checkout .” or “git checkout — “. To unstage staged changes: “git reset” or “git revert “.

Q15. What is the Gitflow workflow?


A15. Gitflow is a workflow model based on the project release cycle. It uses dedicated feature and release branches stemmed off a production main branch for parallel development and easier tracking of what code will ship with each planned release.

Q16. Explain what remote repositories are in Git.


A16. Remote repositories serve as remote counterparts of local repositories that enable collaboration across distributed development teams. Remote repos can live on various hosting services like GitHub or your own internal servers.

Q17. How do you revert a commit that has already been pushed to remote in Git?


A17. 1) Revert the commit locally using “git revert “, which creates a new commit undoing the original 2) Force push the branch to remote (“git push -f”) so remote matches local. Others may need to rebase their work after the forced update.

Q18. What is cherry-picking in Git? When would you use it?


A18. Cherry-picking lets you selectively apply commits from one branch into another branch. This allows taking a few commits rather than whole branches/merges.

Reasons to cherry-pick: hotfixes, porting commits between long-running release branches, reverting local commits.

Q19. What is the difference between ‘git clone’ and ‘git fork’?


A19. “git clone” copies an existing Git repository usually from some central source. “git fork” forks a repository into your own GitHub account to independently develop features and possibly contribute back updates via pull requests.

Check in detail here.

Q20. How can you find a list of files changed in a specific commit?


A20. Use “git diff-tree -r {hash}” where {hash} is the commit hash you want to inspect. This compares the commit to its parent commit, listing files changed.

Q21. What does ‘git config –global user.name’ and ‘git config –global user.email’ do?


A21. These commands set the username and email address respectively to be associated with your Git commits.

They don’t have to match your actual name/email, but should be consistent across a developer’s projects.

Q22. You did some work on your feature branch, but it’s incomplete. You have to switch contexts and work on something else. How do you save the work?


A22. Use “git stash” as mentioned previously to temporarily shelf uncommitted changes from the working directory and allow clean switching branches.

Q23. How are remote Git repositories updated after new commits are made locally?


A23. Use the “git push” command to send new local commits to the corresponding branch on the remote repository.

Git will report back if the push was rejected due to remote changes not being pulled down locally.

Q24. Describe some best practices for Git commit messages.


A24. Commit messages should have a short, meaningful summary under 50 characters followed by a blank line and then longer description explaining the why and details.

Use present tense imperative mood. Capitalize properly and avoid punctuation like periods.

Q25. How can you configure multiple Git identities based on different projects?


A25. The “git config –local” command can apply Git configuration changes just to an individual repository.

Set user.name/user.email locally per project without globally overriding your default identity.

Q26. How do you tag a specific commit in Git?

A26. To tag a commit, use the command: git tag -a <tag_name> <commit_id>. It allows you to mark specific points in the project history.

Q27. How can you view the commit history in Git?

A27. To view the commit history, use: git log. It displays a list of commits with relevant information.

Q28. Explain the purpose of ‘git blame’ command.

Git blame displays the commit history for each line of a file, helping identify when and by whom changes were made.

Q29. What is the ‘detached HEAD’ state in Git?

The ‘detached HEAD‘ state occurs when you checkout a specific commit, rather than a branch. It allows you to explore historical commits without modifying branches.

Q30. What is the purpose of ‘.gitignore’ file?

The .gitignore file specifies files and directories to be ignored by Git, preventing them from being tracked and committed.

Q31. What is the command you can use to write a commit message?

Use “git commit” command. Example: git commit -m "Your message here"

Conclusion


Whether you’re actively seeking developer or administrator roles or assessing team members’ skills, having mastery over topics like the key Git interview questions above is invaluable.

If you are looking for more Git interview questions or Git tutorials, subscribe to our website.

Buy me a coffeeBuy me a coffee

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.