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 branches
git 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 flag
git 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

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:

  1. Open a terminal on Linux or macOS. Similarly, use command prompts on Windows.
  2. 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"
  3. Next, you can simply input a file path to save your SSH key pair or just press Enter
  4. 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:

  1. 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
  2. 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.