Recently asked GIT interview Questions and Answers:
GIT Interview Questions And Answers are as follows-
1) What is GIT?
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?
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?
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<file>” before git commit –a if new files need to be committed for the first time.
4) What is the difference between GIT and SVN?
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.
Git | SVN |
Git is a Decentralized Version Control tool | SVN is a Centralized Version Control tool |
It belongs to the 3rd generation of Version Control tools | It belongs to the 2nd generation of Version Control tools |
Clients can clone entire repositories on their local systems | Version history is stored on a server-side repository |
Commits are possible even if offline | Only online commits are allowed |
Push/pull operations are faster | Push/pull operations are slower |
Works are shared automatically by commit | Nothing is shared automatically |
5) 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
6) What language is used in GIT?
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?
‘GIT PUSH’ updates remote refs along with associated objects.
8) Why GIT better than Subversion?
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 “Staging Area” or “Index” in GIT?
Before completing the commits, it can be formatted and reviewed in an intermediate area known as ‘Staging Area’ or ‘Index’.
10) What is GIT stash?
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.
11) What is GIT stash drop?
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.
12) How will you know in GIT if a branch has been already merged into master?
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
13) What is the function of git clone?
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’?
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?
- 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?
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?
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?
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?
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?
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?
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?
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?
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?
“Rebasing” is an alternative to merging in git.
25) What is the syntax for “Rebasing” in Git?
The syntax used for rebase is “git rebase [new-commit] “
26) What is the difference between ‘git remote’ and ‘git clone’?
‘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?
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?
Some of the best GIT client for LINUX is
- a) Git Cola
- b) Git-g
- c) Smart git
- d) Giggle
- e) Git GUI
- f) Git
29) What is Subgit? Why to use Subgit?
‘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?
‘git diff ’ shows the changes between commits, commit and working tree etc.
31) What is ‘git status’ is used for?
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’?
‘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?
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’?
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’?
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’?
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?
‘git add’ adds file changes in your existing directory to your index.
38) What is the function of ‘git reset’?
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?
‘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?
‘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?
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?
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?
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?
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?
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
- Pikacode
- Visual Studio Online
- GitHub
- GitEnterprise
- net
47) What is a distributed VCS?
- These are the systems that don’t rely on a central server to store a project file and all its versions.
- In Distributed VCS, every contributor can get a local copy or “clone” of the main repository.
- As you can see in the above diagram, every programmer can maintain a local repository which is actually the copy or clone of the central repository which is present on their hard drive. They can commit and update their local repository without any hassles.
- With an operation called “pull”, they can update their local repositories with new data from the central server and “pull” operation affects changes to the main repository from their local repository.
48) What is the difference between Git and Github?
Git is a version control system of distributed nature that is used to track changes in source code during software development. It aids in coordinating work among programmers, but it can be used to track changes in any set of files. The main objectives of Git are speed, data integrity, and support for distributed, non-linear workflows.
GitHub is a Git repository hosting service, plus it adds many of its own features. GitHub provides a Web-based graphical interface. It also provides access control and several collaboration features, basic task management tools for every project.
49) What are the benefits of using Version Control System?
- 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.
50) What language is used in Git?
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.
51) How can you create a repository in Git?
This is probably the most frequently asked question and the answer to this is really simple.
To create a repository, create a directory for the project if it does not exist, then run the command “git init”. By running this command .git directory will be created in the project directory.
52) Name a few Git commands and explain their usage.
Below are some basic Git commands:
Command | Function |
git rm [file] | deletes the file from your working directory and stages the deletion. |
git log | list the version history for the current branch. |
git show [commit] | shows the metadata and content changes of the specified commit. |
git tag [commitID] | used to give tags to the specified commit. |
git checkout [branch name]
git checkout -b [branch name] |
used to switch from one branch to another.
creates a new branch and also switches to it. |
53) In Git how do you revert a commit that has already been pushed and made public?
There can be two approaches to tackle 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 then push it to the remote repository. This is the most obvious way to fix an error. Once you have made necessary changes to the file, then commit it to the remote repository using the command: git commit -m “commit message”
- Also, you can create a new commit that undoes all changes that were made in the bad commit. To do this use the command
git revert <name of bad commit>
54) What is SubGit?
SubGit is a tool for SVN to Git migration. It can create a writable Git mirror of a local or remote Subversion repository and use both Subversion and Git as long as you like.
Now you can also 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 an existing Subversion repository. You can push to Git or commit to Subversion as per your convenience. Synchronization will be done by SubGit.
55) 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
56) What is ‘staging area’ or ‘index’ in Git?
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.
57) What work is restored when the deleted branch is recovered?
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>”.
58) What is 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 some time 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.
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.
59) What is the function of ‘git stash apply’?
If you want to continue working where you had left your work then ‘git stash apply‘ command is used to bring back the saved changes onto your current working directory.
60) What is the difference between the ‘git diff ’and ‘git status’?
‘git diff ’ depicts the changes between commits, commit and working tree, etc. whereas ‘git status’ shows you the difference between the working directory and the index, it is helpful in understanding a git more comprehensively. ‘git diff’ is similar to ‘git status’, the only difference is that it shows the differences between various commits and also between the working directory and index.
61) What is the difference between ‘git remote’ and ‘git clone’?
‘git remote add’ creates an entry in your git config that specifies a name for a particular URL whereas ‘git clone’ creates a new git repository by copying an existing one located at the URL
62) What is 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}.
63) What does a commit object contain?
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
64) Describe the branching strategies you have used.
- 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.
65) Explain the advantages of forking workflow
- 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.
66) How will you know in Git if a branch has already been merged into master?
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.
67) Why is it desirable to create an additional commit rather than amending an existing commit?
There are a couple of reasons for this –
- The amend operation destroys the state that was previously saved in a commit. If there is just the commit message being changed then that’s not a problem. But if the contents are being amended then chances of eliminating something important remains more.
- Abusing “git commit- amend” can result in the growth of a small commit and acquire unrelated changes.
68) What does ‘hooks’ comprise of in Git?
This directory consists of shell scripts that are activated if you run the corresponding Git commands. For example, git will try to execute the post-commit script after you have run a commit.
69) How to remove a file from git without removing it from your file system?
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>.
70) Can you explain the Gitflow workflow?
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.
71) What is the difference between HEAD, working tree and index, in Git.
- 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 <baseOfRepo>/.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.
72) What is Git fork? What is the difference between fork, branch, and clone?
- 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.
73) What is the difference between rebasing and merge in Git?
- 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.
74) Explain the difference between reverting and resetting.
- Git reset is a powerful command that is used to undo local changes to the state of a Git repository. Git reset operates on “The Three Trees of Git” which are, Commit History ( HEAD ), the Staging Index, and the Working Directory.
- Revert command in Git creates a new commit that undoes the changes from the previous commit. This command adds a new history to the project. It does not modify the existing history.
75) What is git cherry-pick?
The command git cherry-pick is normally used to introduce particular commits from one branch within a repository onto a different branch. Another common use is to forward- or back-port commits from a maintenance branch to a development branch. This is in contrast with other ways such as merge and rebase which normally apply many commits onto another branch.
Consider:
git cherry-pick <commit-hash>
76) What is Git bisect? How can you use it to determine the source of a (regression) bug?
- 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.
77) What are the advantages of using GIT?
Here are some of the most important advantages of Git:
- Data redundancy and data replication is possible
- It is a highly available service
- For one repository you can have only one directory of Git
- The network performance and disk utilization are excellent
- It is very easy to collaborate on any project
- You can work on any sort of project within the Git
78) What are the advantages of Git over SVN?
Since Git is an open source version control system it lets you run multiple versions of your project so that it shows the changes that are made to the code over time and if needed you can keep a track of the changes that you have made. This means that large number of developers can make their own changes and upload those changes so that the changes can be attributed to the particular developers.
79) Who created Git?
Git was created by Linus Torvalds in 2005 on the road to develop Linux Kernel.
80) Which command is used for writing a Commit Message in Git?
The command used for passing on a message to a git commit is git commit -m “commit message”. The flag m is used to pass a commit message.
81) Name some Basic Operations in Git.
Some basic operation in Git include:
- Initialize
- Add
- Commit
- Push
- Pull
82) Name some Advanced Operations in Git.
Some advanced operations in Git are:
- Branching
- Merging
- Rebasing
83) What’s the difference between rebase and merge? When should you rebase and when should you merge?
Both rebase and merge commands are used to integrate changes from one branch to another but in a different manner.
As seen in the below two images, suppose you have commits (this is before merge/rebase). After the merge, you will get the result as a combination of commits. It binds together the histories of both the branches and creates a new ‘merge commit’ in the feature branch.
On the other hand, rebase will move the whole feature branch to begin at the tip of the master branch.
Rebasing is not recommended for public branches as it creates inconsistent repositories. However, rebasing is a good option for private branches/individual developers. It is not very suitable for branch-per-feature mode. But if you have a branch-per-developer model, then rebasing is of no harm.
Also, rebase is a destructive operation, so your development team should be skilled enough to apply it correctly. Otherwise, committed work can be lost.
Furthermore, reverting a merge is easier than reverting a rebase. So, if you know that there can be possibilities for revert required, then you should use the merge.
Merge perseveres history as it is whereas rebase rewrites history. Thus, if you want to see the history completely as it occurred then you should use merge.
84) What is the syntax for rebasing?
The syntax for rebase command is git rebase [new-commit]
85) Explain git push command
The Git push command is used to push the content in a local repository to a remote repository. After a local repository has been modified, a push is executed to share the modifications with remote team members
86) Explain git pull command.
Git pull is used to fetch and merge changes from the remote repository to the local repository. Git pull is a combination of two commands: git fetch; followed by git merge.
87) How is a bare repository different from the standard way of initializing a Git repository?
Using a standard way | Using a bare way |
· You create a working directory with git init command
· A .git subfolder is created with all the git-related change history |
· Does not contain any working or checked out copy of source files
· Bare repositories store git revision history in the root folder of your repository instead of .git subfolder |
88) What does the git reset –mixed and git merge –abort commands do?
git reset –mixed is used to undo changes made in the working directory and staging area.
git merge –abort helps stop the merge process and return back to the state before the merging began.
89) Explain the different points when a merge can enter a conflicted stage.
There are two stages when a merge can enter a conflicted stage.
- Starting the merge process
- If there are changes in the working directory of the stage area in the current project, the merge will fail to start
- In this case, conflicts happen due to pending changes that need to be stabilized using different Git commands
- During the merge process
- The failure during the merge process indicates that there’s a conflict between the local branch and the branch being merged
- In this case, Git resolves as much as possible, but some things have to be fixed manually in the conflicted files
90) What is git pull origin?
A pull is a fetch and a merge. * `git pull origin master` fetches commits from the master branch of the origin remote (into the local origin/master branch), and then it merges origin/master into the branch you currently have checked out.
Related Posts:
- UPDATED Jenkins Interview Questions And Answers
- Chef Interview Questions And Answers
- DevOps Interview Questions And Answers
- TOP 150+ Ansible Interview Questions And Answers
- Ruby Interview Questions And Answers
- Puppet Interview Questions And Answers
- Latest Kubernetes Interview Questions and Answers
- TOP 200+ JAVA Interview Questions And Answers
For more Interview Questions And Answers click here