TOP 15 GIT Interview Questions And Answers
How will you find a list of files that has been modified in a particular commit?
The command to get a list of files that has been changed in a particular commit is:
git diff-tree –r {commit hash}1) -r flag allows the command to list individual files
2) commit hash lists all the files that were changed or added in the commit.Difference between git pull and git fetch?
The Git pull command retrieves and incorporates new changes or commits from a specific branch in your central repository, updating the corresponding branch in your local repository. Essentially, Git pull combines the functionalities of git fetch and git merge.
Similarly, Git fetch serves the same purpose but operates in a slightly distinct manner. When you execute a git fetch, it retrieves all the recent commits from the designated branch and stores them in a new branch within your local repository. To reflect these changes in your target branch, you need to follow git fetch with a git merge.
How do you revert a commit that has already been pushed and made public?
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
How do you cherry-pick a merge commit?
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
Explain the difference between rebasing and merge in Git?
The Git rebase command enables developers to incorporate changes from one branch into another, while the Git merge command allows for the merging of branches in Git.
Both Git rebase and merge serve the purpose of integrating changes from one branch to another. However, they differ in their usage. Git rebase is used to move a feature branch into a master branch, whereas Git merge adds a new commit while preserving the existing history.
When working individually or in a small team, it is recommended to use rebase. On the other hand, when collaborating with a large team, it is advisable to use merge.
What is a version control system (VCS)?
VCS keeps track of the contributions of the developers working as a team on the projects. They maintain the history of code changes done and with project evolution, it gives an upper hand to the developers to introduce new code, fixes bugs, and run tests with confidence that their previously working copy could be restored at any moment in case things go wrong.
Tell me something about git stash?
Git stash can be used in cases where we need to switch in between branches and at the same time not wanting to lose edits in the current branch. Running the
git stash
command basically pushes the current working directory state and index to the stack for future use and thereby providing a clean working directory for other tasks.What is the command used to delete a branch?
To delete a branch we can simply use the command
git branch –d [head]
.To delete a branch locally, we can simply run the command:
git branch -d <local_branch_name>
To delete a branch remotely, run the command:
git push origin --delete <remote_branch_name>
Deleting a branching scenario occurs for multiple reasons. One such reason is to get rid of the feature branches once it has been merged into the development branch.
What differentiates between the commands git remote and git clone?
git remote
command creates an entry ingit config
that specifies a name for a particular URL. Whereasgit clone
creates a new git repository by copying an existing one located at the URL.How will you resolve conflict in Git?
Conflicts occur whenever there are multiple people working on the same file across multiple branches. In such cases, git won't be able to resolve it automatically as it is not capable of deciding what changes has to get the precedence.
Following are the steps are done in order to resolve git conflicts:
1. Identify the files that have conflicts.
2. Discuss with members who have worked on the file and ensure that the required changes are done in the file.
3. Add these files to the staged section by using the git add command.
4. Commit these changes using the git commit command.
5. Finally, push the changes to the branch using the git.
Define “Index”?
Before making commits to the changes done, the developer is given provision to format and review the files and make innovations to them. All these are done in the common area which is known as ‘Index’ or ‘Staging Area’.
What has to be run to squash multiple commits (last N) into a single commit?
Squashing multiple commits to a single one overwrites the history which is why it is recommended to be done using full caution. This step can be done by running the command:
git rebase -i HEAD~{{N}}
where {{N}} represents the number of commits needed to be squashed.Can you tell something about git reflog?
This command tracks every single change made in the repository references (that can be branches or tags) and also maintains the branches/tags log history that was either created locally or checked out. Reference logs such as the commit snapshot of when the branch was created or cloned, checked-out, renamed, or any commits made on the branch are maintained by Git and listed by the ‘reflog’ command.
This recovery of the branch is only possible when 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 should be executed in the repository that had the lost branch.
What is the functionality of “git cherry-pick” command?
This command is used to introduce certain commits from one branch onto another branch within the repository. The most common use case is when we want to forward- or back-port commits from the maintenance branch to the development branch.
What is the purpose of branching and its types?
It allows the user to switch between the branches to keep the current work in sync without disturbing master branches and other developer’s work as per their requirements.1) 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.
2) Task branching - In this branch, 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.
3) 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.
Conclusion
In this article, we have seen the introduction and scope of git along with the most commonly asked git interview questions. The popularity of Git is growing at an amazing pace and it is predicted that one day all the developers around the world would be working together as one single entity with the help of git.
Subscribe to my newsletter
Read articles from Prabhat Yadav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Prabhat Yadav
Prabhat Yadav
I'm deeply passionate about cloud computing and DevOps, driven by an insatiable curiosity to master cutting-edge technologies. From AWS to Docker, Kubernetes, Jenkins, Linux, Git, and Terraform, I thrive on exploring and pushing the boundaries of what's possible. Beyond the code, I believe in the power of collaboration and shared learning experiences. With each interaction, I cherish the opportunity to grow together and build meaningful connections. As I embark on this exciting journey, I'm eager to contribute my expertise and bring a human touch to the ever-evolving landscape of cloud and DevOps.