Google News
logo
Git Interview Questions
Git is a free and open source project Distributed Version Control System(DVCS) designed to handle everything from small to very large projects with speed and efficiency.
 
Many commercial projects rely on Git as every developer’s code copy is also treated as a repository, which contains all changes done in the past. Below is the detailed description of DVCS :
 
Control System : Git is known for its features like a content tracker, and it stores content.

Version Control System : It helps developers to store code at the same time and Git modifies as and when more codes are added. The version control system helps in maintaining and keeping records of all the changes. Further, it offers features like branches and merges.

Distributed Version Control System : Git has a remote repository and a local repository, which are stored on servers and computers, respectively. This means that code is stored in both the central server and the developer’s computer. Hence it is termed a distributed version control system.
2 .
What is a repository in GIT?
A repository contains a directory named .git, where git keeps all of its metadata for the repository. The content of the .git directory are private to git.
Git was developed in 2015 by Linus Torvalds for Linux kernel development. But in the last decade, it gained a lot of interest, and today, due to its flexibility, nearly every development environment uses Git and runs Git command-line tools on every major operating system.
 
Below are the reasons why Git is popular :
 
Performance : Git has very powerful raw performance characteristics be it branching, merging, or comparing the past versions; it is robust and optimized. Git gives special attention to the content, and it uses a blend of delta encoding and compression. Further, it also clearly stores directory contents and metadata object versions.

Security : Integrity is the topmost priority of Git. Its cryptography hashing algorithm named SHA1 safely stores all objects in the Git repository and maintains a true relationship between files and directories.

Flexibility : From supporting nonlinear development workflow to adaptability with various systems and protocols, Git is exceptionally elastic. Git’s amazing tracking system offers features like treating branching and tagging as first-class citizens. Its ‘change history’ also features stores operations affecting branches and tags.
A Version Control System(VCS) keeps track of the contributions of the developers working as a team on the projects. They maintain the history of code changes done and with project evolution, it gives an upper hand to the developers to introduce new code, fixes bugs, and run tests with confidence that their previously working copy could be restored at any moment in case things go wrong.
 
This helps ensure that all team members are working on the latest version of the file.

VCS
5 .
What does git clone do?
The command creates a copy (or clone) of an existing git repository. Generally, it is used to get a copy of the remote repository to the local repository.
With the Version Control System(VCS), all the team members are allowed to work freely on any file at any time. VCS gives you the flexibility to merge all the changes into a common version.
 
All the previous versions and variants are neatly packed up inside the VCS. You can request any version at any time as per your requirement and you’ll have a snapshot of the complete project right at hand.
 
Whenever you save a new version of your project, your VCS requires you to provide a short description of the changes that you have made. Additionally, you can see what changes are made in the file’s content. This helps you to know what changes have been made in the project and by whom.
 
A distributed VCS like Git allows all the team members to have a complete history of the project so if there is a breakdown in the central server you can use any of your teammate’s local Git repository.
7 .
How can you fix a broken commit?
In order to fix any broken commit, use the command “git commit --amend”. When you run this command, you can fix the broken commit message in the editor.
Git GitHub
Git is a software GitHub is a service
Provides a desktop interface called git GUI Provides a desktop interface called GitHub Desktop.
This is a distributed version control system installed on local machines which allow developers to keep track of commit histories and supports collaborative work. This is a cloud-based source code repository developed by using git.
This is maintained by “The Linux Foundation”. This was acquired by “Microsoft”
SVN, Mercurial, etc are the competitors GitLab, Atlassian BitBucket, etc are the competitors.
If you want to initialize an empty repository to a directory in Git, you need to enter the git init command. After this command, a hidden .git folder will appear.
Git Repository Initialize
Instead of just telling the name of the language, you need to tell the reason for using it as well. I will suggest you to answer this by saying:
 
Git uses ‘C’ language. GIT is fast, and ‘C’ language makes this possible by reducing the overhead of run times associated with high-level languages.
A “bare” repository in Git contains information about the version control and no working files (no tree) and it doesn’t contain the special .git sub-directory. Instead, it contains all the contents of the .git sub-directory directly in the main directory itself, whereas the working directory consists of :
 
* A .git subdirectory with all the Git related revision history of your repository.

* A working tree, or checked out copies of your project files.
Git can handle on its own most merges by using its automatic merging features. There arises a conflict when two separate branches have made edits to the same line in a file, or when a file has been deleted in one branch but edited in the other. Conflicts are most likely to happen when working in a team environment.
GIT stash takes the present state of the working file and index and puts in on the stack for next and gives you back a clean working file. So in case if you are in the middle of object and require to jump over to the other task, and at the same time you don't want to lose your current edits, you can use GIT stash.
14 .
Explain what is commit message?
Commit message is a feature of git which appears when you commit a change. Git provides you a text editor where you can enter the modifications made in commits.
15 .
What is the meaning of the commands – git status, git log, git diff, git revert <commit>,  git reset <file>?
Command Meaning
git status Gives a list of which files are staged, unstaged, and untracked
git log Illustrates the entire commit history by using the default format
git diff Displays the unstaged changes between index and working directory
git revert <commit> Undoes all the changes made in <commit> and applies it to the current branch by creating a new commit
git reset <file> Removes <file> from a staging area without overwriting any changes by keeping the working directory unchanged
Git creates a commit as and when a developer saves any new work. Commit is a screenshot of all the files, and Git will use the previously used file if a file is not changed from one commit to another. One commit creates a chain to other commits and forms a development history graph. Unique cryptocurrency hash identifies commit in Git.
git config” : Configure the username and email address
 
git add” : Add one or more files to the staging area

git commit” : Commit changes to head but not to the remote repository

git diff” : helps in showing changes between commits and those between commits and working tree.
 
git status” : helps in showing differences between the index and working directories
 
git stash applies” : is the command for bringing back saved changes on the working directory
 
git log” : helps in finding a specific commit in the history

git init” : Initialize an empty Git repository
 
git checkout” : is the command for updating directories of the working tree with directories from another branch without merging
 
git rm” : helps in removing files from staging area and files on the disk
 
git reset” : command helps in resetting the index. It also helps in resetting working directory to the state of the last commit
 
git is a tree” : is ideal for the representation of a tree object alongside the mode and name for each item
Here are some of the essential advantages of Git :
 
* Data repetition and data replication is possible

* It is a much applicable service

* For one depository you can have only one directory of Git

* The network performance and disk application are excellent

* It is effortless to collaborate on any project

* You can work on any plan within the Git
19 .
What is the functionality of git ls-tree?
This command returns a tree object representation of the current repository along with the mode and the name of each item and the SHA-1 value of the blob.
This command adds files and changes to the index of the existing directory.
 
* You can add all changes at once using git add . command.
 
* You can add files one by one specifically using git add <file_name> command.
 
* You can add contents of a particular folder by using git add /<folder_name>/ command.
Git SVN
Decentralized and distributed version control tool Centralized version control tool
Clones all repositories on the local system Stores version history on the server-side repository
Supports offline commits Supports online commits only
Swift push/pull operations Slow push/pull operations
Automatically shares work to commit Doesn’t support automatic sharing
The contents of Git are hashed using the SHA-1 hash algorithm. SVN doesn’t support hashed contents. 
This is an important Git interview question. “git pull” and “git fetch” are used for downloading new data from a remote repository.

"git pull" : This command is used to update the current HEAD data branch with all the changes that occurred in the remote repository. Thus, it downloads the data and integrates it with existing working files.
 
"git fetch" : It downloads new data from the repository but does not support integrating this data into working files. It offers a fresh view of things that happened in the remote repository.
23 .
How to resolve and solve merge conflicts?
It is very easy to resolve merge conflicts as Git allows you to go back to the previous state. Just use a “git merge –abort” command, and you will be able to undo the merge and start the task again.
If by mistake, you have committed a change into the wrong branch, you can use the “git cherry-pick” command. This command will allow you to apply commit from one branch to another branch. 
$ git cherry-pick <commit id>
Git cherry-pick command can sometimes result in duplicate commits, and thus, it must be cautiously used. The below situations are apt if planning to use the git cherry-pick command :
 
* When you mistakenly make a commit in the wrong branch.

* When you want to make changes that are proposed by other team members.
SubGit is a tool for migrating from SVN to Git. SubGit can help in the creation of a writable Git mirror of a local or remote Subversion repository. It can use Subversion as well as Git for any duration the user needs. SubGit also provides faster one-time import from Subversion to Git. In addition, you can use SubGit within the Atlassian Bitbucket server. SubGit does not demand any changes in existing infrastructure. Furthermore, SubGit also offers flexibility for using all features of Git and Subversion.
27 .
What are the uses of git instaweb?
The “git instaweb” command helps in automatically directing a web browser and running a web server with an interface to the local repository. 
A Git fork is a remote, server-side replica of a repository, different from the original. It is evident to note that a fork is not a Git concept and is a social paradigm.

Clone in Git is a local copy of a particular remote repository. During the process of cloning, users copy the entire source repository information alongside all the branches and history.

The branch is a process for the management of all changes in a single repository before merging them into the code. You can consider a branch as a thread of the development project that exists within a repository. 
29 .
What is the git push command?
The git push command is applied for uploading content to a remote repository from a local repository. Pushing can overwrite changes, so it should be used with caution.
30 .
What is the git pull command?
The git pull command is for fetching and downloading content from a remote repository and integrating it with a local repository.
Rebasing is the reapplying of commits on top of another base trip. A sequence of commits is applied from distinct branches into the final commit. It is a linear process of merging and an alternative to the git merge command. Rebasing makes it seem like one has created a branch from a different commit.
32 .
What is the difference between git rebase and git merge?
In git rebase, a feature branch is moved into a master. Git merge maintains the history by adding a new commit.
Squashing multiple commits to a single one overwrites the history which is why it is recommended to be done using full caution. This step can be done by running the command: git rebase -i HEAD~{{N}} where {{N}} represents the number of commits needed to be squashed.
In order to make git work, it uses a set of configurations that are pre-defined by default by means of configuration files (or config files). We can change the default behavior of git by just modifying these files which are basically text files. In order to do this, it is important to understand how git identifies these files. It does so by following the below steps :
 
- Firstly, git searches for the config values in the system-wide gitconfig file stored in <<installation_path>>/etc/gitconfig file that has settings defined and applied to every user of the system and all their repos.

     - In case you want git to search from this particular file and read/write on it, we can pass the option --system to git config command.
 
- Next, git searches for the ~/.gitconfig file or ~/.config/git/config that has the scope specific to the user.

   - Git can be made to read/ write from this file specifically bypassing --global to the git config command.
 
- Lastly, git searches for the config values in the git directory of the local repository that we are currently working on.

   - These config values are specific to that particular repository alone and can be accessed by passing --local to the git config command.This is the default config file that gets accessed and modified upon in case we do not specify any levels.
git revert  git reset
This command is used for creating a new commit that undoes the changes of the previous commit. This command is used for undoing the local changes done in the git repository
Using this command adds a new history to the project without modifying the existing history This command operates on the commit history, git index, and the working directory.
* A set of files, representing the state of a project at a given point of time
 
* Reference to parent commit objects
 
* An SHAI name, a 40 character string that uniquely identifies the commit object.
Some of the best GIT client for LINUX is
 
* Git Cola
 
* Git-g
 
* Smart git
 
* Giggle
 
* Git GUI
 
* qGit
38 .
What does 'hooks' consist of in git?
This directory consists of Shell scripts which are activated after running the corresponding Git commands.  For example, git will try to execute the post-commit script after you run a commit.
That before completing the commits, it can be formatted and reviewed in an intermediate area known as ‘Staging Area’ or ‘Index’. From the diagram it is evident that every change is first verified in the staging area I have termed it as “stage file” and then that change is committed to the repository.
The files which were stashed and saved in the stash index list will be recovered back. Any untracked files will be lost. Also, it is a good idea to always stage and commit your work or stash them.
 
If you want to fetch the log references of a particular branch or tag then run the command – “git reflog <ref_name>”.
Git ‘stash drop’ command is used to remove the stashed item. It will remove the last added stash item by default, and it can also remove a specific item if you include it as an argument.
 
Example :
 
If you want to remove a particular stash item from the list of stashed items you can use the below commands :
 
git stash list : It will display the list of stashed items like:

stash@{0} : WIP on master: 049d078 added the index file

stash@{1} : WIP on master: c264051 Revert “added file_size

stash@{2} : WIP on master: 21d80a5 added number to log
 
If you want to remove an item named stash@{0} use command git stash drop stash@{0}.
Below is the list of Git repository hosting functions :
 
* Pikacode
* Assembla
* Visual Studio Online
* GitHub
* GitEnterprise
* net
* Beanstalk
* CloudForge
* GitLab
* Planio
* Perforce
* Fog Creek Kiln
Feature branching : A feature branch model keeps all of the changes for a particular feature inside of a branch. When the feature is fully tested and validated by automated tests, the branch is then merged into master.
 
Task branching : In this model, each task is implemented on its own branch with the task key included in the branch name. It is easy to see which code implements which task, just look for the task key in the branch name.
 
Release branching : Once the develop branch has acquired enough features for a release, you can clone that branch to form a Release branch. Creating this branch starts the next release cycle, so no new features can be added after this point, only bug fixes, documentation generation, and other release-oriented tasks should go in this branch. Once it is ready to ship, the release gets merged into master and tagged with a version number. In addition, it should be merged back into the develop branch, which may have progressed since the release was initiated.
 
In the end tell them that branching strategies vary from one organization to another so I know basic branching operations like delete, merge, checking out a branch, etc.
There is a fundamental difference between the forking workflow and other popular git workflows. Rather than using a single server-side to act as the “central” codebase, it gives every developer their own server-side repository. The Forking Workflow is commonly seen in public open-source projects.
 
A crucial advantage of the Forking Workflow is that contributions can be integrated without even needing everybody to push to a single central repository that leads to clean project history. Developers can push to their own server-side repositories, but only the project maintainer can push to the official repository.
 
If developers are ready to publish a local commit, then they push the commit to their own public repository and not the official one. After this, they go for a pull request with the main repository that lets the project maintainer know an update is ready to be integrated.
To record the history of the project, Gitflow workflow employs two parallel long-running branches – master and develop:
 
Master : this branch is always ready to be released on LIVE, with everything fully tested and approved (production-ready).

Hotfix :
 these branches are used to quickly patch production releases. These branches are a lot like release branches and feature branches except they’re based on master instead of develop.

Develop :
 this is the branch to which all feature branches are merged and where all tests are performed. Only when everything’s been thoroughly checked and fixed it can be merged to the master.

Feature :
 each new feature should reside in its own branch, which can be pushed to the develop branch as their parent one.
46 .
What command helps us know the list of branches merged to master?
git branch --merged helps to get the list of the branches that have been merged into the current branch.

git branch --no-merged lists the branches that have not been merged to the current branch.
There can be cases where we want to revert from the pushed changes and go back to the previous version. To handle this, there are two possible approaches based on the situations :
 
Approach 1 : Fix the bad changes of the files and create a new commit and push to the remote repository. This step is the simplest and most recommended approach to fix bad changes. You can use the command: git commit -m "<message>"

Approach 2 : New commit can be created that reverts changes done in the bad commit. It can be done using git revert <name of bad commit>
Detached HEAD indicates that the currently checked-out repository is not a local branch. This can be caused by the following scenarios:
 
* When a branch is a read-only branch and we try to create a commit to that branch, then the commits can be termed as “free-floating” commits not connected to any branch. They would be in a detached state.
 
* When we checkout a tag or a specific commit and then we try to perform a new commit, then again the commits would not be connected to any branch. When we now try to checkout a branch, these new commits would be automatically placed at the top.
 
In order to ensure that detached state doesn't happen, =instead of checking out commit/tag, we can create a branch emanating from that commit and then we can switch to that newly created branch by using the command: git checkout -b <<new_branch_name>>. This ensures that a new branch is checkout out and not a commit/tag thereby ensuring that a detached state wouldn't happen.
One has to be careful during a git add, else you may end up adding files that you didn’t want to commit. However, git rm will remove it from both your staging area (index), as well as your file system (working tree), which may not be what you want.
 
Instead, use git reset :
 
git reset filename          # or
 
echo filename >> .gitingore # add it to .gitignore to avoid re-adding it
 
This means that git reset <paths> is exactly the opposite of git add <paths>.
* Git bisect is used to find the commit that introduced a bug by using binary search. The command for Git bisect is
git bisect <subcommand> <options>

* Now since you have mentioned the command above explain to them what this command will do.

* This command uses a binary search algorithm to find which commit in your project’s history introduced a bug. You use it by first telling it a “bad” commit that is known to contain the bug, and a “good” commit that is known to be before the bug was introduced. Then Git bisect picks a commit between those two endpoints and asks you whether the selected commit is “good” or “bad”. It continues narrowing down the range until it finds the exact commit that introduced the change.