Git 31 Fef
Git 31 Fef
Git is a Distributed Version Control system(DVCS). It lets you track changes made to a file
and allows you to revert back to any particular change that you wish.
It is a distributed architecture that provides many advantages over other Version Control
Systems (VCS) like SVN. One of the major advantages is that it does not rely on a central
server to store all the versions of a project’s files.
Instead, every developer “clones” a copy of a repository I have shown in the diagram with
“Local repository” and has the full history of the project available on his hard drive. So when
there is a server outage all you need to do to recover is one of your teammate’s local Git
repository.
There is a central cloud repository where developers can commit changes and share them
with other teammates.
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.
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.
7. Mention the various Git repository hosting functions.
Github
Gitlab
Bitbucket
SourceForge
GitEnterprise
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.
1. A .git subdirectory with all the Git related revision history of your repository.
2. A working tree, or checked out copies of your project files.
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] used to switch from one branch to another.
git checkout -b [branch name] creates a new branch and also switches to it.
Intermediate level Questions
17. How to resolve a conflict in Git?
The following steps will resolve conflict in Git-
18. 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>
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.
20. 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:
Instructor-led Sessions
Real-life Case Studies
Assignments
Lifetime Access
Explore Curriculum
If you want to fetch the log references of a particular branch or tag then run the command – “ git
reflog <ref_name>”.
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.
26. 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
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}.
28. How do you find a list of files that have changed in a particular commit?
For this answer instead of just telling the command, explain what exactly this command will do.
To get a list file that has changed in a particular commit use the below command:
git diff-tree -r {hash}
Given the commit hash, this will list all the files that were changed or added in that commit. The -r
flag makes the command list individual files, rather than collapsing them into root directory names
only.
You can also include the below-mentioned point, although it is totally optional but will help in
impressing the interviewer.
The output will also include some extra information, which can be easily suppressed by including
two flags:
git diff-tree --no-commit-id --name-only -r {hash}
Here –no-commit-id will suppress the commit hashes from appearing in the output, and –name-only
will only print the file names, instead of their paths.
git config –global user.name “Your Name” : This command will add a username.
git config –global user.email “Your E-mail Address”: This command will add an email id.
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.
34. Why is it desirable to create an additional commit rather than amending an existing
commit?
There are a couple of reasons for this –
7. 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.
8. Abusing “git commit- amend” can result in the growth of a small commit and acquire
unrelated changes.
36. In Git, how would you return a commit that has just been pushed and made open?
One or more commits can be reverted through the use of git revert. This command, in a true sense,
creates a new commit with patches that cancel out the changes introduced in specific commits. If in
case the commit that needs to be reverted has already been published or changing the repository
history is not an option then in such cases, git revert can be used to revert commits. If you run the
following command then it will revert the last two commits:
git revert HEAD~2..HEAD
Alternatively, there is always an option to check out the state of a particular commit from the past
and commit it anew.
37. 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.
This means that git reset <paths> is exactly the opposite of git add <paths>.
38. 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.
39. Tell me 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.
40. 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.
41. What are the different ways you can refer to a commit?
In Git each commit has a unique hash. These hashes are used to identify the corresponding
commits in various scenarios, for example, while trying to checkout a particular state of the
code using the git checkout {hash} command.
Along with this, Git maintains a number of aliases to certain commits, known as refs. Also,
every tag that is created in the repository effectively becomes a ref and that is exactly why
you can use tags instead of committing hashes in various git commands. Git also maintains a
number of special aliases that are changed based on the state of the repository, such as
HEAD, FETCH_HEAD, MERGE_HEAD, etc.
In Git, commits are allowed to be referred to as relative to one another. In the case of merge
commits, where the commit has two parents, ^ can be used to select one of the two
parents, for example, HEAD^2 can be used to follow the second parent.
And finally, refspecs are used to map local and remote branches together. However, these
can also be used to refer to commits that reside on remote branches allowing one to control
and manipulate them from a local git environment.
Consider:
git cherry-pick <commit-hash>
45. How do you find a list of files that have changed in a particular commit?
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 suppress the commit hashes from appearing in the output, and –name-only
will only print the file names, instead of their paths.
Advanced level Questions
46. How do you squash the last N commits into a single commit?
There are two options to squash the 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})”
47. 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.
48. How do you configure a Git repository to run code sanity checking tools right before
making commits, and preventing them if the test fails?
I will suggest you to first give a small introduction to sanity checking.
Sanity or smoke test determines whether it is possible and reasonable to continue testing.
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.
Note: The branch will be recoverable from your working directory only if the branch ever existed in
your local repository i.e. the branch was either created locally or checked-out from a remote
repository in your local repository for Git to store its reference history logs.
This command must be executed in the repository that had the lost branch. If you consider the
remote repository situation, then you have to execute the reflog command on the developer’s
machine who had the branch.
As you can see from the above snapshot, the highlighted commit id: e2225bb along with the HEAD
pointer index:4 is the one when ‘preprod’ branch was created from the current HEAD pointer
pointing to your latest work.
Step 3: Recover
If you want to recover back the ‘preprod‘ branch then use the command ‘git checkout’ passing the
HEAD pointer reference with the index id – 4. This is the pointer reference when ‘preprod’ branch
was created long commit id highlighted in the output screenshot.