Welcome to our Git Interview Questions and Answers Page!

Here you’ll find a compilation of commonly asked questions and well-structured answers to help you ace your Git interviews. Whether you’re a beginner or an experienced developer, this resource will provide you with valuable insights and knowledge to enhance your skills in Git version control.

Top 20 Basic Git interview questions and answers

1. What is Git?
Git is a distributed version control system used for tracking changes in source code during the software development process.

2. What is a repository in Git?
A repository in Git is a storage space where all the files, directories, and version history of a project are stored.

3. How do you create a new Git repository?
To create a new Git repository, you can use the command “git init” followed by the name of the directory you want to initialize as a Git repository.

4. How do you add files to a Git repository?
To add files to a Git repository, you can use the command “git add ” to add a specific file or “git add .” to add all the files in the current directory.

5. How do you commit changes in Git?
To commit changes in Git, you can use the command “git commit -m ” where is a brief description of the changes made.

6. How do you create a new branch in Git?
To create a new branch in Git, you can use the command “git branch ” followed by the name of the new branch.

7. How do you switch between branches in Git?
To switch between branches in Git, you can use the command “git checkout ” where is the name of the branch you want to switch to.

8. What is the difference between “git pull” and “git fetch”?
“git pull” fetches changes from a remote repository and merges them into the current branch, while “git fetch” only fetches the changes without merging.

9. How do you merge branches in Git?
To merge branches in Git, you can use the command “git merge ” where is the name of the branch you want to merge into the current branch.

10. How do you resolve merge conflicts in Git?
To resolve merge conflicts in Git, you need to manually edit the conflicting files to remove the conflict markers and choose the desired changes. After resolving the conflicts, you can use the command “git add ” to mark the conflicts as resolved.

11. What is a remote in Git?
A remote in Git refers to a version of the repository that is hosted on a different server. It allows multiple developers to work on the same project and push/pull changes to/from a shared repository.

12. How do you clone a Git repository?
To clone a Git repository, you can use the command “git clone ” where is the URL of the repository you want to clone.

13. What is a Git stash?
A Git stash allows you to save changes that have been made to a branch without committing them. It allows you to switch to a different branch without losing your changes.

14. How do you apply a Git stash?
To apply a Git stash, you can use the command “git stash apply” to apply the most recent stash, or “git stash apply ” to apply a specific stash.

15. How do you undo the last Git commit?
To undo the last Git commit, you can use the command “git revert HEAD~1” which creates a new commit that undoes the changes made in the last commit.

16. How do you remove a file from a Git repository?
To remove a file from a Git repository, you can use the command “git rm ” followed by a commit to permanently delete the file from the repository.

17. How do you rename a file in Git?
To rename a file in Git, you can use the command “git mv ” followed by a commit to update the file name in the repository.

18. What is the difference between Git and SVN?
Git is a distributed version control system, whereas SVN (Subversion) is a centralized version control system. In Git, each developer has a local copy of the entire repository, while in SVN, developers only have access to their own branch.

19. How do you view the commit history in Git?
To view the commit history in Git, you can use the command “git log” which displays a list of commits, including the commit message, author, and timestamp.

20. How do you revert a Git commit?
To revert a Git commit, you can use the command “git revert ” where is the ID of the commit you want to revert. It creates a new commit that undoes the changes made in the specified commit.

Top 20 Advanced Git interview questions and answers

1. What is Git and how does it work?
Git is a distributed version control system that allows multiple developers to work on a project simultaneously. It tracks changes made to files, facilitates collaboration, and provides the ability to revert to previous versions of the project if needed.

2. What is the difference between Git merge and Git rebase?
Git merge combines changes from different branches into one, preserving the commit history. Git rebase moves or combines a sequence of commits to a new base commit, resulting in a linear commit history.

3. What is the purpose of git cherry-pick?
Git cherry-pick allows you to copy individual commits from one branch to another. It is useful when you want to apply a specific commit from another branch without merging the entire branch.

4. How can you undo the last commit?
The command “git revert HEAD” can be used to undo the last commit while preserving the commit history. It creates a new commit that undoes the changes made in the last commit.

5. What is the meaning of the term “remote” in Git?
In Git, a remote refers to a version of the repository that is hosted on a different machine or server. It allows multiple developers to work on the same project by pushing and pulling changes to and from the remote repository.

6. What is the purpose of the command “git stash”?
The “git stash” command allows you to temporarily save changes that you don’t want to commit yet. It creates a stash that can be reapplied later, allowing you to switch branches or perform other operations without committing the changes.

7. How can you rename a branch in Git?
The command “git branch -m ” can be used to rename a branch in Git. Alternatively, you can use “git branch -m ” while on the branch you want to rename.

8. What is the purpose of Git hooks?
Git hooks are scripts that are triggered before or after specific events in Git. They allow you to automate certain tasks or enforce custom workflows. Examples of Git hooks include pre-commit, post-commit, pre-push, etc.

9. How can you revert a specific commit in Git?
The command “git revert ” can be used to revert a specific commit in Git. It creates a new commit that undoes the changes made in the specified commit while preserving the commit history.

10. What is a “detached HEAD” state in Git?
A “detached HEAD” state occurs when you checkout a specific commit instead of a branch in Git. In this state, any new commits you make will not be part of any branch, making it temporary. It is often used for inspection or experimentation purposes.

11. How does Git handle merge conflicts?
When Git encounters a merge conflict, it notifies the user and marks the conflicting lines in the affected files. The user must manually resolve the conflicts by editing the files, choosing which version of the conflicting code to keep, and then committing the changes.

12. What is a Git submodule?
A Git submodule is a repository added as a subdirectory of another repository. It allows you to incorporate an external repository into your project, making it easy to reference specific versions of the external repository.

13. What is the purpose of the .gitignore file?
The .gitignore file is used to specify files and directories that should be ignored by Git. It prevents these files from being tracked or staged, making it useful for excluding build artifacts, temporary files, and other files that should not be committed.

14. How can you squash multiple commits into one?
The command “git rebase -i HEAD~n” can be used to squash the last n commits into one. It will open an interactive rebase where you can choose how to modify or combine the commits before finalizing the changes.

15. How can you modify the message of the last commit?
The command “git commit –amend” can be used to modify the message of the last commit. It opens an editor where you can change the commit message before saving and closing the editor.

16. What is the difference between Git pull and Git fetch?
Git pull is a combination of Git fetch and Git merge. It fetches the changes from a remote repository and automatically merges them into the current branch. Git fetch, on the other hand, only fetches the changes without merging them, allowing you to review and merge them manually.

17. How can you create a new branch in Git?
The command “git branch ” can be used to create a new branch in Git. Alternatively, you can use “git checkout -b ” to create a new branch and switch to it at the same time.

18. How does Git handle binary files?
Git treats binary files as blobs and stores their changes as a whole instead of individual lines. This means that Git cannot show a visual diff for binary files and stores the entire file for each commit that modifies it.

19. What is the purpose of the command “git bisect”?
The “git bisect” command is used for binary searching through the commit history to find a specific commit that introduced a bug. By marking known “good” and “bad” commits, Git can help narrow down the problematic commit.

20. How can you resolve conflicts in a Git rebase?
During a Git rebase, conflicts can occur when Git tries to apply changes from one branch onto another. To resolve conflicts, you need to manually edit the affected files, remove conflict markers, choose the desired changes, and then continue the rebase using the command “git rebase –continue”.

Interview Questions and answers