Top GIT Interview Questions & Answers
Top Git Interview Questions & Answers Collection:
Git is the most popular version control tool that is used to stores different versions of a file to track its changes.
Here, you can find Frequently Asked Git Interview Questions & Answers for jobs. These are top Interview Questions related to cloning existing git branch, pull & push changes, creating a new branch, and submodules. We will cover commonly used Git commands with examples.
Tutorials Class covers Interview Questions for Freshers as well as experienced professionals.
What is Git?
Git is the most popular version control tool that is used to manage project files along with their changes over time. It allows multiple persons to work together on a project.
Git stores all project-related information in a special .git
directory. It stores different versions of a file to track its changes.
What is Git Clone and How to Clone a Git repository?
Git Clone is a method to copy or download an existing Git repository to your local computer. We can use the git clone command to create a clone of the target repository.
How to Clone a Git repository using command:
Suppose we have a sample Git repository with URL https://gitlab.com/tutorialsclass/hello-world.git. Once you click Clone button, copy Git repository path available under option Clone with HTTPS
If the Git Repository path is https://gitlab.com/tutorialsclass/hello-world.git, you can use the following command to Clone Git repository.
Git command syntax:
git clone https://gitlab.com/tutorialsclass/hello-world.git
How you can see list of all branches in Git?
There are various Git commands to list branches:
List all branches in local and remote Git repositories:git branch -a
List all branches and along with commits as follows:git show-branch
List only local Git branchesgit branch
How do I create a new branch in Git?
You can use the git checkout command with -b
option to create and switch to a new branch.
Syntax: git checkout -b new-branch
How to switch to an another branch in Git?
You can switch to another branch using the git checkout command. Make
Syntax: git checkout branch_name
How can you add files to Git?
Once you created new files in Git project folder, you can add them in Git using git add
command. Only files that are added, can be pushed to make it part of Git repository.
Add selected files in the current branch:
git add file1.html, file2.txt
Add All files in the current branch:
When you have a lot of files and directories and want to add all, you can use the following commands:
1) Use git add command with dot (.)
git add *
2) Use git add command with –all
flaggit add -all
How can you commit a file in Git?
Git commit is a way to save your changes in the Git repository along with a message. Once you added files into git, you can commit them, and push to Git repository. Here is the example to commit your changes.
Syntax: git commit -m "this is sample change message"
How can you push changes to a remote branch in Git?
Git push command allows us to push or upload our changes to the remote repository. Here useful commands to push your local changes to a remote git repository.
Push changes to the remote master branch
The first time, you need to set the origin branch name where you want to push your changes.git push origin master
Push changes to the current origin
Once set the origin, you can simply push your changes to the current remote branch.git push
Push changes to all branches:git push origin --all
What is the difference between git fetch and git pull?
In simple words, git pull
is a combination of git fetch
(only for current branch) and a git merge
.
The git fetch
command is used to downloads all changes from a remote repository into your local. Git fetch is useful for getting the latest files and commit available in a remote repository. Git fetch does not merge changes to your current branch.
The git pull
command will fetch latest commits from your current branch only and automatically merge into your local. This can cause conflicts if multiple persons have modified same code/file, so need to take care before git pull.
What is git revert?
The git revert
command is used to undo the changes from a previous commit. This creates a new commit history to the project and it doesn’t modify existing commit history.
How to undo last commit using git reset?
There are many situations where you really want to undo the last commit.
Use soft reset:
Note the reset with –soft flag makes sure that the changes in undone revisions are also kept. After running the command, you’ll find the changes as uncommitted local modifications in your working copy.
git reset --soft HEAD~1
Use hard reset:
If you are sure that you don’t want to keep any changes, simply use the reset with –hard flag.
git reset --hard HEAD~1
How to merge one branch to another in Git?
First, you need to checkout into the target branch where you want to merge. Then, run the command git merge
with branch name, you want to merge into the current branch.
Example to merge current dev branch into master:
Let us suppose your current branch is dev
and you want to merge dev
into master
branch.
git checkout master
git merge dev
How to rename a local Git branch?
To rename the current branch, simply use following command:
git branch -m new-branch
To rename other branch, mention both old and new branch names:
git branch -m old_branch new_branch
Why SSH key is used in Git?
We need a secure communication channel to push code changes to the Git server such as GitLab or GitHub. An SSH key is a way of authentication without entering a username and password every time.
How to create SSH key and use for GitLab?
SSH is a preferred way to use Git as compared to HTTP.
SSH key is generated as pair. A public key that is stored on your machine and a private key that is saved in git-based servers such as GitLab or GitHub.
Steps to create & use SSH Key for GitLab:
- Open a terminal on Linux or macOS. Similarly, use command prompts on Windows.
- Generate a new ED25519 SSH key pair with command:
ssh-keygen -t ed25519 -C "username@mail.com"
or Generate RSA:ssh-keygen -t rsa -b 4096 -C "username@mail.com"
- Next, you can simply input a file path to save your SSH key pair or just press
Enter
- Then, you will be asked to set a “passphrase”. This will be used once you use git commands first time after SSH setup.
How to add an SSH key (that generated above) to your GitLab account:
- Get & Copy public SSH key to the clipboard by using following command:
macOS command:pbcopy < ~/.ssh/id_ed25519.pub
Linux command:xclip -sel clip < ~/.ssh/id_ed25519.pub
Linux command:cat ~/.ssh/id_ed25519.pub | clip
- Add your public SSH key to your GitLab account by:
Go to Settings > SSH Key > and press Add key
Now, you should be able to interact (clone, pull, push, etc) with git from the local machine without username and password.
What is a git submodule?
Git Submodule is a Git repository inside another Git repository. When your project contains independent folders and modules, you can set them as a submodule.
Git Submodules are very helpful when multiple people are working on different independent features in any project.
What are the common files to ignore in Git?
When you add or commit files in a git repository, there are some common files you can exclude or ignore. Because these files are not part of your application or project. These files may be related to debugging log, confidential files, editor or some other tools related files.
.gitignore
is the special file where you can mention files to be ignored as plain text. You can write a pattern for files/directories one per line. Anything after the hash (#) symbols will be treated as a comment.
List of Common files to ignore:
You can simply copy following text and add in .gitignore file.
# macOS files
.DS_Store
# log files
*.log
# Node Modules
node_modules
# Netbeans project files
nbproject
# node package file
package-lock.json
How to remove untracked files in Git?
You can use git clean
command to remove untracked files in local Git repository.
git clean -f
This command will permanently delete untracked files, therefore it is recommenced to take precautions.
You can see which files will be deleted with git clean using following command.
git clean -n