Top 9 GIT Interview Questions and Answers for 2020 | FAQ
Here are some of the frequently asked questions about GIT in job interviews.
In interviews, you will be asked for various git commands that I have already explained in the git tutorial. Go through it.
GIT Interview Questions and Answers
Now let’s start with some of the Git questions usually asked in the job interviews.
1. Explain Git Lifecycle.
Answer: Refer to this Git Lifecycle diagram. This will give answers to many of the git questions.
It is expected from you to explain all four stages (local directory, staging area, local repo and remote repo) along with basic git commands. I have explained everything in our git tutorial.
Explain how the transition happens starting from modifying code to pushing it to the remote repo using git commands.
2. What is Origin in Git?
Answer: It is the short name given the original remote repository from where the code is cloned.
When you run the clone command, you have to mention the remote repository URL as a parameter. This URL parameter becomes origin.
Now next time, whenever you want to use remote repo URL (Especially, in push command, you need remote repo URL. ), you don’t need to mention the complete URL. Instead of that, you can mention “origin”.
3. How to switch from one branch to another?
Answer: Usually, one local branch is used to work on one feature or defect. Git provides a provision to work on multiple features or defects by creating multiple branches.
You can switch the branch with the git checkout command.
git checkout <branch-name-you-want-to-switch-to>
4. How to get a list of all local branches?
Answer: Use the git branch command.
It will list out all the local branches.
5. How to colorize the git difference?
Answer: Use diff command to see check the differences between files in the local directory and files in the staging area.
git diff --color
Git diff command shows us uncommitted changes since the last commit.
Use –color option to check the difference so that output will be colorized and more visual.
You can see the different colors for the words that are changed.
Note: You will not see the difference if all the modified files are committed to the local repository.
6. How to check the committed changes in git?
Answer: You can use the git show command to check the metadata and content changes of the specific commit.
git show --color
The –color is an optimal parameter to see the output in different colors.
7. Can we set the different usernames for each repo /project?
Answer: Yes, you can do that. While configuring username, use —local option.
git config --local user.name "<Your_Name>"
8. What are the advantages of Git?
Answer: It has several advantages over a centralized server model like SVN.
- Git is a distributed model.
- You can capture the commit and build up locally before pushing it back.
- When you get the local repository, you don’t need the internet to work on. The internet is required when you want to push the code to the remote repo.
- It is more robust as it does not have a single point of failure.
9. What is the difference between Git Pull and Git Fetch?
Answer: Basically git pull is the combination of two command- git fetch and git merge.
When you run git fetch command, it fetches the recent commit into the local repository. After that, git merge command merges changes to the local directory.
If you want to pull the changes from the remote repo and merge it into the local directory, it easy to convenient to use git pull command, instead of executing two different commands.
These are the GIT interview questions and answers. If there is anything you want to ask, feel free to write in the comment. If you find these questions useful, comment your thought.
I will keep adding more questions to this list in the coming day. Stay tuned!