Git Interview Questions

Git Interview Questions


Git Beginner

Question
What is the difference between git pull and git fetch?
Shortly, git pull = git fetch + git merge

When you run git pull, it gets all the changes from the remote or central
repository and attaches it to your corresponding branch in your local repository.

git fetch gets all the changes from the remote repository, stores the changes in
a separate branch in your local repository
                    

Question
Explain the following: git directory, working directory and staging area

The Git directory is where Git stores the meta data and object database for your project. This is the most important part of Git, and it is what is copied when you clone a repository from another computer.

The working directory is a single checkout of one version of the project. These files are pulled out of the compressed database in the Git directory and placed on disk for you to use or modify.

The staging area is a simple file, generally contained in your Git directory, that stores information about what will go into your next commit. It’s sometimes referred to as the index, but it’s becoming standard to refer to it as the staging area.

This answer taken from [git-scm.com](https://git-scm.com/book/en/v1/Getting-Started-Git-Basics#_the_three_states)
                    

Question
How to resolve git merge conflicts?
First, you open the files which are in conflict and identify what are the conflicts.
Next, based on what is accepted in your company or team, you either discuss with your
colleagues on the conflicts or resolve them by yourself
After resolving the conflicts, you add the files with `git add [file_name]`
Finally, you run `git rebase --continue`
                    

Question
What is the difference between git reset and git revert?
git revert creates a new commit which undoes the changes from last commit.
git reset depends on the usage, can modify the index or change the commit which the branch head
is currently pointing at.
                    

Question
You would like to move forth commit to the top. How would you achieve that?
Using git rebase> command
                    

Question
In what situations are you using git rebase?
                    

Question
What merge strategies are you familiar with?

Mentioning two or three should be enough and it's probably good to mention that 'recursive' is the default one.

recursive
resolve
ours
theirs

This page explains it the best: https://git-scm.com/docs/merge-strategies
                    

Question
How can you see which changes have done before committing them?

git diff
                    

Question
How do you revert a specific file to previous commit?
git checkout HEAD~1 -- /path/of/the/file
                    

Question
What is the .git directory? What can you find there?
                    

Question
What are some Git anti-patterns? Things that you shouldn't do

  * Not waiting to long between commits
  * Not removing the .git directory :)
                    
Advanced Git

Question
Explain Git octopus merge

Probably good to mention that it's:

  * It's good for cases of merging more than one branch (and also the default of such use cases)
  * It's primarily meant for bundling topic branches together 

This is a great article about Octopus merge: http://www.freblogg.com/2016/12/git-octopus-merge.html