Back
Decor-dark

60+ Best GIT Interview Questions To Hire Or Get Hired With

4752 words Read time 23:47

Git and the use of GitHub is one of the most used tools for large technology organizations. Git allows code to be written by multiple engineers. Each engineer can work on a particular part of the product without interfering with the other engineers' work. At the end of the process, they can merge their two pieces of work together using the Git repository. The repository is simply a version management system. It allows multiple versions of a piece of code to exist. This means one engineer can start from the current version, build on top of it safely, and when functionality is confirmed as working, merge it into the master branch which can then be deployed to users.

Experienced engineers know that Git and GitHub are part of their everyday life. They will most likely be using Git and GitHub hundreds of times in a week. Below are some helpful git interview questions and answers to get you prepared for your next job interview. If you are looking for GitHub interview questions, these are essentially the same thing as Git and GitHub are so closely tied together in their process.

1. What is Git?

• Git is a distributed version control system and source code management (SCM) system that is acclaimed by developers for handling small and large projects with speed and efficiency.
• Git is great software since it can track changes to a file and allows the developer to revert back to any particular changes.
• Its distributed architecture makes it have superior advantages over other versions control systems. One great advantage is that it does not rely on a central server to store all the versions of a projects files.
• The software allows developer to clone a copy of the repository and access the full history of the project on the hard drive and therefore when there is a server outage the local Git repository will be used for recovery.
• Additionally the software has a central clod repository that allows the developer to commit changes and share it with other teammates and the collaborators can commit changes to remote repository.

2. Explain what a Git repository is.

• This is a directory named .Git and contains all metadata for the repository.
• The content of the .Git directory are private to Git.

3. Describe what a bare repository in Git is.

• This a directory that does not contain the special .Git subdirectory but contains on the version control information and n working files ( no tree is available.
• The directory carries all the contents of the Git sub-directory in the main directory itself.
• The working directory consists of a working tree, or checked out copies of your project files.
• The .Git subdirectory will all the Git related version history of the repo.

4. Explain the main way you can use to refer to a commit.

• In the Git software, every single commit is given its own unique hash. Typically the hashes are used to identify the corresponding commits in various scenarios (such as while trying to checkout a particular state of the code using the Git checkout {hash} command.
• Git has a capability to maintain a number of aliases to some particular commits. Additionally, every tag created in the repository effectively becomes a ref( and this allows the developer to use tags instead of commit hashes in various Git commands). Additionally, Git has the ability to maintain many special aliases that change on the state of the repository such as Head, Fetch_Head, and Merge_head.
• In Git is also possible to allow commits to be referred as relative to one another. For instance, HEAD~1 refers to the commit parent to HEAD, and HEAD~2 refers to the grandparent of HEAD. In merge commits, when the commit has two parents, ^ is used to select one of the two parents, e.g. HEAD^2 can be used to follow the second parent.
• The other way is the use of refspecs. They are basically used to map local and remote branches together. However, they can be used to refer to commits that are located in remote branches allowing one to control and manipulate them from a local Git environment.

5. Give the command you would use to write a commit message

The command I would use to write a commit message is “Git command -a”.

In this command, -a instructs Git to commit the new content of all tracked files that have been modified.

Developers can also add “Git add file” before Git commit –a if the new files need are being committed for the first time.

6. Explain the difference between Git and SVN

• Git I a decentralized version control tool while SVN is a centralized version control tool.
• Git belongs to the 3rd generation of versions control tools while SVN belongs to the second generation of version control tools.
• in Git software clients can clone entire repositories on their local systems while in SVN the versions history is stored on server side repository.
• in Git commits are possible even if they are offline while on SVN only the online commits are allowed.
• In Git developers benefit form faster push/pull operations while in SVN the push and pull operations are relatively slower.
• In Git works are shared automatically by commit while in SVN nothing is shared automatically.

7. Explain the major benefits developers get when using Git

• Data redundancy and replication
• High availability
• Only one .Git directory per repository
• Superior disk utilization and network performance
• Collaboration friendly
• Any sort of projects can use Git

8. What language is used in Git

• Git utilized the ‘C’ language and this makes the software faster and reduces the overhead of runtimes associated with higher languages

9. Describe the function of “Git Push” in Git

The function ‘Git PUSH’ updates remotes refs along with associated objects

10. Explain why Git is better that Subversion?

Typically, Git is an open source version control system; it allows developers to run versions of a project, and it shows the changes that were integrated to the code overtime and also allows the developer to keep the backtrack if necessary and undo the changes.

When there are multiples developers, it is possible to checkout and upload changes and each change can then be attributed to a specific developer.

11. Explain what is “staging area” or Index in Git

• Prior to completing the commits, developers can format and review the commits in an intermediate area. This areas is what is known as “staging area” or “index”

12. Define Git stash

• This a tool that record the current state of working directory and index and puts it on the stack for later reference and it allows developers access to a clean working directory.
• When a developers is working on something and he need to move to another job, and the same time don’t want to lose the current edit he will call Git stash for this operation.

13. Define Git stash drop.

The Stash Drop command is run when the developer wants to remove a stashed item form the list. This function removes the stashed item by default and can also remove a specific item if it is used as an argument.

14. Explain what is a conflict in Git and how it can be resolved?

Conflict in Git occurs when more than one commit that need to be merged has some change in the same place or same line of code. Git will have trouble predicting which change should take precedence. This is a Git conflict. The conflict can be resolved by editing the files to fix the conflicting changes and then add the resolved files by running Git add. Additionally, to commit the repaired marge, the developer will need to run Git commit. The Git software will remember you are on a marge and will set the parents of the commit correctly.

15. Explain the contents of a commit object.

The commit object contains

• A set of files indicating the state of a project at a give point of time
• Reference to parent commit objects
• An SHAI name, a 40 character string t that uniquely identifies the commit object

16. Define a subGit.

This is a tool for SVN to Git migration. It creates a writable Git mirror of a local or remote subversion repository an uses both subversion and Git as along as you like.

17. Explain what a HEAD is.

• The HEAD is a reference to the present looked at conferring.
• It is a representative reference to the branch that we have looked at.
• A t any given time, one head is chosen as the “present head” this head is also referred as HEAD (dependently in capitalized)

18. Explain the major Git design objectives.

• Distributed workflow (decentralized)
• Easy merging (merge deemed more frequent than commit)
• Integrity (protection against accidental or malicious corruptions)
• Speed and scalability

19. Describe how you will know if a Git branch has been already merged into master

• The Git branch merged list is the branch that have been merged into the current branch • Git branch no merged lists the branches that have not been merged

20. Explain the use of Git Clone

The command creates a copy of existing Git repository.

In order to get a copy of a central repository, cloning is the method used by developers.

21. Explain the function of ‘Git config’

This used to set up configuration options for Git installation. Behavior of a repository, user information, preferences can be defined through the ‘Git config’ command.

22. Describe Version control in Git.

Versions control automatically notifies the programmers whenever there is a conflict between one person’s work and another’s It is also used to maintain records on the changes on the project and answering questions that users might have.

Version control allows programmers to keep record of commits. With it, it is possible to go back in time and know exactly who wrote changes in a specific day or identify the particular version hat was used to generate some particular set of results. It is therefore used as an undo feature in editor because all its pervious versions are saved.

Git is relatively complex, but it is widely used because its easier to set up and because of a hosting site called GitHub.

23. Explain the benefits of using Git

• Flexible workflow: the software allows programmers to create their work process. They can utilize some specific procedures that suits their projects, for instance a programmer can for brought together or ace slave among other work processes.
• Git is relatively quick when contrasted with other control frameworks.
• Data integrity is possible with Git since Git utilizes SHA1, information is relatively less ending to degenerate.
• It is possible to utilize. Beginners frequently use it to utilize their underlying activities. It also works with substantial size tasks.
• For ventures that require collaboration, Git is the right software to use. There are numerous prevalent open source programming over the globe utilize Git.

24. Explain how you can create a repository in Git

This can be done by creating a directory for the project if it does not exit, and then run command “Git Init”. This command .Git directory will be created in the project directory; the directory does not have to be empty.

25. Explain the purpose of branching in Git

• Branching allows the programmers to create an own branch an jump between the branches.
• Additionally, it allows programmers it go to previous work keeping the current work intact.

26. Give the popular branching pattern in Git

• This is mostly done by maintaining one as Main branch and create another branch to implement new features.
• The pattern is mostly used when there are multiple developers working on a project on a single project

27. Explain the process of integrating a new feature in the main branch

Bringing a new feature in the main branch, can be done by entering the command “Git merge” or “Git pull command”

28. Give the command for deleting a branch in Git

When the development branch is merged into the main branch, you will no longer need the development branch. The branch can then be deleted using the “Git branch – d[head]”

29. Give the functions you would use to remove a particular stash item from the list of stashed items:

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

30. What is the alternative method of merging in Git?

Rebasing.

31. Explain the difference between “Git remote” and “Git clone”

The command ‘Git remote add” is used in creating an entry into the Git config that specifies a name of a particular URL. On the other hand, the ‘Git clone’ creates a new Git repository by copying and existing one located URI.

32. List the best graphical Git client for Linux

• Git Cola
• Git-g
• Smart Git
• Giggle
• Git GUI
• qGit

33. What are the advantages of using SubGit in Git?

• It is much better for the migration process than Git-svn
• It does not require changing the infrastructure that is already replaced
• It enables programmers to use all Git and all subversion features
• Provides genuine stress – free migration experience

34. Explain the use of ‘Git diff’ command in Git

The command Git diff is used to show changes between commits, commit and working tree.

35. Explain the use of ‘Git status’ in Git.

This function will show the difference between the working directory and the index, it is helpful in understanding a Git more comprehensibly.

36. What is the function of ‘Git checkout’ in Git.

The command is used to update directories or specific files in the working tree and others from another branch without necessarily merging them in the whole branch.

37. Explain the use of ‘Git Rm’ command

This command is used to remove file form is the staging area and also off the disk.

38. Explain the use of command ‘Git stash apply’

This is used when a programmer wants to continue working where he left his work. The command brings back the saved changes onto the working directory.

39. Explain the use of the function ‘Git log’

The function is used to find specific commits in your project history – by author, date, content or history.

40. Explain how you can revert a commit that has been pushed and made public

The first method would be to remove or fix the bad file in anew commit and push it to the remote repository. This is the most popular way for resolving the error. After the change has been made to the file, the programmer can proceed to commit it to the remote repository.

The command used for the purpose is Git commit -m “commit message”.

The alternative method is to create a new commit that undoes all the changes made in the bad commit. The command used for the procedure is Git revert

41. Explain the difference between Git pull and Git fetch

The function Git pull is used to pull new changes or commits form a particular branch for the central repository and also updates the target branch in the local repository.

The Git fetch is a command used to for the same purpose but works in different way. the function pulls all the new commits from the desired branch and stores it in a new branch in the local repository. If you want to highlight the changes I the target branch, Git fetch must be followed with a Git merge. The target branch will only be updated after merging the target branch and fetched branch. It is useful to remember the following function Git pull = Git fetch + Git merge.

42. Explain the ‘Git reset’ function

This function is used to rest the index and also the working directory to the state of last commit.

43. Describe what a Git Is-Tree function is

The Git is-Tree indicates a tree object including the mode the name of each item and the SHA-1 value of the blob or the tree.

44. How would you use the Git Instaweb?

The Git instaweb automatically directs a web browser and runs webserver with an interface into the local repository.

45. What are hooks in Git?

These are shell scripts in the directory that are activated after running the corresponding Git commands. For instance, the Git can attempt to execute the post commit script after the programmer runs a commit.

46. Describe what a commit message is.

• This is a feature of Git that result after a commit change.
• The Git software will provide a text editor where the user can enter the modifications made in commits.

47. Describe how you can fix a broken commit.

The syntax used for fixing a broken commit is “Git commit – amend” and it will fix the broken commit in the editor.

48. Explain why it is necessary to create an additional commit rather than amending an existing commit.

• The amend operation is known to destroy the state that was previously saved in a commit. If the contents are amended there is a risk of eliminating important items.
• Amending will resulting abuse of “Git commit- amend” this might result in the small commit growing and acquiring unrelated changes.

49. List some popular Git repository hosting services:

• Pikacode
• Visual Studio Online
• GitHub
• GitEnterprise
• SourceForge.net

50. Give the syntax for rebasing in Git

The syntax used for rebasing is “Git rebase [new-commit]”

51. Explain the functions you would use to find a list to f files that has changed in particular commit

The first command is Git diff-tree -r {hash}

The command will list all the files that were changed or added in the particular commit. The –r flag makes the command list individual files, rather than collapsing them into root directory.

The alternative command is Git diff-tree –no-commit-id –name-only -r {hash}

52. Explain how would squash the last N commits into a single commit

The programmer can decide to write the new commit message from scratch a using the following command;
Git reset –soft HEAD~N &&
Git commit

The alternative method is to start editing the new commit messages with a concatenation of the existing commit messages this can be done by extracting the messages and passing them to Git commit.
The following syntax is used;
Git reset –soft HEAD~N &&
Git commit –edit -m”$(Git log –format=%B –reverse .HEAD@{N})”

53. Define the Git Bisect

• Git bisect is used to find the commit that introduced a bug by using binary search
• You can use the following command for bisecting is Git bisect <subcommand> <options></options>

54. Describe how you can determine the source of a regression using binary search

The command uses a binary search algorithms to find the commit available in the projects history introduced to a bug. The programmer will first need to contain the bug using the bad commit and later the good commit will come before the bug is introduced, the Git bisect command pick a commit between the two endpoints and asks whether selected commit is Good or bad. It then continues narrowing down the range until the exact commit that was introduced changes.

55. What is sanity check in Git?

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

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

It can be done using the simple script related to the pre-commit hook of the repository. The pre-commit hook is activated before a commit is made, just before entering the commit message.

The script allows programmer to run other tools, such as linters and perform sanity checks on the changes being committed into the repository.

An example of the syntax used for the process is as shown below:

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

57. Describe branching strategies you are familiar with.

• Feature ranching: this keeps all 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 the process every task is implemented on this own branch with the task key included in the branch name. It is easy to see the particular code that implements a task. The programmer would do it by checking for the tasks key in the branch name

Releasing branch: Upon acquiring enough features, the develop branch can be cloned to form a release branch. This allows the developer to proceed with the next release cycle. When it is ready to ship, the release will merge with master and be tagged with a version number. It should be merged with develop branch that progressed after the release was started.

58. Explain the difference between a Pull request and a branch in Git.

A branch is just a separate version of the code.

A pull request is when someone take the repository, makes their own branch, does some changes, then tries to merge that branch in (put their changes in the other person's code repository).

59. Define “Git cherry-pick’ function in Git application.

• The command Git cherry-pick is typically used to introduce particular commits from one branch within a repository onto a different branch. A common use is to forward- or back-port commits from a maintenance branch to a development branch.
• This differs in other ways such as merge and rebase which normally apply many commits onto another branch.
Syntax:
Git cherry-pick

60. What is the Git forking workflow?

This is a unique workflow in Git that uses a single server-side repository to act as the ‘central’ code base. The workflow gives a developer a unique server-side repository. The forking workflow is most often accessible in public open sources projects

61. Describe the main advantages of using forking workflow.

Developers benefit from the working workflow in the sense that contributions can be implemented without the need for all users to push to a single central repository that eats to a clean project history. Typically, the forking workflow allows developers to push to their own server-side repositories and only the project maintainer can push to the official repository.

When developers are ready to publish a local commit, they push the commit to their own public repository—not the official one. Then, they file a pull request with the main repository, which lets the project maintainer know that an update is ready to be integrated.

62. Describe the Gitflow workflow.

Gitflow workflow employs two parallel long-running branches to record the history of the project, master and develop:

Master - is always ready to be released on LIVE, with everything fully tested and approved (production-ready).

Hotfix - Maintenance or “hotfix” branches are used to quickly patch production releases. Hotfix branches are a lot like release branches and feature branches except they're based on master instead of develop.

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

63. Describe how you can remove a file from Git without removing it from your file system.

The function Git rm will remove a file from both your staging area (index), as well as your filesystem (working tree), which may not be what you want.

The best alternative here is;
Git reset filename # or
echo filename >> .Gitingore # add it to .Gitignore to avoid re-adding it
This means that Git reset <paths> is the opposite of Git add <paths>.

64. When do you use "Git rebase" instead of "Git merge"?

Both of these commands are designed to integrate changes from one branch into another branch - they just do it in very different ways.

Consider before merge/rebase:

A <- B <- C [master]
^
\
D <- E [branch]
after Git merge master:
A <- B <- C
^ ^
\ \
D <- E <- F
after Git rebase master:
A <- B <- C <- D <- E
With rebase you say to use another branch as the new base for your work.
When to use:
If you have any doubt, use merge.
The choice for rebase or merge based on what you want your history to look like.

65. Describe the difference between the working tree and index, in Git.

• The working tree/working directory/workspace is the directory tree of (source) files that you see and edit.
• The index/staging area is a single, large, binary file in <baseOfRepo>/.Git/index, which lists all files in the current branch, their sha1 checksums, timestamps, and the file name - it is not another directory with a copy of files in it.
• HEAD is a reference to the last commit in the currently checked-out branch.

Share this article

C

Reach out