GIT - Interview Questions

What is Git?

 FAQ

Git is an open source distributed version control system, and is the most widely used version control system used today. Git was originally developed by Linus Torvalds in 2005 to version control the Linus OS kernel. Today Git is widely used as a source control system for commercial projects as well as for open source projects.

What is the key features of Git that makes it more efficient compared to other version control systems such as SVN and CVS?

 FAQ

Distributed: Git is a distributed version control system. Git has a central server which maintains the project file and its history, called as a repository. Each user of Git has a complete and independent copy of this repository on his own local machine. Hence, Git users can continue working with Git and refer to history even if the central repository is unavailable.

Like Git, alternative source control systems such as SVN and CVS also have a central server which maintains the project files and its history. But unlike Git, users of these version control system do not have a local copy of the project files and its history. Hence these users are dependent on the central system, and cannot continue to work with the version control system if the central server becomes unavailable for any reason.

Snapshot-based: Git is a snapshot-based version control system. Every time a user commits the project, Git takes a snapshot of the project files and changes at that moment and stores a reference to that snapshot. If a file has not changed, Git links to the previous file instead of storing the file again.

Alternative version control systems such as SVN and CVS are delta-based version control systems. These systems store the project files and changes made to each file over time.

Speed: Git is built for speed. Nearly all operations in Git use local files and resources, and do not require information from the central server or any other computer. This eliminates network latency and dependency on the central server resulting in fast operations compared to the alternative source control systems.

Alternative source control systems such as SVN and CVS depend on information on the central server for all operations. Hence the operations are comparatively slower compared to Git, and most of these operations will not work of the central server is not available.

Integrity: Every file in Git is checksummed using a SHA-1 hash before it is stored, and is referred to with that checksum. This makes it impossible to lose or corrupt the files without Git detecting it.

What are the three main sections or regions of a Git project?

 FAQ

A Git projects consists of the following three main regions.

Git directory - Git stores the meta-data and object database in the Git directory. When a user clones a repository the Git directory is copied to the local machine.

Working directory - Working directory is the checked out version of the Git project. The project files are retrieved from the compressed database of the Git directory and put on disk for users to modify.

Index or staging area - The staging area, also called as Index, is a file in the Git directory that stores information on what goes into the next commit.

How do you create a new repository and add all contents in the current directory to this repository?

 FAQ

You create a new repository using the git init command. You can then add the contents of the current directory to this repository using the git add and git commit commands

$ git init
$ git add .
$ git commit -am 'Initial Commit'

How do you create a new branch?

 FAQ

You create a new branch using the git checkout command with the -b option.

$ git checkout -b NewBranch

Lets say that you made a commit to a branch but later realized that you have made a mistake and should not have made that commit. How do you discard this last commit made to the branch?

 FAQ

You discard the last commit made by using the git reset command.

$ git reset HEAD~

How do you update your local branch with updates made to the original repository?

 FAQ

You can update your local branch with updates made to the original repository using the git pull command.

$ git pull

How do you update the original repository with the changes made to your local branch?

 FAQ

You can update the original repository with the changes made to your local branch using the git push command.

$ git push

What does the git stash command perform?

 FAQ

Git Stash command saves the current index and working tree, and resets the working tree to mat the HEAD commit. The saved state can be restored back using git stach pop or git stach apply commands.

$ git stash

What does the git stash command perform?

 FAQ

Git Stash command saves the current index and working tree, and resets the working tree to mat the HEAD commit. The saved state can be restored back using git stach pop or git stach apply commands.

$ git stash

How do you find the list of files that have changed in a particular commit?

 FAQ

You can find the list of files that were change in a particular commit by using the diff-tree command.

$ git diff-tree -r {hash}
 
Subscribe to our Questions

 

DevOps - Interview Questions

DevOps BasicsGITJenkinsJFrogDockerDocker SwarmKubernetes
 
RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example