GIT interview Question and Answers

GIT interview Question and Answers

1. What is ‘bare repository’ in Git?

A “bare” repository in Git just contains the version control information 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, where as working directory consist of:

– A .git subdirectory with all the Git related revision history of your repo.
– A working tree, or checked out copies of your project files.

2. In Git how do you revert a commit that has already been pushed and made public?

There can be two answers to this question and make sure that you include both because any of the below options can be used depending on the situation:

Remove or fix the bad file in a new commit and push it to the remote repository. This is the most natural way to fix an error. Once you have made necessary changes to the file, commit it to the remote repository for that I will use

# git commit -m “commit message”

Create a new commit that undoes all changes that were made in the bad do this I will use a command

# git revert <name of bad commit>

3. What is the difference between git pull and git fetch?

Git pull command pulls new changes or commits from a particular branch from your central repository and updates your target branch in your local repository.

Git fetch is also used for the same purpose but it works in a slightly different way. When you perform a git fetch, it pulls all new commits from the desired branch and stores it in a new branch in your local repository. If you want to reflect these changes in your target branch, git fetch must be followed with a git merge. Your target branch will only be updated after merging the target branch and fetched branch. Just to make it easy for you, remember the equation below:

Git pull = git fetch + git merge

4. How do you find a list of files that has changed in a particular commit?

To get a list files that has changed in a particular commit use the below command:

git diff-tree -r {hash}

Given the commit hash, this will list all the files that were changed or added in that commit. The -r flag makes the command list individual files, rather than collapsing them into root directory names only.

You can also include the below mentioned point, although it is totally optional but will help in impressing the interviewer.

The output will also include some extra information, which can be easily suppressed by including two flags:

git diff-tree –no-commit-id –name-only -r {hash}

Here –no-commit-id will suppress the commit hashes from appearing in the output, and –name-only will only print the file names, instead of their paths.

5. What does commit object contains?

Commit object contains the following components, you should mention all the three points present below:

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.

6. How do you squash last N commits into a single commit?

There are two options to squash last N commits into a single commit include both of the below mentioned options in your answer:

If you want to write the new commit message from scratch use the following command

# git reset –soft HEAD~N &&
# git commit

If you want to start editing the new commit message with a concatenation of the existing commit messages then you need to extract those messages and pass them to Git commit for that I will use

# git reset –soft HEAD~N &&
# git commit –edit -m”$(git log –format=%B –reverse .HEAD@{N})”

7. How do you configure a Git repository to run code sanity checking tools right before making commits, and preventing them if the test fails?

A sanity or smoke test determines whether it is possible and reasonable to continue testing.

This can be done with a simple script related to the pre-commit hook of the repository. The pre-commit hook is triggered right before a commit is made, even before you are required to enter a commit message. In this script one can run other tools, such as linters and perform sanity checks on the changes being committed into the repository.

Finally, give an example, you can refer the below script:

files=$(git diff –cached –name-only –diff-filter=ACM | grep ‘.go$’)
if [ -z files ]; then
exit 0
unfmtd=$(gofmt -l $files)
if [ -z unfmtd ]; then
exit 0
echo “Some .go files are not fmt’d”
exit 1

This script checks to see if any .go file that is about to be committed needs to be passed through the standard Go source code formatting tool gofmt. By exiting with a non-zero status, the script effectively prevents the commit from being applied to the repository.

8.How will you know in Git if a branch has already been merged into master?

To know if a branch has been merged into master or not you can use the below commands:

git branch –merged It lists the branches that have been merged into the current branch.
git branch –no-merged It lists the branches that have not been merged.

9. What is SubGit?

SubGit is a tool for SVN to Git migration. It creates a writable Git mirror of a local or remote Subversion repository and uses both Subversion and Git as long as you like.

Now you can include some advantages like you can do a fast one-time import from Subversion to Git or use SubGit within Atlassian Bitbucket Server.We can use SubGit to create a bi-directional Git-SVN mirror of existing Subversion repository. You can push to Git or commit to Subversion at your convenience. Synchronization will be done by SubGit.

10. How do you cherry-pick a merge commit?

Cherry-pick uses a diff to find the difference between branches.

As a merge commit belongs to a different branch, it has two parents and two changesets.

For example, if you have merge commt ref 63ad84c, you have to specify -m and use parent 1 as a base:

# git checkout release_branch
# git cherry-pick -m 1 63ad84c

11. What is Git fork? What is difference between fork and branch? How to create tag?

A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project.

A fork is really a Github (not Git) construct to store a clone of the repo in your user account. As a clone, it will contain all the branches in the main repo at the time you made the fork.

Create Tag:

– Click the releases link on our repository page.
– Click on Create a new release or Draft a new release.
– Fill out the form fields, then click Publish release at the bottom.
– After you create your tag on GitHub, you might want to fetch it into your local repository too: git fetch.

12. How to rebase master in git? Difference between rebase and merge. How to squash or fixup commits?

Rebasing is the process of moving a branch to a new base commit.

The golden rule of git rebase is to never use it on public branches. … The only way to synchronize the two master branches is to merge them back together, resulting in an extra merge commit and two sets of commits that contain the same changes.

13. What Are The Advantages Of Using Git?

a) Data redundancy and replication
b) High availability
c) Only one.git directory per repository
d) Superior disk utilization and network performance
e) Collaboration friendly
f) Any sort of projects can use GIT.

For More Interview Questions: Interview QA