Here we have mentioned most frequently asked Github Interview Questions and Answers specially for freshers and experienced.


 

1. What is GIT?

Ans:

GIT is a distributed version control system and source code management (SCM) system with an emphasis to handle small and large projects with speed and efficiency.

2. What is a repository in GIT?

Ans:

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.

3. What is the command you can use to write a commit message?

Ans:

The command that is used to write a commit message is “git commit –a”. The –a on the command line instructs git to commit the new content of all tracked files that have been modified. You can use “git add” before git commit –a if new files need to be committed for the first time.

4. What is the difference between GIT and SVN?

Ans:

The difference between GIT and SVN is
a) Git is less preferred for handling extremely large files or frequently changing binary files while SVN can handle multiple projects stored in the same repository.
b) GIT does not support ‘commits’ across multiple branches or tags. Subversion allows the creation of folders at any location in the repository layout.
c) Gits are unchangeable, while Subversion allows committers to treat a tag as a branch and to create multiple revisions under a tag root.

5. What are the advantages of using GIT?

Ans:

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

6. What language is used in GIT?

Ans:

GIT is fast, and ‘C’ language makes this possible by reducing the overhead of runtimes associated with higher languages.

7. What is the function of ‘GIT PUSH’ in GIT?

Ans:

‘GIT PUSH’ updates remote refs along with associated objects.

8. Why GIT better than Subversion?

Ans:

GIT is an open source version control system; it will allow you to run ‘versions’ of a project, which show the changes that were made to the code overtime also it allows you keep the backtrack if necessary and undo those changes. Multiple developers can checkout, and upload changes and each change can then be attributed to a specific developer.

9. What is GIT stash?

Ans:

GIT stash takes the current state of the working directory and index and puts in on the stack for later and gives you back a clean working directory. So in case if you are in the middle of something and need to jump over to the other job, and at the same time you don’t want to lose your current edits then you can use GIT stash.

10. What is GIT stash drop?

Ans:

When you are done with the stashed item or want to remove it from the list, run the git ‘stash drop’ command. It will remove the last added stash item by default, and it can also remove a specific item if you include as an argument.



 

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

Ans:

Git branch—merged lists the branches that have been merged into the current branch
Git branch—-no merged lists the branches that have not been merged

12. Can you put the Computer software and computer program separate from one another by a simple comparison?

Ans:

Basically, a computer program is nothing but the part of a programming code and the same is responsible for the successful execution of a task. On the other hand, computer software includes the code for programming including the guide on how to use it and concerned documents.

13. What is the function of git clone?

Ans:

The git clone command creates a copy of an existing Git repository. To get the copy of a central repository, ‘cloning’ is the most common way used by programmers.

14. What is the function of ‘git config’?

Ans:

The ‘git config’ command is a convenient way to set configuration options for your Git installation. Behaviour of a repository, user info, preferences etc. can be defined through this command.

15. What does commit object contain?

Ans:

a) A set of files, representing the state of a project at a given point of time
b) Reference to parent commit objects
c) An SHAI name, a 40 character string that uniquely identifies the commit object.

16. How can you create a repository in Git?

Ans:

In Git, to create a repository, create a directory for the project if it does not exist, and then run command “git init”. By running this command .git directory will be created in the project directory, the directory does not need to be empty.

17. What is ‘head’ in git and how many heads can be created in a repository?

Ans:

A ‘head’ is simply a reference to a commit object. In every repository, there is a default head referred as “Master”. A repository can contain any number of heads.

18. What is the purpose of branching in GIT?

Ans:

The purpose of branching in GIT is that you can create your own branch and jump between those branches. It will allow you to go to your previous work keeping your recent work intact.

19. What is the common branching pattern in GIT?

Ans:

The common way of creating branch in GIT is to maintain one as “Main“
branch and create another branch to implement new features. This pattern is particularly useful when there are multiple developers working on a single project.

20. How can you bring a new feature in the main branch?

Ans:

To bring a new feature in the main branch, you can use a command “git merge” or “git pull command”.




 

21. What is a ‘conflict’ in git?

Ans:

A ‘conflict’ arises when the commit that has to be merged has some change in one place, and the current commit also has a change at the same place. Git will not be able to predict which change should take precedence.

22. How can conflict in git resolved?

Ans:

To resolve the conflict in git, edit the files to fix the conflicting changes and then add the resolved files by running “git add” after that to commit the repaired merge, run “git commit”. Git remembers that you are in the middle of a merger, so it sets the parents of the commit correctly.

23. To delete a branch what is the command that is used?

Ans:

Once your development branch is merged into the main branch, you don’t need development branch. To delete a branch use, the command “git branch –d [head]”.

24. What is another option for merging in git?

Ans:

“Rebasing” is an alternative to merging in git.

25. What is the syntax for “Rebasing” in Git?

Ans:

The syntax used for rebase is “git rebase [new-commit] “

26. What is the difference between ‘git remote’ and ‘git clone’?

Ans:

‘git remote add’ just creates an entry in your git config that specifies a name for a particular URL. While, ‘git clone’ creates a new git repository by copying and existing one located at the URI.

27. What is GIT version control?

Ans:

With the help of GIT version control, you can track the history of a collection of files and includes the functionality to revert the collection of files to another version. Each version captures a snapshot of the file system at a certain point of time. A collection of files and their complete history are stored in a repository.

28. Mention some of the best graphical GIT client for LINUX?

Ans:

Some of the best GIT client for LINUX is
a) Git Cola
b) Git-g
c) Smart git
d) Giggle
e) Git GUI
f) qGit

29. What is Subgit? Why to use Subgit?

Ans:

‘Subgit’ is a tool for a smooth, stress-free SVN to Git migration. Subgit is a solution for a company -wide migration from SVN to Git that is:
a) It is much better than git-svn
b) No requirement to change the infrastructure that is already placed
c) Allows to use all git and all sub-version features
d) Provides genuine stress –free migration experience.

30. What is the function of ‘git diff ’ in git?

Ans:

‘git diff ’ shows the changes between commits, commit and working tree etc.


 

31. What is ‘git status’ is used for?

Ans:

As ‘Git Status’ shows you the difference between the working directory and the index, it is helpful in understanding a git more comprehensively.

32. What is the difference between the ‘git diff ’and ‘git status’?

Ans:

‘git diff’ is similar to ‘git status’, but it shows the differences between various commits and also between the working directory and index.

33. What is the function of ‘git checkout’ in git?

Ans:

A ‘git checkout’ command is used to update directories or specific files in your working tree with those from another branch without merging it in the whole branch.

34. What is the function of ‘git rm’?

Ans:

To remove the file from the staging area and also off your disk ‘git rm’ is used.

35. What is the function of ‘git stash apply’?

Ans:

When you want to continue working where you have left your work, ‘git stash apply’ command is used to bring back the saved changes onto the working directory.

36. What is the use of ‘git log’?

Ans:

To find specific commits in your project history- by author, date, content or history ‘git log’ is used.

37. What is ‘git add’ is used for?

Ans:

‘git add’ adds file changes in your existing directory to your index.

38. What is the function of ‘git reset’?

Ans:

The function of ‘Git Reset’ is to reset your index as well as the working directory to the state of your last commit.

39. What is git Is-tree?

Ans:

‘git Is-tree’ represents a tree object including the mode and the name of each item and the SHA-1 value of the blob or the tree.

40. How git instaweb is used?

Ans:

‘Git Instaweb’ automatically directs a web browser and runs webserver with an interface into your local repository.



 

41. What does ‘hooks’ consist of in git?

Ans:

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.

42. Explain what is commit message?

Ans:

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.

43. How can you fix a broken commit?

Ans:

To fix any broken commit, you will use the command “git commit—amend”. By running this command, you can fix the broken commit message in the editor.

44. Why is it advisable to create an additional commit rather than amending an existing commit?

Ans:

There are couple of reason
a) The amend operation will destroy the state that was previously saved in a commit. If it’s just the commit message being changed then that’s not an issue. But if the contents are being amended then chances of eliminating something important remains more.
b) Abusing “git commit- amend” can cause a small commit to grow and acquire unrelated changes.

45. What is ‘bare repository’ in GIT?

Ans:

To co-ordinate with the distributed development and developers team, especially when you are working on a project from multiple computers ‘Bare Repository’ is used. A bare repository comprises of a version history of your code.

46. Name a few Git repository hosting services

Ans:

Pikacode
Visual Studio Online
GitHub
GitEnterprise
SourceForge.net

47. What is a clone in GitHub?

Ans:

Cloning a Git repository means we can create a local copy of the code provided by the developer. You can simply do it with a command line: git clone git://github.com/facebook/facebook-ios-sdk.git . and we can have the code in the facebook-ios-sdk directory.

48. How much space do we get on GitHub?

Ans:

We get a space of 1 GB but if it exceeds 1GB, we receive a polite email from GitHub Support requesting to reduce the size of the repository and scale it down. In addition, here we placed a limit of files exceeding 100 MB in size.

49. What do you know about GitHub and its repository?

Ans:

It is basically a source code management system which can be considered for both small as well as large-scale software development projects. Generally, it is widely preferred for error-free and reliable computer code. Although the users can keep up the pace with SCM, it is also possible for them to add features as per their preference. A Repository is basically the directory of the Git where the metadata of the same is stored. The data might be shared or private depending on the project.

50. How it is possible for you as a user of Git to define the information of a user, behavior of a repository as well as the information of preferences in the programming?

Ans:

This can simply be done with the help of a command named Git config. Although there are other methods but getting the results through the command prompt always make sure of originality and reliability.




 

51. Tell what exactly do you know about GIT stash?

Ans:

It is used when there is a need of storing the current state of a project so that the users can continue with the same at a later stage. There is often a need to switch to another job when one is active and developers can simply keep up the pace with such a situation with the Stash. It simply enables the users to not to lose their edits.

52. Name the tool that can be deployed for Git migration?

Ans:

SubGit

53. Can you tell us a few benefits of using the GitHub over other platforms?

Ans:

There are a very large number of benefits that developer can easily make sure of with this approach. The very first one is the high availability of the GitHub along with an excellent support. In addition to this, all the users can simply make sure of the data replication, as well as the redundancy of the same. Moreover, the error-free outcomes are exactly what for which GitHub is widely appreciated. It is a platform independent and users can easily get the results in the desired manner without compromising with anything. Also, it is collaboration-friendly and users can simply use it in the way they are comfortable.

54. What language is considered in Git and what is the benefit of same in this approach according to you?

Ans:

Git is purely based on the C and the same make sure of imposing a limit on the overhead of runtimes which are generally associated with other platforms in its class. Also, c makes it compatible with all the other domains and developer’s already existing work.

55. Compare Git with SVN?

Ans:

When it comes to handling the data with large size, Git is not widely preferred. However, for users, it is possible to handle more than one project with the latter provided they remain in a same repository. In multiple branches, the Git fails to support the commits while the SVN can do so easily.

56. When it comes to software development, what are the major factors the user should be careful about?

Ans:

The software should be developed by understanding the exact needs of the client or the task which it has to perform. It should be rich in terms of features and API. Moreover, it should be secure and reliable enough to be trusted by the organizations. There are other factors such as the length of the code and the factors that can influence the same which are also necessary to pay attention to.

57. On what projects you have already worked on which are based on Github?

Ans:

This question is often asked in the IT interviews. You need to give a short or a detailed overview of the projects you have handled, the problems you faced, the outcome of the project, the benefits organization and you as a developer derived from it, the scope of the project and the time taken to complete it. Moreover, you should mention what sort of experience you derived from them.

58. What is the upper limit on the heads in the Gits?

Ans:

There is no limit and users are free to involve any number of heads in the repository. It can be considered as a simple standard reference to a commit object. The commit object couldn’t be same for all the heads.

59. Tell us whatever you know about the Github development process?

Ans:

It is basically nothing but quite similar to that of a life cycle of any specific software. Thus, you should have knowledge about the life cycle of software and the factors that can directly influence the same. There are actually several activities which are a part of the process and they are:
1. Analysis of the requirement
2. Specifications of the project
3. Architecture of the software
4. Real time implementation
5. Testing of software
6. Documentation and reporting
7. Maintenance
8. Training and support available with the same

60. Do you think GitHub is batter as compared to Subversion of same? Why or why not?

Ans:

One of the best things about the Github is it’s an open source technology where the developers are free to run the versions of their projects without worrying about anything. This is exactly what that enable the developers to have a quick review of all the changes made to the code over time. The users can also keep a track or actual code before modification. With a sub version, nothing like this is possible. Therefore, Github is a better option.


 

61. What was your biggest problem till date while working on a Github project?

Ans:

Generally, the problems are quite common in any development process. Depending on the nature and type of tasks, you might have faced a lot of issues. You can mention them all here and can genuinely answer how they were actually sorted out. Problem solving is actually learning how to tackle the challenges.

62. How many characters are there in the SHAI name?

Ans:

It is a 40 Character String that can vary in some special cases.

63. How it is possible for a developer to simply update the remote references related to the different objects?

Ans:

This is possible through the Git PUSH. In fact, it is actually the prime function of the same.

64. What do you know about the significance of software development?

Ans:

In the century we are living in, the overall time taken to accomplish a task largely matters when it comes to corporate level. The software are powerful in saving a lot of time. Also, they impose a strict upper limit on the dependency of a task on the humans. The tasks are generally governed, controlled, monitored and accomplished by the software in the current time. Thus, the scope is software development is blooming and users should pay attention to using the best available technology of the same.

65. Is it possible in the Git to merge a branch into the master? How can you find the same?

Ans:

Yes, it is possible and the users can easily keep the pace up with the same anytime. The users can directly check the list under the branch section to know more about this.

66. Are you familiar with the Git Clone?

Ans:

It is basically a command which is deployed when it comes to copying a Git repository which already exists. There are a lot of programmers who make use of this. The best thing is it really doesn’t matter whether the project is large or small, the same can easily be considered whenever the need of same is felt.

67. Name any two Git repository hosting services which are common

Ans:

These are Visual Studio Online and Git Enterprise

68. What is SHAI name?

Ans:

It is basically a string character which is responsible for the identification of the commit objects. The fact is users are free to make the changes to the default commit objects and the same is used for knowing and locating the overall changes made with a track record of the same.

69. Is it possible to create a repository in the Git? What is the step that needs to be performed before doing the same?

Ans:

Yes, it is possible and the users have to first create a directory. The same defines the project under consideration and the information related to the same.

70. What is branching it Git and what are its benefits?

Ans:

The users are free to make as many branches as they want. A branch is nothing but a set of tasks that is created by the users. While performing any task, whenever the interrupt arrives, the user can switch to another branch on priority and can accomplish the same first. The users can easily switch to the previous branch without compromising with the anything. It actually boosts up speed and enables users to perform multiple tasks at the same time. Branches are generally marinated as one in the Github.



 

71. What is Gitlog and when you can use it?

Ans:

It is basically a command that can be executed when it comes to finding the history of a project according to the date, changes made, the developer who handled it and usefulness of the same.

72. What is conflict situation in the Git and how it can be solved?

Ans:

Conflict is a situation when both the commits that need to be merged have changes at the same place. This often confuses the software which change should be taken into consideration and which one should be neglected. The best manner to solve this concern is to simply edit the files through the appropriate procedure.

73. Can you name an alternative method for performing the merging task in the Git?

Ans:

This is called as Rebasing. Generally, the users don’t prefer this method and this is because it takes a lot of time for the accomplishment of a lot of tasks that matters a lot. Sometimes a lot of unexpected errors can declare their presence in case this task is not performed accurately. This is a method that is useful only for those who have a great experience in Git technology.

74. Name a few graphical Git clients for the Linux platforms?

Ans:

These are Git-g, Git cola, Git GUI and Giggle
These clients can easily be used in conjunction with each other or they can be deployed independently

75. What is the significance of Git version control?

Ans:

When it comes to simply tracking the background of an array of files and changing their version, this approach is generally preferred. This actually works by capturing the snapshot of the moments and tasks associated. All the information remains present in the repository. However, if the users want, they can simply keep it at any desired location.

76. What do you mean by the Commit message?

Ans:

It is basically a message which you can see on the screen while working on Git whenever a change is committed. It is possible to keep a record of all the changes made by the user in an editor. The history of changes needs to be explored lately for a specific task.

77. How can the users enhance the functionality of a branch in Git?

Ans:

It is possible to do so by adding a desired feature in any of the branches. This is generally done through a command Git merge and the best part is there is no limit on adding the features in the branch. Any branch can have any number of features.

78. What is a gist in Git?

Ans:

Gists are a great way to share the work of any developer. They can share parts of files, full applications or single files. Anyone can access gists at https://gist.github.com. Each Gist is Git repository, which means, it can be forked, and cloned.

79. How can we create a gist?

Ans:

Creating a gist requires a very simple process as depicted in the steps below: –
1. Sign in to GitHub.
2. We should the navigate to the gist home page.
3. After this, we need to type an optional description and name for the gist.
4. Key in the text of your gist into the gist text box.
5. Following this we should select either to create a public gist or to create a secret gist.

80. What is a gist programming?

Ans:

GitHub provides a hosting service that facilitates a web-based Git repository. It includes all the functionality of Git with additional features added in. The gist is an additional attribute added to GitHub, which facilitates the sharing of code snippets, notes, to do lists and more. We can save our Gists as secret or public in the repository.




 

81. rence between Git vs SVN

Ans:

The main point in Git vs SVN debate boils down to this: Git is a distributed version control system (DVCS), whereas SVN is a centralized version control system.

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

Ans:

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.

83. What is git cherry-pick?

Ans:

Cherry picking in git means to choose a commit from one branch and apply it onto another.
This is in contrast with other ways such as merge and rebase which normally applies many commits onto a another branch.
Make sure you are on the branch you want apply the commit to. git checkout master Execute the following:
git cherry-pick

84. How to revert previous commit in git?

Ans:

To revert to a previous commit, ignoring any changes:
git reset-hard HEAD
where HEAD is the last commit in your current branch

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

Ans:

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.

86. Explain git stash, pop

Ans:

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory.
git stash pop applies the top stashed element and removes it from the stack. git stash apply does the same, but leaves it in the stash stack.

87. Branching strategies Pros and Cons?—?Feature, Release branching, Git flow, Lean Git flow

Ans:

Pro: By keeping latest deployed version in trunk, small fixes can be rolled out quickly without extensive testing of the latest development version.
Pro: Developers can work more freely in tighter iterations without stepping on eachother’s feet.
Pro: if you have many branches you’ll be pushed to adopt a modern DVCS (my experience is with Mercurial but I hear git or Bazaar are also good) rather than stay with a traditional centralized system (like, say, svn).
Pro: Branches can be used to facilitate ‘what-if’ scenario’s in trying out new code. At the end a decision can be made to merge the new feature or to abandon it.
Con: Having too many branches in the air at the same time and you start forgetting where things where commited, where changes have been made etc.
Con: someone has to manage the branch(es) and keep on top of things. In most teams this falls by the way-side.

88. What is GIT?

Ans:

GIT is a distributed version control system and source code management (SCM) system.

89. What is ‘head’ in git and how many heads can be created in a repository?

Ans:

A ‘head’ is simply a reference to a commit object. In every repository, there is a default head referred as “Master”. A repository can contain any number of heads.

90. What are Git repository hosting services you used?

Ans:

Github, Bitbucket, Gitlab or you can specify if you have used something else here.


 

91. What is the function of ‘git reset’?

Ans:

‘Git Reset’ is to reset your index as well as the working directory to the state of your last commit.

92. What is the syntax for “Rebasing” in Git?

Ans:

Syntax: “git rebase [new-commit] “

93. What is a ‘conflict’ in git?

Ans:

A ‘conflict’ arises when the commit that has to be merged has some change in one place, and the current commit also has a change at the same place.

94. What language is used in Git?

Ans:

Git used “C programing language”.

95. What Are The Advantages Of Using Git?

Ans:

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

96. How do you revert a commit that has already been pushed and made public?

Ans:

One or more commits can be reverted through the use of git revert. This command, in essence, creates a new commit with patches that cancel out the changes introduced in specific commits. In case the commit that needs to be reverted has already been published or changing the repository history is not an option, git revert can be used to revert commits. Running the following command will revert the last two commits:
git revert HEAD~2..HEAD
Alternatively, one can always checkout the state of a particular commit from the past, and commit it anew.

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

Ans:

Squashing multiple commits into a single commit will overwrite history, and should be done with caution. However, this is useful when working in feature branches. To squash the last N commits of the current branch, run the following command (with {N} replaced with the number of commits that you want to squash):
git rebase -i HEAD~{N}
Upon running this command, an editor will open with a list of these N commit messages, one per line. Each of these lines will begin with the word “pick”. Replacing “pick” with “squash” or “s” will tell Git to combine the commit with the commit before it. To combine all N commits into one, set every commit in the list to be squash except the first one. Upon exiting the editor, and if no conflict arises, git rebase will allow you to create a new commit message for the new combined commit.

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

Ans:

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.
The output will also include some extra information, which can be easily suppressed by including a couple of flags:
git diff-tree –no-commit-id –name-only -r {hash}

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

99. How do you setup a script to run every time a repository receives new commits through push?

Ans:

To configure a script to run every time a repository receives new commits through push, one needs to define either a pre-receive, update, or a post-receive hook depending on when exactly the script needs to be triggered.
Pre-receive hook in the destination repository is invoked when commits are pushed to it. Any script bound to this hook will be executed before any references are updated. This is a useful hook to run scripts that help enforce development policies.
Update hook works in a similar manner to pre-receive hook, and is also triggered before any updates are actually made. However, the update hook is called once for every commit that has been pushed to the destination repository.
Finally, post-receive hook in the repository is invoked after the updates have been accepted into the destination repository. This is an ideal place to configure simple deployment scripts, invoke some continuous integration systems, dispatch notification emails to repository maintainers, etc.
Hooks are local to every Git repository and are not versioned. Scripts can either be created within the hooks directory inside the “.git” directory, or they can be created elsewhere and links to those scripts can be placed within the directory.

100. What is git bisect? How can you use it to determine the source of a (regression) bug?

Ans:

Git provides a rather efficient mechanism to find bad commits. Instead of making the user try out every single commit to find out the first one that introduced some particular issue into the code, git bisect allows the user to perform a sort of binary search on the entire history of a repository.
By issuing the command git bisect start, the repository enters bisect mode. After this, all you have to do is identify a bad and a good commit:
git bisect bad # marks the current version as bad
git bisect good {hash or tag} # marks the given hash or tag as good, ideally of some earlier commit

Once this is done, Git will then have a range of commits that it needs to explore. At every step, it will checkout a certain commit from this range, and require you to identify it as good or bad. After which the range will be effectively halved, and the whole search will require a lot less number of steps than the actual number of commits involved in the range. Once the first bad commit has been found, or the bisect mode needs to be ended, the following command can be used to exit the mode and reset the bisection state:
git bisect reset



 

101. What are the different ways you can refer to a commit?

Ans:

In Git each commit is given a unique hash. These hashes can be 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).
Additionally, Git also maintains a number of aliases to certain commits, known as refs. Also, every tag that you create in the repository effectively becomes a ref (and that is exactly why you can use tags instead of commit hashes in various git commands). Git also maintains a number of special aliases that change based on the state of the repository, such as HEAD, FETCH_HEAD, MERGE_HEAD, etc.
Git also allows commits to be referred as relative to one another. For example, HEAD~1 refers to the commit parent to HEAD, HEAD~2 refers to the grandparent of HEAD, and so on. In case of merge commits, where the commit has two parents, ^ can be used to select one of the two parents, e.g. HEAD^2 can be used to follow the second parent.
And finally, refspecs. These are used to map local and remote branches together. However, these can be used to refer to commits that reside on remote branches allowing one to control and manipulate them from a local Git environment.

102. What is git rebase and how can it be used to resolve conflicts in a feature branch before merge?

Ans:

In simple words, git rebase allows one to move the first commit of a branch to a new starting location. For example, if a feature branch was created from master, and since then the master branch has received new commits, git rebase can be used to move the feature branch to the tip of master. The command effectively will replay the changes made in the feature branch at the tip of master, allowing conflicts to be resolved in the process. When done with care, this will allow the feature branch to be merged into master with relative ease and sometimes as a simple fast-forward operation.

103. One of your teammates accidentally deleted a branch, and has already pushed the changes to the central git repo. There are no other git repos, and none of your other teammates had a local copy. How would you recover this branch?

Ans:

Check out the latest commit to this branch in the reflog, and then check it out as a new branch.

104. What is SubGit?

Ans:

Begin this answer by explaining what is SubGit used for.
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.

105. How can you copy a commit made in one branch to another (e.g. a hot fix commit from released branch to current development branch)?

Ans:

You need to use the cherry-pick command. It provides the possibility to play back an existing commit to your current location/branch. So you need to switch to the target branch (e.g. git checkout development) and callgit cherry-pick {hash of that commit}.
In spite of applying the same changes, it will be a new commit with a new hash because the changes are applied to a different destination.

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

Ans:

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

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

Ans:

git fetch only downloads new data from a remote repository, but it doesn’t integrate any of the downloaded data into your working files. All it does is provide a view of this data.
git pull downloads as well as merges the data from a remote repository into your local working files. It may also lead to merge conflicts if your local changes are not yet committed. Use the git stash command to hide your local changes.

108. What is a conflict in git and how can it be resolved?

Ans:

A conflict arises when more than one commit that has to be merged has some change in the same place or same line of code. Git will not be able to predict which change should take precedence. This is a git conflict.
To resolve the conflict in git, edit the files to fix the conflicting changes and then add the resolved files by running git add. After that, to commit the repaired merge, run git commit. Git remembers that you are in the middle of a merge, so it sets the parents of the commit correctly.

109. What is the command to write a commit message in Git?

Ans:

Answer to this is pretty straightforward.
Command that is used to write a commit message is “git commit -a”.
Now explain about -a flag by saying -a on the command line instructs git to commit the new content of all tracked files that have been modified. Also mention you can use “git add” before git commit -a if new files need to be committed for the first time.

110. What is ‘bare repository’ in Git?

Ans:

You are expected to tell the difference between a “working directory” and “bare repository”.
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.




 

111. What language is used in Git?

Ans:

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.

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

Ans:

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 commit.to do this I will use a command
git revert

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

Ans:

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

114. What is Git stash?

Ans:

According to me you should first explain the need for Git stash.
Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for sometime to work on something else. The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later. The answer to this issue is Git stash.
Now explain what is Git stash.
Stashing takes your working directory that is, your modified tracked files and staged changes and saves it on a stack of unfinished changes that you can reapply at any time.

115. What is Git stash drop?

Ans:

Begin this answer by saying for what purpose we use Git ‘stash drop’.
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.
Now give an 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}.

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

Ans:

For this answer instead of just telling the command, explain what exactly this command will do.
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.

117. What is the function of ‘git config’?

Ans:

First tell why we need ‘git config‘.
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 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 username.
git config –global user.email “Your E-mail Address”: This command will add email id.

118. What does commit object contains?

Ans:

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.

119. How can you create a repository in Git?

Ans:

This is probably the most frequently asked questions and answer to this is really simple.
To create a repository, create a directory for the project if it does not exist, then run command “git init”. By running this command .git directory will be created in the project directory.

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

Ans:

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})”


 

121. What is Git bisect? How can you use it to determine the source of a (regression) bug?

Ans:

I will suggest you to first give a small definition of Git bisect.
Git bisect is used to find the commit that introduced a bug by using binary search. Command for Git bisect is
git bisect
Now since you have mentioned the command above explain 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.

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

Ans:

I will suggest you to first give a small introduction to sanity checking.
A sanity or smoke test determines whether it is possible and reasonable to continue testing.
Now explain how to achieve this.
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:

#!/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

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.
The Interviewer has not started asking questions on branching yet, so the next set of Git interview questions will be dealing with branching in Git.

123. Describe branching strategies you have used?

Ans:

This question is asked to test your branching experience with Git so, tell them about how you have used branching in your previous job and what purpose does it serves, you can refer the below mention points:
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 develop branch, which may have progressed since the release was initiated.
In the end tell them that branching strategies varies from one organization to another so I know basic branching operations like delete, merge, checking out a branch etc..

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

Ans:

The answer is pretty direct.
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.

125. What is Git rebase and how can it be used to resolve conflicts in a feature branch before merge?

Ans:

According to me you should start by saying git rebase is a command which will merge another branch into the branch where you are currently working, and move all of the local commits that are ahead of the rebased branch to the top of the history on that branch.
Now, once you have defined Git rebase time for an example to show how it can be used to resolve conflicts in a feature branch before merge.
If a feature branch was created from the master, and since then the master branch has received new commits, Git rebase can be used to move the feature branch to the tip of master. The command effectively will replay the changes made in the feature branch at the tip of master, allowing conflicts to be resolved in the process. When done with care, this will allow the feature branch to be merged into master with relative ease and sometimes as a simple fast-forward operation.
You can also expect some off track questions, so the next question in this Git interview questions blog will be regarding SubGit.