Git Questions and Answers
Git is a critical tool in modern software development, and understanding its concepts is essential for developers. In this article, we'll explore 30+ common Git interview questions and answers that can help you prepare for your next technical interview.
What is Git?
Git is a distributed version control system (VCS) that allows multiple developers to track and manage changes in the codebase. It helps in coordinating work among team members and tracks the history of every change made.
What are the main features of Git?
- Distributed: Each developer has a local copy of the entire repository.
- Performance: Git is fast and efficient for both small and large projects.
- Branching and Merging: Git makes branching and merging easy and efficient.
- Data Integrity: Git ensures that the data remains unchanged.
- Support for non-linear development: Git allows the use of branches and merging.
Explain the difference between Git and GitHub.
Git is a version control system that manages code history, while GitHub is a hosting service for Git repositories. GitHub provides additional collaboration features like issue tracking, pull requests, and wikis.
What is a Git repository?
A Git repository is a storage space where your project files and their history are stored. It can be local or remote.
What is the difference between a local and a remote repository?
- Local Repository: The repository stored on your local machine.
- Remote Repository: The repository hosted on a remote server like GitHub, GitLab, or Bitbucket.
How do you create a new Git repository?
To create a new Git repository, navigate to your project directory and run:
git init
This command initializes a new Git repository in the current directory.
What is a commit in Git?
A commit in Git is a snapshot of your changes. It's a way to record what you’ve done in your project at a specific point in time.
How do you check the status of your working directory in Git?
Use the git status command to check the status of your working directory:
git status
git status shows the status of the working directory and the staging area. It displays which files are staged for commit, modified, and untracked.
What is the difference between git add and git commit?
git add
stages changes for the next commit.git commit
records the changes in the repository.
Explain the difference between git pull and git fetch.
- git pull fetches the latest changes from the remote repository and merges them into your local branch.
- git fetch only downloads the changes from the remote repository but doesn't merge them.
What is a branch in Git?
A branch in Git is a separate line of development. It allows you to work on different features or bug fixes without affecting the main codebase.
How do you create and switch to a new branch in Git?
To create and switch to a new branch, use:
git checkout -b <branch-name>
What is the purpose of the git merge command?
The git merge command is used to combine the changes from one branch into another.
What is a merge conflict, and how do you resolve it?
A merge conflict occurs when two branches have conflicting changes. You resolve it by manually editing the conflicting files and then committing the resolved changes.
What is git rebase, and how is it different from git merge?
git rebase re-applies commits on top of another base tip, creating a linear history. Unlike git merge, which creates a merge commit, rebase integrates changes by modifying the commit history.
How do you undo a commit in Git?
You can undo a commit using git reset or git revert:
git reset
removes the commit and moves the changes back to the working directory.git revert
creates a new commit that undoes the changes.
What is git stash, and how do you use it?
git stash temporarily saves your changes without committing them. Use git stash apply to reapply the stashed changes.
git stash
git stash apply
How do you remove a file from the staging area in Git?
Use git reset to remove a file from the staging area:
git reset <file>
What is a remote repository in Git, and how do you connect to it?
A remote repository is a version of your project hosted on the internet or network. You connect to it using:
git remote add origin <repository-url>
How do you rename a branch in Git?
To rename the current branch:
git branch -m <new-branch-name>
What is a tag in Git, and how do you create one?
A tag in Git is a reference to a specific commit, often used for marking release points. Create a tag with:
git tag <tag-name>
Explain the git diff command.
git diff shows the changes between the working directory and the staging area or between commits.
git diff
What is the purpose of the .gitignore file?
The .gitignore file specifies which files and directories to ignore and not track in the repository.
How do you view the commit history in Git?
Use git log to view the commit history:
git log
What is a fork in Git?
A fork is a copy of a repository that allows you to experiment with changes without affecting the original project.
How do you squash commits in Git? / What has to be run to squash multiple commits (last N) into a single commit?
To squash multiple commits into a single commit, you can use an interactive rebase:
git rebase -i HEAD~n
// or
git rebase -i HEAD~N
Replace pick with squash for the commits you want to combine.
What is the HEAD in Git?
HEAD is a pointer to the current branch reference, typically pointing to the latest commit.
How do you revert to a previous commit in Git?
To revert to a previous commit, you can use git reset or git checkout:
git reset --hard <commit-hash>
How do you find a specific commit in Git?
Use git log with search options:
git log --grep="<commit-message>"
How do you configure a global or local username and email in Git?
git config is used to configure Git settings, such as your username, email, and other preferences. It can be applied at different levels: system, global, or local. Configure a global username and email with:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
What are Git Hooks?
Git Hooks are scripts that run automatically on specific Git events like commit, push, or merge. They can be used to enforce policies, automate tasks, and more.
What does git clone
do?
git clone
creates a copy of an existing Git repository. It clones the repository from a remote source and sets up the entire history of the project.
git clone <repository-url>
Can you explain HEAD in terms of Git, and how many heads can be present in a repository?
HEAD in Git refers to the current snapshot or the latest commit in your working directory. Typically, there is only one HEAD per repository, pointing to the current branch. However, you can have multiple HEADs in detached HEAD states, but only one active at any time.
What is a conflict in Git?
A conflict occurs when multiple changes are made to the same part of a file and Git cannot automatically merge them. It requires manual resolution to reconcile the differences.
What is the functionality of git ls-tree?
git ls-tree displays the contents of a tree object, which includes the file mode, object type, and object name of each entry in a tree.
git ls-tree <tree-ish>
Define “Index”.
The "Index" in Git is also known as the staging area. It's a space where changes are collected before a commit is made, allowing you to control exactly what will be included in the next commit.
What does git add command do?
git add updates the index with the current content found in the working directory, preparing it for the next commit.
git add <file-name>
What is a version control system (VCS)?
A Version Control System (VCS) is a tool that helps manage changes to source code over time, allowing multiple people to work on a project without conflicting changes, and providing the ability to revert to previous versions.
How would you recover a branch that has already pushed changes in the central repository but has been accidentally deleted from every team member’s local machines?
To recover a branch that has been deleted locally but exists on the remote:
git checkout -b <branch-name> origin/<branch-name>
Can you tell something about git reflog?
git reflog
is a mechanism that tracks changes made to the tip of branches or HEAD
. It allows you to view and recover commits even if they aren't referenced by any branch.
git reflog
What consists of a commit object?
A commit object in Git includes the following:
- A pointer to the parent commit(s)
- A tree object that represents the snapshot of the project at that point in time
- An author and committer (with timestamps)
- A commit message
Explain the levels in git config
and how can you configure values using them?
git config
has three levels:
System
: Configuration for every user on the system (/etc/gitconfig
).Global
: Configuration for a specific user (~/.gitconfig
).Local
: Configuration for a specific repository (.git/config
).- Use the
--system
,--global
, or--local
flag to specify the level.
What is a detached HEAD
, what causes this, and how to avoid it?
A detached HEAD
occurs when HEAD
is pointing to a specific commit instead of a branch. This happens when you check out a commit directly:
git checkout <commit-hash>
To avoid this, always work on a branch:
git checkout -b <new-branch-name> <commit-hash>
What does git annotate command do?
git annotate (or git blame) shows who made changes to each line in a file and when those changes were made.
git annotate <file-name>
What is the difference between git stash apply and git stash pop command?
git stash
apply applies the stashed changes without removing them from the stash list.git stash
pop applies the stashed changes and removes them from the stash list.
What do the git diff and git status commands do?
- git diff shows the differences between the working directory and the staging area or between commits.
- git status shows the state of the working directory and the staging area, indicating which changes are staged, unstaged, and untracked.
Why is it considered easy to work on Git?
Git is considered easy to work with because it provides powerful branching and merging, distributed version control, comprehensive history tracking, and efficient performance even with large projects.
Tell me something about git stash?
git stash temporarily saves changes that are not ready to be committed so you can work on something else and reapply them later.
git stash
What is the command used to delete a branch?
To delete a local branch:
git branch -d <branch-name>
To delete a remote branch:
git push origin --delete <branch-name>
What differentiates between the commands git remote and git clone?
- git remote manages the set of repositories tracked by your local repository. It lists, adds, or removes remote repositories.
- git clone copies an existing repository from a remote source to your local machine.
What does git stash apply command do?
- git stash apply applies the most recently stashed changes to your working directory without removing the stash from the stash list.
Differentiate between git pull and git fetch.
- git pull fetches the latest changes from the remote repository and merges them into your current branch.
- git fetch only downloads the latest changes from the remote repository without merging them. You can review the changes before integrating them.
Can you give differences between “pull request” and “branch”?
- Branch: A branch is a separate line of development within a repository.
- Pull Request: A pull request is a request to merge changes from one branch into another, often used in collaborative workflows.
Why do we not call a Git “pull request” as “push request”?
A "pull request" is a request to pull changes from one branch into another. It’s called a "pull" request because the maintainer of the target branch pulls the changes, rather than the contributor pushing them.
Can you tell the difference between Git and GitHub?
- Git: A distributed version control system to manage source code history.
- GitHub: A web-based platform for hosting Git repositories, providing collaboration tools like issue tracking, code reviews, and pull requests.
How will you resolve conflict in Git?
To resolve a conflict:
- Identify the conflicting files with git status.
- Open the files and manually resolve the conflicts.
- Mark the conflicts as resolved with git add.
- Commit the changes with git commit.
What command helps us know the list of branches merged to master?
To list branches merged into master:
git branch --merged master
What is the best advisable step in cases of a broken commit: Create an additional commit OR amend an existing commit?
The advisable step is to amend the existing commit if the commit has not been pushed. Use:
git commit --amend
If the commit has been pushed, create an additional commit to fix the issue.
How to revert a bad commit that is already pushed?
To revert a bad commit that has been pushed:
git revert <commit-hash>
This creates a new commit that undoes the changes from the bad commit.
What is the functionality of the git cherry-pick command?
git cherry-pick allows you to apply the changes from a specific commit to your current branch.