Introduction
Git is the starting point for DevOps journey. So, I just outlined 50 quick questions with answers of Git. These questions are really quick, you can ask in DevOps interview. It’s for beginners to intermediate level.
Interview Questions and Answers
1. Q: What is Git?
A: Git is a distributed version control system that allows multiple developers to collaborate on a project and track changes to source code.
2. Q: What is the difference between Git and SVN?
A: Git is a distributed version control system, whereas SVN (Subversion) is a centralized version control system. Git allows offline work and has more advanced branching and merging capabilities.
3. Q: How do you create a new Git repository?
A: To create a new Git repository, you can use the command: `git init`.
4. Q: What is a Git branch?
A: A branch in Git is a lightweight pointer to a specific commit. It allows developers to isolate changes and work on different features or bug fixes concurrently.
5. Q: How do you create a new branch in Git?
A: To create a new branch, you can use the command: `git branch <branch_name>`.
6. Q: How do you switch to a different branch in Git?
A: You can switch to a different branch using the command: `git checkout <branch_name>`.
7. Q: How do you merge branches in Git?
A: To merge branches, you can use the command: `git merge <branch_name>`.
8. Q: What is a Git conflict, and how do you resolve it?
A: A conflict occurs when Git is unable to automatically merge changes from different branches. It requires manual intervention to resolve conflicts by editing the conflicting files and using the `git add` command.
9. Q: How do you discard local changes in Git?
A: You can discard local changes using the command: `git checkout — <file_name>`.
10. Q: What is the purpose of the Git stash command?
A: The `git stash` command allows you to temporarily save changes that are not ready to be committed, so you can switch branches or apply the changes later.
11. Q: How do you apply a stash in Git?
A: You can apply a stash using the command: `git stash apply`.
12. Q: What is Git rebase?
A: Git rebase is used to integrate changes from one branch into another. It moves or combines a sequence of commits to a new base commit.
13. Q: What is the difference between Git fetch and Git pull?
A: Git fetch retrieves the latest changes from a remote repository without merging them, while Git pull fetches and merges the changes into the current branch.
14. Q: How do you undo the last Git commit?
A: To undo the last commit while keeping the changes, you can use the command: `git reset HEAD~1`.
15. Q: How do you revert a commit in Git?
A: To revert a commit and create a new commit that undoes the changes, you can use the command: `git revert <commit_id>`.
16. Q: What is the purpose of the Git reflog?
A: Git reflog is a reference log that stores the history of all branch updates and other changes in the repository. It can be used to recover lost commits or branches.
17. Q: How do you cherry-pick a commit in Git?
A: To apply a specific commit from one branch to another, you can use the command: `git cherry-pick <commit_id>`.
18. Q: How do you squash multiple commits into one in Git?
A: You can squash multiple commits into one using the interactive rebase with the command: `git rebase -i HEAD~<number_of_commits>`.
19. Q: What is a Git tag, and how do you create one?
A: A Git tag is a named reference to a specific commit. You can create a tag using the command: `git tag <tag_name> <commit_id>`.
20. Q: How do you push tags to a remote repository in Git?
A: To push tags to a remote repository, you can use the command: `git push –tags`.
21. Q: How do you delete a branch in Git?
A: To delete a branch, you can use the command: `git branch -d <branch_name>`.
22. Q: What is the purpose of the Git rebase interactive mode?
A: The interactive mode of Git rebase allows you to modify commits during the rebase process, such as squashing, reordering, or editing commit messages.
23. Q: How do you configure a Git username and email?
A: You can configure a Git username and email globally using the commands: `git config –global user.name <your_name>` and `git config –global user.email <your_email>`.
24. Q: How do you list all remote branches in Git?
A: To list all remote branches, you can use the command: `git branch -r`.
25. Q: What is Git bisect used for?
A: Git bisect is a tool used to find the commit that introduced a bug or regression by performing a binary search through the commit history.
26. Q: How do you set up Git to ignore certain files?
A: You can create a file called `.gitignore` in your repository and list the files or patterns you want Git to ignore.
27. Q: How do you rename a branch in Git?
A: To rename a branch, you can use the command: `git branch -m <new_branch_name>`.
28. Q: How do you find the difference between two Git branches?
A: You can compare two branches using the command: `git diff <branch1>..<branch2>`.
29. Q: How do you view the commit history of a Git repository?
A: You can view the commit history using the command: `git log`.
30. Q: How do you configure Git to use a proxy server?
A: You can configure Git to use a proxy server using the commands: `git config –global http.proxy <proxy_url>` and `git config –global https.proxy <proxy_url>`.
31. Q: What is Git rebase -i used for?
A: Git rebase -i allows you to perform an interactive rebase, where you can modify, rearrange, or delete commits before applying them to a different branch.
32. Q: How do you split a commit into multiple commits in Git?
A: You can split a commit into multiple commits using the interactive rebase with the command: `git rebase -i <commit_id>`.
33. Q: How do you sign Git commits?
A: You can sign Git commits using the `-S` flag with the `git commit` command.
34. Q: How do you show the changes introduced by a specific commit in Git?
A: To show the changes introduced by a specific commit, you can use the command: `git show <commit_id>`.
35. Q: How do you revert a Git merge?
A: To revert a Git merge, you can use the command: `git revert -m 1 <merge_commit_id>`.
36. Q: How do you find the author of a specific line in Git?
A: You can find the author of a specific line using the `git blame` command followed by the file path and line number.
37. Q: How do you undo a Git rebase?
A: If you want to undo a Git rebase, you can use the command: `git reflog` to find the commit before the rebase and then use `git reset –hard <commit_id>`.
38. Q: How do you change the last commit message in Git?
A: You can change the last commit message using the command: `git commit –amend`.
39. Q: How do you remove untracked files in Git?
A: You can remove untracked files using the command: `git clean -f`.
40. Q: How do you show the changes between two Git commits?
A: To show the changes between two commits, you can use the command: `git diff <commit1>..<commit2>`.
41. Q: How do you set up Git to use SSH for remote repository access?
A: You can set up Git to use SSH by adding your SSH key to your Git hosting provider and configuring the remote repository URL to use the SSH protocol.
42. Q: How do you list all the files changed in a specific Git commit?
A: You can list all the files changed in a specific commit using the command: `git show –name-only <commit_id>`.
43. Q: How do you unstage files in Git?
A: To unstage files, you can use the command: `git reset HEAD <file_name>`.
44. Q: How do you view the changes between the working directory and the last Git commit?
A: You can view the changes using the command: `git diff HEAD`.
45. Q: How do you show the commit that introduced a specific line in Git?
A: You can use the `git blame` command followed by the file path and line number to find the commit that introduced the line.
46. Q: How do you change the remote repository URL in Git?
A: You can change the remote repository URL using the command: `git remote set-url origin <new_remote_url>`.
47. Q: How do you configure Git to use a specific text editor for commit messages?
A: You can configure the text editor using the command: `git config –global core.editor <editor_name>`.
48. Q: How do you ignore changes to a tracked file in Git?
A: You can ignore changes to a tracked file using the command: `git update-index –assume-unchanged <file_name>`.
49. Q: How do you show the commit history of a specific file in Git?
A: You can show the commit history of a specific file using the command: `git log — <file_path>`.
50. Q: How do you apply only certain commits from one Git branch to another?
A: You can apply specific commits using the interactive rebase with the command: `git rebase -i <commit_id>`. Then choose which commits to keep or discard.
These questions cover various aspects of Git and should help you prepare for Git-related interview. Good luck!