• +91 9723535972
  • info@interviewmaterial.com

GIT Interview Questions and Answers

Related Subjects

GIT Interview Questions and Answers

Question - 51 : - How do you find a list of files that have changed in a particular commit?

Answer - 51 : -

For this answer instead of just telling the command, explain what exactly this command will do.

To get a list file 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.

Question - 52 : - What is the function of ‘git config’?

Answer - 52 : -

Git uses your username to associate commits with an identity. The git config command can be used to change your Git configuration, including your username.

Now explain with an example.

Suppose you want to give a username and email id to associate a commit with an identity so that you can know who has made a particular commit. For that I will use:

git config –global user.name “Your Name”: This command will add a username.
git config –global user.email “Your E-mail Address”: This command will add an email id. 

Question - 53 : - What does a commit object contain?

Answer - 53 : -

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

  • A set of files, representing the state of a project at a given point of time
  • Reference to parent commit objects
  • An SHA-1 name, a 40 character string that uniquely identifies the commit object 

Question - 54 : - Describe the branching strategies you have used.

Answer - 54 : -

  • 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.

Question - 55 : - What is the difference between rebasing and merge in Git?

Answer - 55 : -

  • In Git, the rebase command is used to integrate changes from one branch into another. It is an alternative to the “merge” command. The difference between rebasing and merge is that rebase rewrites the commit history in order to produce a straight, linear succession of commits.
  • Merging is Git’s way of putting a forked history back together again. The git merge command helps you take the independent lines of development created by git branch and integrate them into a single branch.

Question - 56 : - What are the different ways you can refer to a commit?

Answer - 56 : -

  • In Git each commit has a unique hash. These hashes are used to identify the corresponding commits in various scenarios, for example, while trying to checkout a particular state of the code using the git checkout {hash} command.
  • Along with this, Git maintains a number of aliases to certain commits, known as refs. Also, every tag that is created in the repository effectively becomes a ref and that is exactly why you can use tags instead of committing hashes in various git commands. Git also maintains a number of special aliases that are changed based on the state of the repository, such as HEAD, FETCH_HEAD, MERGE_HEAD, etc.
  • In Git, commits are allowed to be referred to as relative to one another. In the case of merge commits, where the commit has two parents, ^ can be used to select one of the two parents, for example, HEAD^2 can be used to follow the second parent.
  • And finally, refspecs are used to map local and remote branches together. However, these can also be used to refer to commits that reside on remote branches allowing one to control and manipulate them from a local git environment.

Question - 57 : - What is Git fork? What is the difference between fork, branch, and clone?

Answer - 57 : -

  • A fork is a copy of a repository. Normally you fork a repository so that you are able to freely experiment with changes without affecting the original project. Most commonly, forks are used to either propose changes to someone else’s project or to use someone else’s project as a starting point for your own idea.
  • git cloning means pointing to an existing repository and make a copy of that repository in a new directory, at some other location. The original repository can be located on the local file system or on remote machine accessible supported protocols. The git clone command is used to create a copy of an existing Git repository.
  • In very simple words, git branches are individual projects within a git repository. Different branches within a repository can have completely different files and folders, or it could have everything the same except for some lines of code in a file.

Question - 58 : - Tell me the difference between HEAD, working tree and index, in Git.

Answer - 58 : -

  • The working tree/working directory/workspace is the directory tree of (source) files that you are able to see and edit.
  • The index/staging area is a single, large, binary file in /.git/index, which lists all files in the current branch, their SHA-1 checksums, timestamps, and the file name – it is not another directory which contains a copy of files in it.
  • HEAD is used to refer to the last commit in the currently checked-out branch.

Question - 59 : - Can you explain the Gitflow workflow?

Answer - 59 : -

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.

Question - 60 : - How to remove a file from git without removing it from your file system?

Answer - 60 : -

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 is exactly the opposite of git add .


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners