Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
As one of the top source code management systems, Git plays a significant role in various businesses. Today, Git has over 70 million users all around the world.
The most widely used source code management technology is Git. Git's collaborative tools, such as bug tracking, task management, and wikis, will aid you whether you're a coder or a non-technical person.
This article will discuss the most common Git interview questions to help you crack an interview, but before that let us first know about Git in detail.
What Is Git and Why Is It Used?
Git is a distributed version control system used by software developers to manage the changes in the code. It also allows developers to collaborate and work together on their code.
Git works by creating repositories where the changes in the code can be stored. Thus it helps to keep track of the modifications and revert to the previous changes if required very efficiently.
Scope of Git
Git is a distributed version control system that has become an essential tool in modern software development. Its scope encompasses:
Version control: Git tracks changes in source code over time, allowing developers to review history and revert to previous versions if needed.
Collaboration: It enables multiple developers to work on the same project simultaneously, merging their changes seamlessly.
Branching and merging: Git allows developers to create separate branches for different features or experiments, and later merge them back into the main codebase.
Remote repository hosting: Git integrates with platforms like GitHub, GitLab, and Bitbucket, facilitating code sharing and collaboration on a global scale.
Continuous Integration/Continuous Deployment (CI/CD): Git is often a crucial component in automated build and deployment pipelines.
Open-source contribution: Git's distributed nature makes it ideal for open-source projects, allowing contributors from around the world to participate.
Preparation Tips to Crack the Git Interview
To succeed in a Git interview, consider the following preparation tips:
Master the basics: Ensure you have a solid understanding of fundamental Git commands and concepts (commit, push, pull, branch, merge, etc.).
Practice regularly: Use Git in your projects or contribute to open-source repositories to gain hands-on experience.
Understand Git workflows: Familiarize yourself with common Git workflows like GitFlow or GitHub Flow.
Learn advanced features: Study complex Git operations such as rebasing, cherry-picking, and resolving merge conflicts.
Prepare for scenario-based questions: Be ready to explain how you would use Git in various real-world situations.
Know Git best practices: Understand commit message conventions, branching strategies, and code review processes.
Explore Git integrations: Learn how Git works with CI/CD tools and project management platforms.
Study Git internals: Knowing how Git works under the hood can impress interviewers and demonstrate depth of understanding.
Be familiar with Git GUIs: While command-line proficiency is crucial, knowing popular Git GUI tools can be beneficial.
Practice explaining concepts: Be prepared to articulate Git concepts and your problem-solving approach clearly.
Basic GIT Interview Questions
Let's discuss some of the basic Git interview questions with their answers.
1. What exactly is git?
Answer: GIT is a distributed source code management and version control system. (SCM) solution designed to handle both small and big projects quickly and efficiently.
2. In Git, what language is used?
Answer: Git is written in the C programming language. Git is fast, and the 'C' programming language enables this by reducing the overhead of high-level languages' run
times.
3. What is a repository?
Answer: A repository comprises an a.git list that contains all of the catalog's metadata. Git keeps the contents of the .git file secret.
4. What are the steps to initialize a Git repository?
The steps below should be followed to create a Git repository:
Open the terminal window.
Navigate to the directory where the repository is to be created.
Run the command line:
git init
In the current directory, this will make a brand-new directory called.git. All of the repository's Git metadata may be found in this directory.
The repository with new files. The git add command can be used to include files in the repository. For instance, you would issue the following command to add the file README.md to the repository:
git add README.md
Add the modifications to the repository.
Use the git commit command to add the changes to the repository. For instance, you would issue the following command to commit the modifications to the file README.md:
git commit -m "Added README.md"
The commit message is specified by the -m parameter. A succinct summary of the changes you are submitting is provided in the commit message.
Push the updates to a distant repository.
5. What is the purpose of 'GIT PUSH'?
Answer: 'GIT PUSH' is a command that changes remote refs and associated items.
6. What does the word 'Version Control System' mean to you?
Answer: A version control system (VCS) keeps track of any changes made to a file or group of data, allowing you to go back to a specific version if necessary. This ensures that everyone in the team is working on the most recent version of the file.
7. Explain different types of version control systems?
Answer: VCSs are software solutions that assist in managing changes to source code and other files. Centralised and distributed version control systems are the two primary categories.
Utilises a central server for code storage and version control (centralised, or CVCS). Distributed (DVCS): Encourages decentralised collaboration by providing each developer with their own repository with a complete history.
8. Explain the difference between git fetch and git pull?
Answer: Git fetch retrieves changes from a remote repository without merging them. Git pull simultaneously collects updates and merges them into the active branch.
9. How does the use of GIT provide benefits?"
Answer: In software development, using Git has various advantages:
Multiple developers may simultaneously work on the same project. Git assists in conflict resolution and smooth integration of their updates.
Git's version control system keeps track of changes made to your code and lets you go back in time if necessary. It facilitates efficient code history management.
Creating branches in Git lets you work on modifications while the main codebase remains stable while you add new features or address bugs.
Git makes it possible for remote repositories to be hosted on websites like GitHub and GitLab, which makes it easier for scattered teams to collaborate.
10.What is the purpose of the git pull origin master?
Answer: The command git pull origin master gets all changes from the master branch and merges them into the local branch.
11. What is the purpose of the git clone command?
Answer: The git clone command copies a Git repository that already exists. Cloning is the most popular method used by programmers to obtain a copy of a central repository.
12. What is the purpose of 'git add'?
Answer: 'git add' adds updates to your index from your existing directory.
13. What is the purpose of the command 'git reset'?
Answer: 'Git Reset' restores your index and working directory to the state they were in before your previous commit.
14. What exactly are Git's 'hooks'?
Answer: This directory contains Shell scripts that are run after the related Git commands are run.
15. What is the purpose of the command 'git log'?
Answer: 'git log' is used to discover individual commits in your project history by author, date, content, or history.
16. What is the purpose of the git clone?
Answer: One may use a Git clone to make a local copy of a remote GitHub repository. After you clone a repo, you may make changes locally on your PC rather than in the remote repo's source files.
17. What is GitHub?
Answer: A web-based platform called GitHub is used to host and collaborate on software development projects that use the Git version control system. It offers resources for hosting code, working together, tracking issues, and more. For a range of software development projects, from tiny personal endeavours to expansive open-source initiatives and enterprise-level systems, developers, teams, and organisations of all sizes use GitHub. It promotes collaboration, code sharing, and community-driven development and plays a key part in contemporary software development practises.
18. Explain the difference between Git and GitHub?
Answer:
GitHub is a service, whereas Git is a tool.
While GitHub serves as the server for code repositories, Git is utilised to manage them.
GitHub offers a GUI, whereas Git is a command-line programme.
While GitHub is cloud-based, Git is either self-hosted or third-party.
19. What happens if you delete the .git directory?
Answer: If the .git/ directory gets deleted, you will lose track of your project's history. Version control will be removed from the repository.
20. In Git, what command creates a Commit Message?
Answer: The command git commit -m "commit message" is used to send a message to a git commit. A commit message is supplied using the flag m.
21. Give some examples of Git's fundamental operations.
Answer: The following are some fundamental Git operations:
Initialize
Add
Commit
Push
Pull
22. Give some examples of Git's advanced operations.
Answer: Some examples of Git’s advanced operations are:-
Git is-tree: It creates a tree object with the mode and names of all elements, as well as the blob or tree's SHA-1 value.
Git bisect: The Git bisect command aids in the identification of problematic commits.
Git rebase: Git rebase works in a similar way as git merge. With one exception, it merges two branches into a single branch. The command git rebase rewrites the commit history.
23. What is the functionality of git ls-tree?
Answer: The contents of a tree object in a Git repository are listed by the git ls-tree command. References to files and other subdirectories inside a directory are contained in a tree object that represents that directory in the Git repository.
Answer: A tool called Git bisect can assist you in identifying the commit that caused a bug or regression in your code. It operates by progressively reducing the number of commits that could have potentially introduced the problem.
Determine a bad commit and a good commit before using git bisect. The bug-free commit is the good commit, whereas the bad commit is the one that caused the bug.
Once you have determined which commits were harmful and which were good, use the following command to begin bisecting:
"
git bisect start
"
25. What is meant by Git merge conflict?
Answer: When two commits to a Git repository modify the same lines of code, a Git merge conflict happens. Git encounters a merge conflict when it tries to reconcile the changes between the two commits automatically.
26. What is the functionality of git clean command?
Answer: Untracked files and directories are removed from a Git repository's working directory using the git clean command. Untracked files are those that are absent from the Git repository or index.
27. When should you use the git config command?
Answer: The git config command is used to configure your Git installation's configuration parameters. For example, after downloading Git, run the config instructions following to set up your login and commit email address in Git:
Answer: SubGit is a tool that allows you to migrate from SVN to Git. TMate is the firm that created it. It transforms SVN repositories to Git, allowing you to work on both systems
simultaneously. It automatically syncs SVN with Git.
29. How do you resolve a conflict in Git?
Answer: If you need to resolve a conflict with Git, update the list to make the necessary changes, then execute "git add" to add the resolved directory, and then "Git commit" to commit the repaired merge.
30. Why is GIT superior to Subversion?
Answer: GIT is an open-source version control framework that allows you to execute
'adaptations' of a job, which show the changes to the code over time and allows you to go back and repair those changes if necessary. Changes may be checked out and transferred by several developers, and each modification can subsequently, be ascribed to a specific developer.
Intermediate GIT Interview Questions
31. Why is branching required in GIT?
Answer: You may retain your branch and hop between the different branches with the aid of branching. You may access your previous work while leaving your current work intact.
32. Mention the numerous hosting options for Git repositories.
Answer: The Git repository hosting functions are as follows:
Visual Studio Online
Github
SourceForge.net
Bitbucket
33. What does the git status command do?
The git status command displays the current status of the working directory. It shows which files are in a modified, staged, or committed state.
34. Tell me something about git stash?
The git stash command is commonly used to save the changes made in the file temporarily without committing them to the repository such that the developer can work on other features by switching branches.
35. Can you give the differences between “pull request” and “branch”?
In Git, a pull request is a way to merge the changes made in the branch with the main code. In contrast, a branch is a copy of the main code that allows developers to work independently of the master branch.
36. What is the difference between 'git merge' and 'git rebase'?
Both are used to integrate changes from one branch into another, but they work differently:
git merge creates a new commit that combines the changes from both branches.
git rebase moves the entire feature branch to begin on the tip of the main branch, effectively incorporating all new commits.
Merge preserves history, while rebase creates a linear history.
37. How do you resolve merge conflicts in Git?
Merge conflicts occur when two branches modify the same lines of code. To resolve, open the conflicted file, manually merge changes, and then use git add <file> to mark the conflict as resolved before committing.
38. How do you undo the last commit in Git?
To undo the last commit while keeping the changes:
git reset --soft HEAD~1
To undo the last commit and discard the changes:
git reset --hard HEAD~1
39. What are Git tags, and how are they different from branches?
Git tags are pointers to specific commits, often used to mark release versions. Unlike branches, tags do not change over time; they remain associated with the same commit.
40. What is a detached HEAD state in Git?
A detached HEAD state occurs when you check out a specific commit, tag, or remote branch instead of a local branch. In this state, any new commits you make will not belong to any branch and may be lost once you switch to another branch.
41. How can you list all Git tags in a repository?
You can list all tags in a repository using the git tag command.
42. How do you create and delete a tag in Git?
To create a tag:
git tag <tagname>
To delete a tag:
git tag -d <tagname>
43. What is a bare repository in Git?
A bare repository is a repository that contains no working directory. It’s mainly used as a remote repository for collaboration. In a bare repo, you cannot modify files directly.
44. What is the purpose of .gitignore file?
The .gitignore file specifies intentionally untracked files that Git should ignore. This is useful for excluding build artifacts, temporary files, or sensitive information from being tracked by Git.
45. How do you apply a patch in Git?
You can apply a patch created by git diff using git apply <patch-file>. Alternatively, if it’s a commit, use git am <patch-file>.
46. How can you temporarily store changes without committing them in Git?
You can use Git stash:
git stash
This saves your changes and reverts to the last commit. To apply the stashed changes later:
git stash pop
47. What is Git reflog, and how is it used?
git reflog is a mechanism to record changes to the tip of branches and other references. It allows you to recover commits even after they’ve been deleted or lost.
48. What is Git cherry-pick?
Git cherry-pick is used to apply a specific commit from one branch to another. The syntax is:
git cherry-pick <commit-hash>
49. How can you view the commit history of a Git repository?
You can view the commit history by running git log. To see a more concise history, use git log --oneline.
50. How do you squash multiple commits into one?
You can use interactive rebase:
git rebase -i HEAD~n
Where n is the number of commits you want to squash, in the interactive mode, replace 'pick' with 'squash' for the commits you want to combine.
51. What does git reset do, and what are its different modes?
git reset changes the state of the repository to a previous commit. There are three modes:
--soft: Resets only the HEAD, keeping all changes in the working directory.
--mixed: Resets the HEAD and index (staging area), keeping changes in the working directory.
--hard: Resets everything, including the working directory.
52. How do you rename a local and remote branch in Git?
To rename a local branch:
git branch -m <old-name> <new-name>
To rename a remote branch, you need to delete the old one and push the new one:
53. How do you move commits from one branch to another in Git?
To move commits to another branch, use git checkout <target-branch> and then git cherry-pick <commit-hash> for each commit you want to move.
54. What is Git submodule?
Git submodules allow you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate.
55. What is the difference between git rebase and git cherry-pick?
git rebase re-applies a series of commits from one branch onto another, rewriting the history, while git cherry-pick applies individual commits onto the current branch without rewriting history.
56. How do you revert a pushed commit in Git?
To revert a pushed commit, use git revert <commit-hash>. This creates a new commit that undoes the changes introduced by the specified commit.
57. What is git remote, and how can you add a remote repository?
git remote manages the connections to remote repositories. To add a new remote repository, use git remote add <name> <URL>.
58. What is the difference between 'git reset' and 'git revert'?
git reset moves the branch pointer to a different commit, potentially discarding commits.
git revert creates a new commit that undoes the changes made in a previous commit.
59. What is the use of git log --graph?
git log --graph displays the commit history in a graphical form, showing how different branches have diverged and merged.
60. How do you amend the most recent commit message?
You can use:
git commit --amend
This opens your default editor where you can modify the commit message.
Advanced GIT Interview Questions
61. How do you remove sensitive data from Git’s history?
To remove sensitive data, you can use the git filter-branch command or BFG Repo-Cleaner. After rewriting the history, you need to force-push (git push --force) the updated history to the remote repository.
62. What is Git LFS?
Git Large File Storage (LFS) replaces large files such as audio samples, videos, datasets, and graphics with text pointers inside Git, while storing the file contents on a remote server.
63. How can you stage parts of a file in Git?
To stage only parts of a file, use git add -p. This will interactively allow you to choose which parts (hunks) of the file to stage.
64. What does the git diff command do?
git diff shows the differences between two commits, the working directory and the index, or the index and the last commit.
65. How do you create a shallow clone in Git?
A shallow clone is a clone with a limited history. You can create one using:
git clone --depth=1 <repository-url>
This clones only the latest commit.
66. What is the difference between git clone and git fork?
git clone creates a local copy of a repository. git fork (typically done on platforms like GitHub) creates a personal copy of someone else’s repository under your account, allowing you to freely modify it.
67. What is the difference between 'git stash pop' and 'git stash apply'?
git stash pop applies the stashed changes and then removes them from the stash.
git stash apply applies the stashed changes but keeps them in the stash.
68. How can you view all branches that contain a specific commit?
You can view all branches containing a specific commit using git branch --contains <commit-hash>.
69. What is Git worktree?
Git worktree allows you to have multiple working trees attached to the same repository. This is useful for working on different branches simultaneously without switching or stashing.
70. How do you create an annotated tag in Git?
To create an annotated tag, use git tag -a <tag-name> -m "<message>". This tag stores additional information such as the tagger name and message.
71. What is Git blame?
Git blame shows what revision and author last modified each line of a file. It's used like this:
git blame <filename>
72. How do you handle a situation where you accidentally commit to the wrong branch?
You can move the commit to the correct branch by first creating a new branch using git checkout -b <branch-name>, then deleting the commit from the wrong branch using git reset --hard HEAD~1.
73. What is the purpose of Git rerere?
Git rerere (reuse recorded resolution) allows Git to remember how you resolved a merge conflict so that it can automatically resolve it in the future.
74. How does Git's garbage collection work?
Git's garbage collection is an automatic process that removes unnecessary objects and optimizes the repository. It's triggered automatically but can be manually run with git gc. It compresses file revisions and removes unreachable objects. The process includes:
Repacking loose objects into pack files
Removing unreferenced objects
Pruning reflog entries older than the expiration date
75. What does the git gc command do?
git gc stands for "garbage collect" and is used to clean up unnecessary files and optimize the local repository by compressing file objects and cleaning unreachable commits.
76. What is the Git object model?
Git's object model consists of four main types of objects:
Blob: Stores file data
Tree: Represents a directory structure
Commit: Represents a specific point in the project's history
Tag: Assigns a name to a specific commit
77. What is the difference between git merge --squash and git merge?
git merge --squash combines all the changes from the merged branch into a single commit but does not create a merge commit. git merge retains the entire commit history, creating a merge commit.
78. How can you use Git namespaces?
Git namespaces allow you to have multiple sets of refs (like branches or tags) in a single repository. This is useful for hosting multiple forks of a project in a single repository. You can use namespaces with:
To track changes in a submodule, navigate to the submodule directory and commit changes as you would in any normal Git repository. Then commit the updated submodule reference in the parent repository.
80. How do you create a Git hook, and what is its purpose?
Git hooks are custom scripts that run automatically when specific events occur, such as before a commit or after a push. To create a Git hook, place a script inside the .git/hooks directory and make it executable.
81. What is the difference between a bare and non-bare repository?
A bare repository contains only the Git revision history, without a working tree. It's typically used as a central repository that developers push to and pull from. A non-bare repository includes both the revision history and a working tree, and is what developers typically use for their local copies.
82. What is the difference between git stash save and git stash push?
git stash save is an older way to save changes, while git stash push is the newer syntax that allows you to name a stash using the -m option. Both commands save uncommitted changes.
83. How does Git handle large binary files efficiently?
Git isn't optimized for large binary files by default, but it offers several strategies:
Git LFS (Large File Storage): Stores large files separately from the main repository
Git Annex: Similar to LFS, but with more flexible storage options
Partial clones: Allow cloning only a subset of a repository's content
84. How can you mirror a repository in Git?
To mirror a repository, you can use git clone --mirror <repo-url>. This creates a bare repository that can be used as an exact copy, including all refs and branches.
85. What is Git's internal directory structure?
Git's internal directory structure (in .git) includes:
objects/: Contains all the content for your database
refs/: Holds pointers into commit objects (branches, tags, remotes)
HEAD: Points to the currently checked-out branch
config: Project-specific configuration options
hooks/: Client or server-side hook scripts
86. What is the git prune command used for?
git prune is used to remove unreachable objects (commits, blobs, etc.) from the object database. These objects may exist due to orphaned commits or deleted branches.
87. How does Git's packfile format work?
Git's packfile format is an efficient way to store objects:
It compresses similar objects together
It stores deltas between similar objects instead of full copies
It uses zlib compression on the packed data
Packfiles are created during garbage collection or when pushing to a remote.
88. How do you handle a corrupted Git repository?
To handle a corrupted Git repository, you can run git fsck to check for any issues in the repository. Depending on the output, you may also use git reflog or git gc to recover lost or orphaned commits.
89. How can you use Git's rev-list command for repository analysis?
git rev-list is a powerful command for analyzing commit history. Some uses include:
Counting commits: git rev-list --count HEAD
Finding commits within a date range: git rev-list --since="1 week ago" --until="yesterday" HEAD
Listing commits that touched a specific file: git rev-list HEAD -- path/to/file
90. What are the security implications of Git hooks?
Git hooks can pose security risks:
They can execute arbitrary code when certain Git operations are performed
Malicious hooks can be inadvertently introduced when cloning a repository
Hooks are not version controlled by default, making them hard to audit
It's important to review hooks, especially in shared environments.
GIT MCQ
1. What does the git clone command do?
A. Creates a new repository
B. Downloads a repository from GitHub
C. Copies an existing repository
D. Pushes changes to the remote repository
Answer: C. Copies an existing repository
2. Which command is used to view the commit history?
A. git status
B. git log
C. git diff
D. git commit
Answer: B. git log
3. What is the default branch name in Git?
A. master
B. main
C. develop
D. trunk
Answer: B. main
4. What does the git stash command do?
A. Stashes the commit history
B. Saves uncommitted changes
C. Reverts changes in the working directory
D. Pushes changes to the remote
Answer: B. Saves uncommitted changes
5. How do you delete a branch in Git?
A. git branch --delete <branch>
B. git remove <branch>
C. git delete <branch>
D. git branch -d <branch>
Answer: D. git branch -d <branch>
6. What is the command to create a new Git repository?
A. git init
B. git create
C. git new
D. git start
Answer: A. git init
7. Which command stages all changes in Git?
A. git add *
B. git stage
C. git commit -a
D. git push --all
Answer: A. git add *
8. What does the git checkout command do?
A. Commits changes
B. Switches branches
C. Stages files for commit
D. Updates the repository
Answer: B. Switches branches
9. How do you set up a new remote for a repository?
A. git remote add origin <url>
B. git set-url origin <url>
C. git config remote <url>
D. git add-remote <url>
Answer: A. git remote add origin <url>
10. What is the purpose of the .gitignore file?
A. To ignore remote repositories
B. To ignore files from being tracked
C. To ignore branches
D. To ignore specific commit messages
Answer: B. To ignore files from being tracked
Frequently Asked Questions
Q. Can we use Git without GitHub?
Yes, Git is usable independently of GitHub. Git is a distributed version control system, therefore the repository doesn't need to be kept on a single server. This means that even if you don't have internet access, you can still use Git to track your code changes.
Q. What are the scenario-based questions for Git?
Scenario-based questions related to Git may include questions like You are working on a branch, and you find a bug in the main code, and you have to fix it before other developers can continue their work. What should you do?
Q. Which language is used in Git?
Git can be used with any programming language as it does not have any language-specific dependencies. The main languages of Git include C, C++, Java, Javascript, Typescript, PHP, Python, Ruby, Scala, etc.
Q. What is the main object of Git?
The main object of Git is to manage the source code. It makes the work of the developers more efficient and productive by helping them collaborate and work together. The main object of Git is the commit object, with the help of which a snapshot of the code is taken.
Q. What are the two basic jobs of Git?
Git offers many advantages to developers. Its two basic jobs are: it allows users to track the changes in the source code, and secondly, it allows developers to collaborate with each other.
Q. Which protocol is used by Git?
Git supports several protocols for smooth communication between local and remote repositories. Some important protocols used by Git are local, HTTP, Git, and secure shell.
Conclusion
In this article, we discussed the most asked Git interview questions. Mastering Git is crucial for any software developer in today's collaborative and fast-paced development environment. This article has covered a wide range of Git interview questions, from basic concepts to advanced techniques, providing you with a solid foundation to showcase your Git expertise during interviews.