Git interview questions and answers

Q1. What is Git and how does it work?

Git is a distributed version control system that allows developers to track changes in their codebase and collaborate with other developers. It works by creating a repository, which is a central location where all the source code files and other resources related to a project are stored. Developers can make changes to the codebase and create commits, which are records of changes made to the repository. They can also create branches to work on new features or bug fixes without affecting the main codebase.
Git allows developers to merge their changes into the main codebase, share their changes with other team members, and keep track of the history of the codebase.


Q2. What is a repository in Git?

A repository is a central location where all the source code files, configuration files, and other resources related to a project are stored. Git repositories can be stored on a local machine or on a remote server. To create a new repository in Git, you can use the git init command.

For example, the following command creates a new repository named "myproject":

$ git init myproject


Q3. What is the difference between Git and SVN?

Git is a distributed version control system, whereas SVN (Subversion) is a centralized version control system. In Git, each developer has a local copy of the repository, and changes are merged into a central repository. In SVN, all changes are made to a central repository, and developers have to update their local copy to get the latest changes.
Git allows developers to work offline and merge changes without a network connection, whereas SVN requires a network connection to commit changes.


Q4. What is a commit in Git?

A commit is a record of changes made to the repository. It is a snapshot of the codebase at a specific point in time. Each commit has a unique identifier, and it includes information such as the author, timestamp, and commit message. To create a new commit in Git, you can use the git commit command.
For example, the following command creates a new commit with the message "Add new feature":

$ git commit -m "Add new feature"


Q5. What is a branch in Git?

A branch is a separate line of development in Git. It allows developers to work on new features or bug fixes without affecting the main codebase. Branches can be merged back into the main branch when the changes are ready to be released. To create a new branch in Git, you can use the git branch command.

For example, the following command creates a new branch named "feature":

$ git branch feature


Q6. What is a merge in Git?

A merge is the process of combining two or more branches in Git. When a branch is merged, the changes made in the branch are incorporated into the main codebase. Git uses a three-way merge algorithm to combine the changes made in two branches. To merge a branch in Git, you can use the git merge command.

For example, the following command merges the "feature" branch into the current branch:

$ git merge feature


Q7. What is a pull request in Git?

A pull request is a feature of Git that allows developers to request changes to be merged into the main codebase. It allows other developers to review the changes before they are merged, ensuring that the codebase remains stable and error-free. To create a pull request in Git, you can use the pull request feature of your Git hosting service, such as GitHub or GitLab.


Q8. What is a rebase in Git?

Rebasing is the process of moving the branch to a new base commit. This is typically done to keep the commit history linear and avoid unnecessary merge commits. It is useful when working with long-lived feature branches or when updating a branch with changes from the main codebase. To perform a rebase in Git, you can use the git rebase command.

For example, the following command rebases the current branch onto the "master" branch:

$ git rebase master


Q9. What is the difference between a merge and a rebase in Git?

Both merge and rebase are ways to combine changes from one branch into another in Git. However, they do it in different ways. A merge creates a new commit that combines the changes from two branches, while a rebase moves the entire branch to a new base commit and reapplies the changes on top of it. The main difference between the two is that a merge preserves the history of the two branches, while a rebase creates a linear history. Merging is useful when you want to keep a clear record of the development history, while rebasing is useful when you want to keep the commit history clean and linear.


Q10. What is a Git stash?

A Git stash is a way to temporarily save changes that are not ready to be committed. It allows developers to switch to another branch or work on another feature without committing the changes made to the current branch. Stashing creates a stack of saved changes that can be popped or applied later. To stash changes in Git, you can use the git stash command.

For example, the following command stashes the current changes:

$ git stash


Q11. What is the difference between Git pull and Git fetch?

Both git pull and git fetch are ways to update the local repository with changes from a remote repository. However, they do it in different ways. Git fetch downloads the changes from the remote repository and updates the remote tracking branches, but does not merge the changes into the local branch. Git pull, on the other hand, downloads the changes from the remote repository and automatically merges them into the local branch.

Git pull is a combination of git fetch and git merge. Fetching is useful when you want to review the changes before merging them, while pulling is useful when you want to quickly update the local branch.


Q12. What is Git cherry-pick?

Git cherry-pick is a way to apply a specific commit from one branch to another. It allows developers to selectively apply changes to a branch without merging the entire branch. Cherry-picking creates a new commit that includes the changes from the selected commit. To cherry-pick a commit in Git, you can use the git cherry-pick command.

For example, the following command cherry-picks the commit with the hash "abc123" to the current branch:

$ git cherry-pick abc123


Q13. What is Git blame?

Git blame is a way to view the changes made to a file and see who made each change. It shows the commit hash, author, and timestamp for each line of the file. Git blame is useful for tracking down when and by whom a particular change was made to a file. To view the Git blame information for a file, you can use the git blame command. For example, the following command shows the Git blame information for the file "myfile.txt":

$ git blame myfile.txt


Q14. What is Git bisect?

Git bisect is a way to find the commit that introduced a bug in the codebase. It works by performing a binary search through the commit history to isolate the commit that introduced the bug. Bisecting requires marking a known good commit and a known bad commit and allowing Git to perform a binary search between the two to find the commit that caused the issue. To use Git bisect, you can use the git bisect command along with the git bisect good and git bisect bad commands to mark the known good and bad commits respectively. Git bisect will then automatically check out a commit between the two and prompt the user to test whether the bug is present in that commit. Based on the feedback, Git will narrow down the search until it isolates the commit that caused the bug.


Q15. What is Git reflog?

Git reflog is a way to view the history of changes made to the Git repository, even if they have been removed or overwritten. It keeps a log of all changes to the repository, including commits, branch creations and deletions, and merges. Reflog is useful for recovering lost commits or branches and for undoing changes that have been overwritten. To view the Git reflog, you can use the git reflog command.

For example, the following command shows the Git reflog for the current branch:

$ git reflog


Q16. What is Git submodule?

Git submodule is a way to include a Git repository as a subdirectory of another Git repository. It allows developers to include external dependencies as part of their project without copying the entire repository. Submodules link to a specific commit in the external repository and can be updated independently of the parent repository. To add a Git submodule to a repository, you can use the git submodule add command.

For example, the following command adds the "mylibrary" repository as a submodule in the "myproject" repository:

$ git submodule add https://github.com/username/mylibrary myproject/mylibrary


Q17. What is Git revert?

Git revert is a way to undo a previous commit by creating a new commit that undoes the changes made in the previous commit. It allows developers to undo changes without removing them from the commit history. Reverting creates a new commit that is the opposite of the previous commit, effectively canceling out its changes. To revert a commit in Git, you can use the git revert command.

For example, the following command reverts the commit with the hash "abc123":

$ git revert abc123


Q18. What is Git reset?

Git reset is a way to undo changes made to the commit history by moving the branch pointer to a previous commit. It allows developers to remove commits from the history, either completely or partially. Resetting changes the commit history and can be dangerous if not used carefully. There are three types of reset: soft, mixed, and hard. Soft reset keeps the changes in the working directory, mixed reset keeps the changes in the working directory and the index, while hard reset discards all changes. To reset a branch in Git, you can use the git reset command.

For example, the following command resets the current branch to the commit with the hash "abc123":

$ git reset abc123


Q19. What is Git tag?

Git tag is a way to mark a specific commit as important, such as a release or a milestone. It creates a permanent reference to a specific commit that can be used to quickly and easily access it later. Tags are immutable and can be shared with others to reference a specific commit. To create a tag in Git, you can use the git tag command.

For example, the following command creates a tag named "v1.0" for the current commit:

$ git tag v1.0


Q20. What is Git clone?

Git clone is a way to create a copy of a Git repository on a local machine or a remote server. It allows developers to work on the repository without affecting the original repository. Cloning creates a complete copy of the repository, including all branches and tags. To clone a Git repository, you can use the git clone command followed by the URL of the repository.

For example, the following command clones the "myproject" repository from GitHub:

$ git clone https://github.com/username/myproject.git