{"id":8491,"date":"2020-01-08T12:55:23","date_gmt":"2020-01-08T12:55:23","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=8491"},"modified":"2020-01-11T10:02:49","modified_gmt":"2020-01-11T10:02:49","slug":"git-interview-questions-and-answer-part-1","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/git-interview-questions-and-answer-part-1\/","title":{"rendered":"Git Interview Questions and Answer Part \u2013 1"},"content":{"rendered":"<h3>Q1. What is GIT?<\/h3>\n<p>Git is a distributed version control system and source code management (SCM) system with focus to handle small and large projects source code versions in the local repository with speed and efficiency. It is free and open source and its one of widly used versioning tools used ever worldwide.<\/p>\n<hr \/>\n<h3>Q2. What do you understant the repository in Git?<\/h3>\n<p>A repository in git, consists of .git directory which contains the each source code commited in form of objects created using SHA1 algorithms. A .git directry where git keeps all of its metadata for the source code in objects forms. It also contains the git configuration file, breanch reference and staging state of the work space.<\/p>\n<hr \/>\n<h3>Q. What is the command you can use to write a commit message?<\/h3>\n<p>The command that is used to write a commit message is \u201cgit commit \u2013m&#8221;this is reason for commit&#8221;.\u00a0 The \u2013a on the command line instructs git to commit the new content of all tracked files that have been modified. You can use &#8220;git add &lt;file&gt;&#8221; before git commit \u2013a if new files need to be committed for the first time.<\/p>\n<p>In nutshell, any new changes has to added from working directory to stageing area and then commit from staging area to reposiory. Please refer the image for the same as below;<\/p>\n<hr \/>\n<h3>Q. What is the difference between GIT and SVN?<\/h3>\n<p>The difference between GIT and SVN is<\/p>\n<ol>\n<li>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.<\/li>\n<li>GIT does not support \u2018commits\u2019 across multiple branches or tags.\u00a0 Subversion allows the creation of folders at any location in the repository layout.<\/li>\n<li>Gits are unchangeable, while Subversion allows committers to treat a tag as a branch and to create multiple revisions under a tag root.<\/li>\n<\/ol>\n<hr \/>\n<h3>Q. What are the advantages of using GIT?<\/h3>\n<ol>\n<li>Data redundancy and replication<\/li>\n<li>High availability<\/li>\n<li>Only one.git directory per repository<\/li>\n<li>Superior disk utilization and network performance<\/li>\n<li>Collaboration friendly<\/li>\n<li>Any sort of projects can use GIT<\/li>\n<\/ol>\n<hr \/>\n<h3>Q. What language is used in GIT?<\/h3>\n<p>GIT is fast, and \u2018C\u2019 language makes this possible by reducing the overhead of runtimes associated with higher languages.<\/p>\n<hr \/>\n<h3>Q. What is the function of \u2018GIT PUSH\u2019 in GIT?<\/h3>\n<p>\u2018GIT PUSH\u2019 updates remote refs along with associated objects.<\/p>\n<hr \/>\n<h3>Q. Why GIT better than Subversion?<\/h3>\n<p>GIT is an open source version control system; it will allow you to run \u2018versions\u2019 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.\u00a0 Multiple developers can checkout, and upload changes and each change can then be attributed to a specific developer.<\/p>\n<hr \/>\n<h3>Q. What is \u201cStaging Area\u201d or \u201cIndex\u201d in GIT?<\/h3>\n<p>Before completing the commits, it can be formatted and reviewed in an intermediate area known as \u2018Staging Area\u2019 or \u2018Index\u2019.<\/p>\n<hr \/>\n<h3>Q. What is GIT stash?<\/h3>\n<p>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.\u00a0 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\u2019t want to lose your current edits then you can use GIT stash.<\/p>\n<h3>Q. What is GIT stash drop?<\/h3>\n<p>When you are done with the stashed item or want to remove it from the list, run the git \u2018stash drop\u2019 command. \u00a0It will remove the last added stash item by default, and it can also remove a specific item if you include as an argument.<\/p>\n<hr \/>\n<h3>Q. How will you know in GIT if a branch has been already merged into master?<\/h3>\n<p>Git branch\u2014merged lists the branches that have been merged into the current branch<\/p>\n<p>Git branch\u2014no merged lists the branches that have not been merged<\/p>\n<hr \/>\n<h3>Q. is the function of git clone?<\/h3>\n<p>The git clone command creates a copy of an existing Git repository.\u00a0 To get the copy of a central repository, \u2018cloning\u2019\u00a0 is the most common way used by programmers.<\/p>\n<hr \/>\n<h3>Q. What is the function of \u2018git config\u2019?<\/h3>\n<p>The \u2018git config\u2019 command is a convenient way to set configuration options for your Git installation.\u00a0 Behaviour of a repository, user info, preferences etc. can be defined through this command.<\/p>\n<hr \/>\n<h3>Q. What does commit object contain?<\/h3>\n<ol>\n<li>A set of files, representing the state of a project at a given point of time<\/li>\n<li>Reference to parent commit objects<\/li>\n<li>An SHAI name, a 40 character string that uniquely identifies the commit object.<\/li>\n<\/ol>\n<hr \/>\n<h3>Q. How can you create a repository in Git?<\/h3>\n<p>In Git, to create a repository, create a directory for the project if it does not exist, and then run command \u201cgit init\u201d. By running this command .git directory will be created in the project directory, the directory does not need to be empty.<\/p>\n<hr \/>\n<h3>Q. What is \u2018head\u2019 in git and how many heads can be created in a repository?<\/h3>\n<p>A \u2018head\u2019 is simply a reference to a commit object. In every repository, there is a default head referred as \u201cMaster\u201d.\u00a0 A repository can contain any number of heads.<\/p>\n<hr \/>\n<h3>Q. What is the purpose of branching in GIT?<\/h3>\n<p>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.<\/p>\n<hr \/>\n<h3>Q. What is the common branching pattern in GIT?<\/h3>\n<p>The common way of creating branch in GIT is to maintain one as \u201cMain\u201c<\/p>\n<p>branch and create another branch to implement new features. This pattern is particularly useful when there are multiple developers working on a single project.<\/p>\n<hr \/>\n<h3>Q. How can you bring a new feature in the main branch?<\/h3>\n<p>To bring a new feature in the main branch, you can use a command \u201cgit merge\u201d or \u201cgit pull command\u201d.<\/p>\n<hr \/>\n<h3>Q. What is a \u2018conflict\u2019 in git?<\/h3>\n<p>A \u2018conflict\u2019 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.<\/p>\n<hr \/>\n<h3>Q. How can conflict in git resolved?<\/h3>\n<p>To resolve the conflict in git, edit the files to fix the conflicting changes and then add the resolved files by running \u201cgit add\u201d after that to commit the repaired merge,\u00a0 run \u201cgit commit\u201d.\u00a0 Git remembers that you are in the middle of a merger, so it sets the parents of the commit correctly.<\/p>\n<hr \/>\n<h3>Q. To delete a branch what is the command that is used?<\/h3>\n<p>Once your development branch is merged into the main branch, you don\u2019t need development branch.\u00a0 To delete a branch use, the command \u201cgit branch \u2013d [head]\u201d.<\/p>\n<hr \/>\n<h3>Q. What is another option for merging in git?<\/h3>\n<p>\u201cRebasing\u201d is an alternative to merging in git.<\/p>\n<hr \/>\n<h3>Q. What is the syntax for \u201cRebasing\u201d in Git?<\/h3>\n<p>The syntax used for rebase is \u201cgit rebase [new-commit] \u201c<\/p>\n<hr \/>\n<h3>Q. What is the difference between \u2018git remote\u2019 and \u2018git clone\u2019?<\/h3>\n<p>\u2018git remote add\u2019\u00a0 just creates an entry in your git config that specifies a name for a particular URL.\u00a0 While, \u2018git clone\u2019 creates a new git repository by copying and existing one located at the URI.<\/p>\n<hr \/>\n<h3>Q. What is GIT version control?<\/h3>\n<p>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.\u00a0 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.<\/p>\n<hr \/>\n<h3>Q. Mention some of the best graphical GIT client for LINUX?<\/h3>\n<p>Some of the best GIT client for LINUX is<\/p>\n<ol>\n<li>Git Cola<\/li>\n<li>Git-g<\/li>\n<li>Smart git<\/li>\n<li>Giggle<\/li>\n<li>Git GUI<\/li>\n<li>qGit<\/li>\n<\/ol>\n<hr \/>\n<h3>Q. What is Subgit? Why to use Subgit?<\/h3>\n<p>\u2018Subgit\u2019 is a tool for a smooth, stress-free SVN to Git migration.\u00a0 Subgit is a solution for a company -wide migration from SVN to Git that is:<\/p>\n<p>a)\u00a0\u00a0\u00a0\u00a0\u00a0 It is much better than git-svn<\/p>\n<p>b)\u00a0\u00a0\u00a0\u00a0\u00a0 No requirement to change the infrastructure that is already placed<\/p>\n<p>c)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Allows to use all git and all sub-version features<\/p>\n<p>d)\u00a0\u00a0\u00a0\u00a0\u00a0 Provides genuine stress \u2013free migration experience.<\/p>\n<hr \/>\n<h3>Q. What is the function of \u2018git diff \u2019 in git?<\/h3>\n<p>\u2018git diff \u2019 shows the changes between commits, commit and working tree etc.<\/p>\n<hr \/>\n<h3>Q. What is \u2018git status\u2019 is used for?<\/h3>\n<p>As \u2018Git Status\u2019 shows you the difference between the working directory and the index, it is helpful in understanding a git more comprehensively.<\/p>\n<hr \/>\n<h3>Q. What is the difference between the \u2018git diff \u2019and \u2018git status\u2019?<\/h3>\n<p>\u2018git diff\u2019 is similar to \u2018git status\u2019, but it shows the differences between various commits and also between the working directory and index.<\/p>\n<hr \/>\n<h3>Q. What is the function of \u2018git checkout\u2019 in git?<\/h3>\n<p>A \u2018git checkout\u2019 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.<\/p>\n<hr \/>\n<h3>Q. What is the function of \u2018git rm\u2019?<\/h3>\n<p>To remove the file from the staging area and also off your disk \u2018git rm\u2019 is used.<\/p>\n<hr \/>\n<h3>Q. What is the function of \u2018git stash apply\u2019?<\/h3>\n<p>When you want to continue working where you have left your work, \u2018git stash apply\u2019 command is used to bring back the saved changes onto the working directory.<\/p>\n<hr \/>\n<h3>Q. What is the use of \u2018git log\u2019?<\/h3>\n<p>To find specific commits in your project history- by author, date, content or history \u2018git log\u2019 is used.<\/p>\n<hr \/>\n<h3>Q. What is \u2018git add\u2019 is used for?<\/h3>\n<p>\u2018git add\u2019 adds file changes in your existing directory to your index.<\/p>\n<hr \/>\n<h3>Q. What is the function of \u2018git reset\u2019?<\/h3>\n<p>The function of \u2018Git Reset\u2019 is to reset your index as well as the working directory to the state of your last commit.<\/p>\n<hr \/>\n<h3>Q. What is git Is-tree?<\/h3>\n<p>\u2018git Is-tree\u2019 represents a tree object including the mode and the name of each item and the SHA-1 value of the blob or the tree.<\/p>\n<hr \/>\n<h3>Q. How git instaweb is used?<\/h3>\n<p>\u2018Git Instaweb\u2019 automatically directs a web browser and runs webserver with an interface into your local repository.<\/p>\n<hr \/>\n<h3>Q. What does \u2018hooks\u2019 consist of in git?<\/h3>\n<p>This directory consists of Shell scripts which are activated after running the corresponding Git commands.\u00a0 For example, git will try to execute the post-commit script after you run a commit.<\/p>\n<hr \/>\n<h3>Q. Explain what is commit message?<\/h3>\n<p>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.<\/p>\n<hr \/>\n<h3>Q. How can you fix a broken commit?<\/h3>\n<p>To fix any broken commit, you will use the command \u201cgit commit\u2014amend\u201d. By running this command, you can fix the broken commit message in the editor.<\/p>\n<hr \/>\n<h3>Q. Why is it advisable to create an additional commit rather than amending an existing commit?<\/h3>\n<p>There are couple of reason<\/p>\n<ol>\n<li>The amend operation will destroy the state that was previously saved in a commit.\u00a0 If it\u2019s just the commit message being changed then that\u2019s not an issue.\u00a0 But if the contents are being amended then chances of eliminating something important remains more.<\/li>\n<li>Abusing \u201cgit commit- amend\u201d can cause a small commit to grow and acquire unrelated changes.<\/li>\n<\/ol>\n<hr \/>\n<h3>Q. What is \u2018bare repository\u2019 in GIT?<\/h3>\n<p>To co-ordinate with the distributed development and developers team, especially when you are working on a project from multiple computers \u2018Bare Repository\u2019 is used. A bare repository comprises of a version history of your code.<\/p>\n<hr \/>\n<h3>Q. How do you revert a commit that has already been pushed and made public?<\/h3>\n<p>One or more commits can be reverted through the use of\u00a0<em>git revert<\/em>. 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,\u00a0<em>git revert<\/em>\u00a0can be used to revert commits. Running the following command will revert the last two commits:<\/p>\n<p>git revert HEAD~2..HEAD<\/p>\n<p>Alternatively, one can always checkout the state of a particular commit from the past, and commit it anew.<\/p>\n<hr \/>\n<h3>Q. How do you squash last N commits into a single commit?<\/h3>\n<p>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):<\/p>\n<p>git rebase -i HEAD~{N}<\/p>\n<p>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 \u201cpick\u201d. Replacing \u201cpick\u201d with \u201csquash\u201d or \u201cs\u201d 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,\u00a0<em>git rebase<\/em>\u00a0will allow you to create a new commit message for the new combined commit.<\/p>\n<hr \/>\n<h3>Q. How do you find a list of files that has changed in a particular commit?<\/h3>\n<p>git diff-tree -r {hash}<\/p>\n<p>Given the commit hash, this will list all the files that were changed or added in that commit. The\u00a0<em>-r<\/em>\u00a0flag makes the command list individual files, rather than collapsing them into root directory names only.<\/p>\n<p>The output will also include some extra information, which can be easily suppressed by including a couple of flags:<\/p>\n<p>git diff-tree \u2013no-commit-id \u2013name-only -r {hash}<\/p>\n<p>Here\u00a0<em>\u2013no-commit-id<\/em>\u00a0will supress the commit hashes from appearing in the output, and\u00a0<em>\u2013name-only<\/em>\u00a0will only print the file names, instead of their paths.<\/p>\n<hr \/>\n<h3>Q. How do you setup a script to run every time a repository receives new commits through push?<\/h3>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Hooks are local to every Git repository and are not versioned. Scripts can either be created within the hooks directory inside the \u201c.git\u201d directory, or they can be created elsewhere and links to those scripts can be placed within the directory.<\/p>\n<hr \/>\n<h3>Q. What is\u00a0git bisect? How can you use it to determine the source of a (regression) bug?<\/h3>\n<p>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,\u00a0<em>git bisect<\/em>\u00a0allows the user to perform a sort of binary search on the entire history of a repository.<\/p>\n<p>By issuing the command\u00a0<em>git bisect start<\/em>, the repository enters bisect mode. After this, all you have to do is identify a bad and a good commit:<\/p>\n<p>git bisect bad # marks the current version as bad<\/p>\n<p>git bisect good {hash or tag} # marks the given hash or tag as good, ideally of some earlier commit<\/p>\n<p>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:<\/p>\n<p>git bisect reset<\/p>\n<hr \/>\n<h3>Q. What are the different ways you can refer to a commit?<\/h3>\n<p>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\u00a0<em>git checkout {hash}<\/em>\u00a0command).<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<hr \/>\n<h3>Q. What is\u00a0git rebase\u00a0and how can it be used to resolve conflicts in a feature branch before merge?<\/h3>\n<p>In simple words,\u00a0<em>git rebase<\/em>\u00a0allows 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,\u00a0<em>git rebase<\/em>\u00a0can 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.<\/p>\n<hr \/>\n<h3>Q. How do you configure a Git repository to run code sanity checking tools right before making commits, and preventing them if the test fails?<\/h3>\n<p>This can be done with a simple script bound 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. For example, the following script:<\/p>\n<p>#!\/bin\/sh<br \/>\nfiles=$(git diff \u2013cached \u2013name-only \u2013diff-filter=ACM | grep \u2018.go$\u2019)<br \/>\nif [ -z files ]; then<br \/>\nexit 0<br \/>\nfi<br \/>\nunfmtd=$(gofmt -l $files)<br \/>\nif [ -z unfmtd ]; then<br \/>\nexit 0<br \/>\nfi<br \/>\necho \u201cSome .go files are not fmt\u2019d\u201d<br \/>\nexit 1<\/p>\n<p>\u2026 checks to see if any .go file that is about to be commited needs to be passed through the standard Go source code formatting tool\u00a0<em>gofmt<\/em>. By exiting with a non-zero status, the script effectively prevents the commit from being applied to the repository.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q1. What is GIT? Git is a distributed version control system and source code management (SCM) system with focus to handle small and large projects source code versions in the local repository with speed and efficiency. It is free and open source and its one of widly used versioning tools used ever worldwide. Q2. What&#8230;<\/p>\n","protected":false},"author":1,"featured_media":8570,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[38,52],"tags":[767,95,160,771,766,482,1441],"class_list":["post-8491","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-git","category-interview-questions-answers","tag-answer","tag-devops","tag-git","tag-git-interview","tag-interview","tag-questions","tag-version-control"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/8491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=8491"}],"version-history":[{"count":5,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/8491\/revisions"}],"predecessor-version":[{"id":25189,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/8491\/revisions\/25189"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/8570"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=8491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=8491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=8491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}