Table of Contents
In this article, we will see 72 Important Git Interview Questions and Answers which can be asked from Software Professionals in 2021. Git is a very popular open source code repository used by many small and large scale organizations to manage and deploy their project source code into the respective environments. Git also provide the flexibility to deploy code in multiple kinds of environment. This makes it a very important topic for almost all kind of IT Interviews. Hence it is absolutely necessary for a candidate to be aware of all the git interview questions that can be asked in 2021.
Important Git Interview Questions and Answers[Updated 2021]
1. What is Git ?
Ans. Git is a free and open source distributed version control system used as central repository to store project files in remote Server as well as in local server. Git has excellent set of features to manage files, branches, metadata and references which makes it a widely used repository.
2. How to Resolve Merge Conflicts in Git ?
Ans. To resolve any merge conflicts in git, you need to first check the files which are causing merge conflicts. Then edit those files and do the necessary changes. Add it again by git add command and then commit it by using git commit command to finalize the merge.
3. What are hooks in git ?
Ans. Hooks are programs placed in hooks directory under .git directory path to trigger actions based on git command execution.
4. How to delete a branch in Git ?
Ans. git branch -d <branch_name>
5. How to Switch branch in Git ?
Ans. git checkout <branch_name>
6. How to create git repository ?
7. How to Install Git in Linux ?
Ans. yum install git(CentOS/RHEL/Fedora) or apt-get install git(Ubuntu/Debian)
8. How to stash changes in Git ?
Ans. git stash push
9. How to create a new branch in Git ?
Ans. git branch <new_branch>
10. How to fork a repo in GitHub ?
Ans. You can visit any repository in GitHub and click on fork button on the upper right hand corner of a repo page to fork that repo.
11. What does git fetch do ?
Ans. git fetch command is used to retrieve the latest metadata information including latest commits, references, branches and files from the remote repository leaving your local work intact as it is.
12. What does git pull do ?
Ans. git pull command is used to download content from remote repository and match it with local repository.
13. How to rename a branch in Git ?
Ans. git branch -m <new_name>
14. How to undo git add changes ?
Ans. You can use git reset or git reset <file> command to unstage all the changes before commit.
15. How to check if there is any file available for commit ?
Ans. git status
16. What does rebase do ?
Ans. Rebase is basically changing the base of your branch from one commit to another making it appear as if you'd created your branch from a different commit.
17. What does git stash do ?
Ans. git stash command is temporarily used to save your local work so that you can re-access it later and allows you to switch your branch to perform some other development tasks.
18. How to check all Git Commit IDs ?
Ans. Using git log command
19. Does git pull overwrite local changes ?
Ans. No, it does not overwrite local changes unless explicitly told to do so.
20. When does .git folder gets created ?
Ans. When you run git init command then .git folder gets created.
21. How to remove untracked files in Git ?
Ans. You can use git clean -fx command to remove untracked files from current git branch.
22. What does git checkout do ?
Ans. git checkout command is used to navigate between the branches created by git branch command.
23. What is branch in Git ?
Ans. A branch in a repository represents an independent line of development. It serve as an abstraction for the edit/stage/commit process.
24. How to download a Git Repository ?
Ans. git clone <repo_url>
25. How to merge two branches in Git ?
Ans. git merge <branch_name>
26. How to revert commit in Git ?
Ans. git reset --hard <commit_id>
27. How to remove file from git ?
Ans. You can use git rm <file_name> command to remove a file from git.
28. How to add file to .gitignore ?
Ans. git config --global core.excludesFile ~/.gitignore
29. How to clone a repository in Git ?
Ans. git clone <repo_url>
30. How to check git version ?
Ans. git --version
31. What is HEAD in git ?
Ans. HEAD is like a pointer in git which always point to last commit ID of current checkout branch.
32. What is origin in git ?
Ans. In Git, origin is just like a standard shorthand name for the remote repository that a project was originally cloned from.
33. What does git init do ?
Ans. git init will create a new git repository. It can also be used to convert local directory into git repository.
34. What is git-flow ?
Ans. It is a set of scripts that combine standard git commands to perform multiple tasks automatically and in a predefined order.
35. What does git reset do ?
Ans. git reset command will move current branch HEAD to specific commit ID.
36. What is stash in git ?
Ans. A stash in git is a staging area to save the uncommitted files temporarily so that it can be re-accessed and re-applied later.
37. How to create new branch and immediately switch to it ?
Ans. git checkout -b <new_branch>
38. What is fork in git ?
Ans. A fork is a copy of a repository. It allows to freely experiment with changes without affecting the original project. More on Github docs.
39. How to untrack a file without deletion ?
Ans. git rm --cached filename
40. How to list only the file names that changed between two commits ?
Ans. git diff --name-only <commit1><commit2>
41. How to List information about a commit in Git ?
Ans. git show <commit ID>
42. How to avoid full details of all the Commits and only show useful information ?
Ans. Using --decorate option.
43. How to search a bug introduced by some commit ?
Ans. Using git bisect command
44. How to undo git commit ?
Ans. git reset --hard HEAD~1
45. How to delete Git Repository in Linux ?
Ans. rm -rf <git_folder>/.git
46. What is the default behavior when you do not pass a mode to git reset ?
Ans. Mixed Reset
47. How many reference types are there in Git ?
Ans. There are three reference types in Git:-
48. What is the default folder of Branches Reference type in Git ?
49. What is the default folder of Tags Reference type in Git ?
50. What is the default folder of Remote Reference type in Git ?
51. What does git push do ?
Ans. git push command will push the changes to the remote repository to a specific branch.
52. How to delete a remote branch in Git ?
Ans. git push origin --delete <remote_branch>
53. How to List all the local and remote branches in Git ?
Ans. git branch -a
54. How to List only local branches in Git ?
Ans. git branch
55. How to List only remote branches in Git ?
Ans. git branch -r
56. How to use Git ?
Ans. To save all the project files in a git repo, below steps needs to be performed inside the project root directory :-
a) git init
b) git config --global user.email "Your Email ID"
c) git config --global user.name "Your Name"
d) git add .
e) git commit -m "Add Your Comment here"
57. Which hashing techniques are used in Git to generate IDs ?
Ans. SHA1 or SHA256
58. What are the different states in which a file can exist in Git Repo ?
Ans. There are four different states in which a file can exist in Git Repo:-
59. What is detached HEAD in git ?
Ans. Detached HEAD basically means it is pointing to a Commit ID rather than any branch.
60. What is cherry pick ?
Ans. git cherry-pick is a very powerful command that enables arbitrary git commits to be picked from a branch and apply it to another.
61. How to view all the changes saved in stash ?
Ans. git stash list
62. How to remove stashed changes without applying them ?
Ans. git stash drop <stash_name>
63. How to clear the stash ?
Ans. git stash clear
64. How to apply changes and remove it from stash ?
Ans. git stash pop <stash_name>
65. Which command is called the mixture of git fetch and git merge command ?
Ans. git pull command
66. How to automatically resolve git merge conflicts ?
Ans. git merge --strategy-option theirs or git merge --strategy-option ours
67. What is git reflog ?
Ans. git reflog command keeps a track of all the changes done in a references(i.e on a branch or tag) of a repository.
68. Is it possible to recover a deleted branch from local repository ?
69. How to fetch the log references of a particular branch or tag in Git ?
Ans. git reflog <ref_name>
70. How to prune older reflog entries in git ?
Ans. git reflog expire
71. How to List Commit objects in reverse chronological order ?
Ans. Using git rev-list command
72. How to restore the working tree files with the content from the given tree ?
Ans. Using git restore command