
Basic Git Questions
What is Git and why is it used?
- Answer: Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It is used for tracking changes in source code, enabling multiple developers to collaborate on a project.
Explain the difference between Git and GitHub.
- Answer: Git is a version control system to manage code history, while GitHub is a cloud-based hosting service for Git repositories, providing a web interface for collaboration and project management.
What is a repository in Git?
- Answer: A repository (or repo) is a storage location for your project files and the entire history of their changes. It can be local (on your computer) or remote (hosted on platforms like GitHub).
How do you initialize a new Git repository?
- Answer: You can initialize a new Git repository by running
git initin the root directory of your project.
- Answer: You can initialize a new Git repository by running
How do you clone a repository?
- Answer: You can clone a repository using
git clone <repository_url>. This command creates a copy of the remote repository on your local machine.
- Answer: You can clone a repository using
GIT-NUTSHELL :
Credit @BRIJ_KISHORE_PANDEY
Intermediate Git Questions
Explain the difference between
git fetchandgit pull.- Answer:
git fetchdownloads commits, files, and references from a remote repository into your local repository without merging.git pullfetches and merges the changes from the remote repository into your current branch.
- Answer:
What are Git branches and why are they important?
- Answer: Branches in Git are pointers to a specific snapshot of your changes. They allow you to develop features, fix bugs, or experiment in isolated environments. This makes it easier to manage multiple versions of a project.
How do you create a new branch in Git?
- Answer: You can create a new branch using
git branch <branch_name>and switch to it usinggit checkout <branch_name>or create and switch in one command withgit checkout -b <branch_name>.
- Answer: You can create a new branch using
What is a merge conflict in Git and how do you resolve it?
- Answer: A merge conflict occurs when changes from different branches clash and Git cannot automatically merge them. To resolve it, you need to manually edit the conflicting files and mark them as resolved with
git add <file>.
- Answer: A merge conflict occurs when changes from different branches clash and Git cannot automatically merge them. To resolve it, you need to manually edit the conflicting files and mark them as resolved with
Explain the purpose of the
git stashcommand.- Answer:
git stashtemporarily saves your uncommitted changes so you can switch branches or perform other tasks. You can later reapply these changes usinggit stash poporgit stash apply.
- Answer:
Advanced Git Questions
What is rebasing in Git and how is it different from merging?
- Answer: Rebasing rewrites the commit history by placing your commits on top of another branch, creating a linear history. Merging combines commits from different branches, preserving the history of both branches.
When would you use
git rebaseinstead ofgit merge?- Answer: You would use
git rebaseto maintain a clean, linear project history. It is useful for incorporating changes from another branch before integrating into the main branch to avoid unnecessary merge commits.
- Answer: You would use
How do you undo the last commit in Git?
- Answer: You can undo the last commit with
git reset --hard HEAD~1to remove the commit and changes, orgit reset --soft HEAD~1to remove the commit but keep the changes in the working directory.
- Answer: You can undo the last commit with
Explain the concept of a detached HEAD in Git.
- Answer: A detached HEAD state occurs when you checkout a commit that is not associated with any branch. Changes made in this state are not associated with any branch unless you create a new branch from that commit.
How do you tag a commit in Git?
- Answer: You can tag a commit using
git tag <tag_name>to create a lightweight tag orgit tag -a <tag_name> -m "message"to create an annotated tag with a message.
- Answer: You can tag a commit using
Practical Git Questions
How do you see the commit history for a repository?
- Answer: You can view the commit history using
git log. To see a simplified view, you can usegit log --oneline.
- Answer: You can view the commit history using
How do you revert a commit that has already been pushed to a remote repository?
- Answer: You can revert a commit using
git revert <commit_hash>. This creates a new commit that undoes the changes from the specified commit.
- Answer: You can revert a commit using
What is the difference between
git resetandgit revert?- Answer:
git resetmoves the HEAD to a specific commit and can alter the commit history, potentially losing changes.git revertcreates a new commit that undoes changes from a previous commit without altering the commit history.
- Answer:
How do you configure a Git repository to ignore specific files or directories?
- Answer: You can configure a Git repository to ignore specific files or directories by adding their paths to a
.gitignorefile in the root of the repository.
- Answer: You can configure a Git repository to ignore specific files or directories by adding their paths to a
What is
git cherry-pickand when would you use it?- Answer:
git cherry-pick <commit_hash>applies the changes from a specific commit to your current branch. It is useful for applying selected commits from one branch to another without merging the entire branch.
- Answer:
Git Collaboration Questions
How do you create a pull request on GitHub?
- Answer: To create a pull request on GitHub, push your changes to a branch on the remote repository, then navigate to the repository on GitHub, select the "Pull requests" tab, and click "New pull request". Choose the base branch and the compare branch, then create the pull request.
How do you handle multiple developers working on the same file in Git?
- Answer: Multiple developers can work on the same file by creating separate branches. Changes can be merged later, and any conflicts can be resolved during the merge process.
Explain the concept of Git workflow.
- Answer: A Git workflow defines how teams use Git for version control and collaboration. Common workflows include centralized workflow, feature branch workflow, Gitflow workflow, and fork-and-pull model.
How do you handle large binary files in a Git repository?
- Answer: Large binary files should be handled using Git Large File Storage (LFS) which replaces large files with text pointers inside Git while storing the file contents on a remote server.
What is the difference between
git forkandgit clone?- Answer:
git forkis a GitHub concept where you create a copy of a repository under your own account to work on.git cloneis a Git command that creates a local copy of a remote repository.
- Answer:
Troubleshooting Git Questions
How do you resolve a detached HEAD state in Git?
- Answer: To resolve a detached HEAD state, you can either checkout an existing branch using
git checkout <branch_name>or create a new branch from the detached state usinggit checkout -b <new_branch_name>.
- Answer: To resolve a detached HEAD state, you can either checkout an existing branch using
What are submodules in Git and how do you use them?
- Answer: Submodules are repositories within a repository. You can add a submodule using
git submodule add <repository_url>. This allows you to track external dependencies.
- Answer: Submodules are repositories within a repository. You can add a submodule using
How do you squash commits in Git?
- Answer: You can squash commits using
git rebase -i <base_commit>. Mark the commits you want to squash assquashorsin the interactive rebase interface.
- Answer: You can squash commits using
How do you rename a branch in Git?
- Answer: To rename the current branch, use
git branch -m <new_name>. To rename another branch, usegit branch -m <old_name> <new_name>.
- Answer: To rename the current branch, use
How do you remove a file from the Git history?
- Answer: To remove a file from Git history, you can use
git filter-branch --tree-filter 'rm -f <file>' HEADor use theBFG Repo-Cleanertool for a simpler approach.
- Answer: To remove a file from Git history, you can use
Here are 20 additional Git interview questions with answers for someone with 5 years of experience in DevOps:
Advanced Git Questions
What is the purpose of
git bisectand how does it work?- Answer:
git bisectis used to find the commit that introduced a bug by performing a binary search through the commit history. You start with a known good commit and a known bad commit, and Git will help you find the exact commit where the issue was introduced.
- Answer:
Explain the difference between
git merge --no-ffandgit merge --ff-only.- Answer:
git merge --no-ffcreates a merge commit even if the merge could be performed with a fast-forward. This preserves the branch history.git merge --ff-onlyperforms the merge only if it can be done with a fast-forward; otherwise, it aborts the merge.
- Answer:
What is the purpose of the
.gitkeepfile?- Answer: The
.gitkeepfile is not a built-in Git feature but a convention used to keep empty directories in a Git repository. Since Git does not track empty directories, adding a.gitkeepfile ensures the directory is included in the repository.
- Answer: The
How do you clean up a repository by removing untracked files?
- Answer: You can clean up untracked files using
git clean -fto remove untracked files andgit clean -fdto remove untracked files and directories.
- Answer: You can clean up untracked files using
What is the reflog in Git and how is it useful?
- Answer: The reflog records changes made to the tips of branches and other references. It is useful for recovering commits that are no longer referenced by any branch or tag.
Git Workflow and Collaboration
How do you handle code reviews in Git?
- Answer: Code reviews in Git are typically handled using pull requests on platforms like GitHub, GitLab, or Bitbucket. Developers push their changes to a branch and create a pull request for team members to review, comment, and approve before merging.
What are Git hooks and how are they used?
- Answer: Git hooks are scripts that run automatically at certain points in the Git workflow, such as before a commit or after a push. They are used to enforce policies, automate tasks, and integrate with other tools.
Explain the purpose of the
pre-commithook.- Answer: The
pre-commithook runs before the commit message editor is displayed and can be used to check the commit for compliance with coding standards, run tests, or check for other conditions that might prevent the commit.
- Answer: The
What is a Git workflow you have used in your projects?
- Answer: One common workflow is the Gitflow workflow, which uses feature branches for development, a develop branch for integration, and a master branch for releases. Hotfix branches are used for urgent fixes in the production code.
How do you enforce branch protection rules in a Git repository?
- Answer: Branch protection rules can be enforced using repository hosting services like GitHub, GitLab, or Bitbucket. These rules can include restrictions on who can push to a branch, require pull request reviews, enforce status checks, and more.
Git Configuration and Optimization
How do you configure Git to use a specific text editor?
- Answer: You can configure Git to use a specific text editor by setting the
core.editorconfiguration option. For example,git config --global core.editor "code --wait"sets Visual Studio Code as the default editor.
- Answer: You can configure Git to use a specific text editor by setting the
What is
git gcand when should you use it?- Answer:
git gcstands for "garbage collection" and is used to clean up unnecessary files and optimize the local repository. It should be used periodically to maintain repository performance, especially in large repositories.
- Answer:
How do you manage large files in Git repositories?
- Answer: Large files can be managed using Git LFS (Large File Storage), which replaces large files with pointers in the Git repository while storing the actual files on a remote server.
What is the purpose of the
.gitattributesfile?- Answer: The
.gitattributesfile is used to define attributes for paths in the repository. It can control settings like end-of-line normalization, file diff handling, and custom merge strategies.
- Answer: The
How do you set up Git to ignore file mode changes?
- Answer: You can set up Git to ignore file mode changes by setting the
core.fileModeconfiguration option tofalseusing the commandgit config core.fileMode false.
- Answer: You can set up Git to ignore file mode changes by setting the
Git Best Practices
How do you ensure consistent line endings across different operating systems in a Git repository?
- Answer: Consistent line endings can be ensured by configuring Git to handle line endings using the
core.autocrlfsetting. Setting it totrueon Windows andinputon Unix-based systems ensures consistent line endings.
- Answer: Consistent line endings can be ensured by configuring Git to handle line endings using the
What is a commit message convention you follow and why is it important?
- Answer: A common commit message convention is the Conventional Commits specification, which includes types like
feat,fix, andchorefollowed by a short description. Consistent commit messages improve readability and help automate release notes.
- Answer: A common commit message convention is the Conventional Commits specification, which includes types like
How do you manage multiple remotes in a Git repository?
- Answer: Multiple remotes can be managed by adding remote URLs using
git remote add <name> <url>. You can fetch, pull, and push to these remotes using the remote name.
- Answer: Multiple remotes can be managed by adding remote URLs using
What is the purpose of the
git blamecommand?- Answer:
git blame <file>shows who last modified each line of a file and the commit information, helping to identify the source of changes and understand the history of a file.
- Answer:
How do you secure your Git repository?
- Answer: Security can be enhanced by using SSH keys for authentication, enforcing branch protection rules, using signed commits, and regularly reviewing repository access and permissions.
Troubleshooting and Recovery
How do you recover a deleted branch in Git?
- Answer: A deleted branch can be recovered if it has not been garbage collected by finding its last commit in the reflog (
git reflog) and recreating the branch withgit checkout -b <branch_name> <commit_hash>.
- Answer: A deleted branch can be recovered if it has not been garbage collected by finding its last commit in the reflog (
What is the purpose of
git fsckand when would you use it?- Answer:
git fsckchecks the integrity of the Git repository's database and identifies issues such as corrupted objects. It is useful for diagnosing and fixing repository corruption.
- Answer:
How do you handle a situation where a push is rejected due to non-fast-forward updates?
- Answer: You can handle this situation by pulling the latest changes from the remote repository using
git pull --rebaseto reapply your commits on top of the latest changes, then pushing again.
- Answer: You can handle this situation by pulling the latest changes from the remote repository using
Explain how to revert multiple commits in Git.
- Answer: Multiple commits can be reverted using
git revert <commit1> <commit2> ...to create individual revert commits for each specified commit, or by using an interactive rebase and squashing or editing commits.
- Answer: Multiple commits can be reverted using
How do you manage conflicting changes in a collaborative environment?
- Answer: Conflicting changes can be managed by communicating with team members, using feature branches, performing regular merges or rebases to keep branches up-to-date, and resolving conflicts collaboratively during code reviews.
Advanced Usage
What is Git submodule and how do you use it?
- Answer: Git submodules allow you to include and track external repositories within a parent repository. They are added using
git submodule add <repository_url> <path>, and submodules are initialized and updated withgit submodule update --init.
- Answer: Git submodules allow you to include and track external repositories within a parent repository. They are added using
How do you configure Git to sign commits and why is it important?
- Answer: Commits can be signed using GPG keys by configuring Git with
git config --global user.signingkey <key_id>and signing commits withgit commit -S. Signed commits verify the authenticity and integrity of the commits.
- Answer: Commits can be signed using GPG keys by configuring Git with
What are some common Git workflows and which one do you prefer?
- Answer: Common Git workflows include Gitflow, feature branch, and trunk-based development. Preference depends on the team's needs and project complexity. Gitflow is often preferred for structured release management, while trunk-based is favored for continuous integration.
How do you handle a corrupted Git repository?
- Answer: A corrupted repository can be handled by using
git fsckto diagnose issues, recovering from backups, or recloning the repository if a remote copy exists. Resolving specific corrupt objects might involve replacing or regenerating them.
- Answer: A corrupted repository can be handled by using
How do you migrate a project from another version control system to Git?
- Answer: Migration involves exporting the project history from the existing VCS, using tools like
git svnfor Subversion orgit fast-importfor other systems, and setting up the Git repository with the imported history. Steps vary depending on the source VCS.
- Answer: Migration involves exporting the project history from the existing VCS, using tools like




