All Git Interview Questions

What is Git?

Git is a free and open source version control tool. It records the changes of your project files over time, so you can see the history, go back to an older version and work with other people on the same project.

It is distributed, which means every developer has a full copy of the repository with its history on their own computer. Git stores all this information in a hidden .git folder inside the project. It was created by Linus Torvalds in 2005.

Read more in Git Introduction.

What is Git Clone and How to Clone a Git repository?

git clone copies an existing Git repository to your local computer. You get all the files, all branches and the full history, and Git also sets a remote named origin which points to the original place.

Syntax:

git clone <repository-url>
git clone <repository-url> <folder-name>

Example:

git clone https://github.com/username/project.git

Git creates a folder with the name of the repository (project) and downloads everything in it. To use a different folder name, write it after the URL. The URL can be HTTPS, SSH or a local path.

See the chapter Git Clone for more options like --branch and --depth 1.

How you can see list of all branches in Git?

Use the git branch command. The current branch is marked with a star.

Terminal output

$ git branch
develop
feature-login
* main

CommandWhat it shows
git branchOnly local branches
git branch -rOnly remote branches
git branch -aLocal and remote branches
git branch -vBranches with their last commit
git branch --show-currentName of the current branch only

See Git Branch for more.

How do I create a new branch in Git?

There are two ways. git branch only creates the branch, and git switch -c creates it and moves you to it.

Terminal output

$ git branch feature-cart
$ git switch -c feature-search
Switched to a new branch 'feature-search'
$ git branch
feature-cart
* feature-search
main

The old command git checkout -b feature-search does the same as git switch -c. A new branch starts from the commit where you are now. See Git Branch.

How to switch to an another branch in Git?

Use git switch with the branch name. The older command git checkout also works.

git switch develop
git checkout develop

Git changes the files of your folder to match the branch. If you have uncommitted changes which conflict with the other branch, Git stops you. Then commit them, or put them aside with git stash.

To go back to the previous branch, use git switch -. More in Git Checkout and Switch.

How can you add files to Git?

Use the git add command. It moves the changes to the staging area, and only staged changes go in the next commit.

Terminal output

$ git add index.html
$ git add *.html
$ git add .
$ git status -s
A about.html
A index.html
A style.css

CommandWhat it adds
git add file1 file2Only the named files (separate with a space, not a comma)
git add .All new and changed files in the current folder
git add -AAll changes in the whole repository
git add -uOnly already tracked files (changed and deleted)
git add -pChoose the parts of a file one by one

See Git Add.

How can you commit a file in Git?

First stage the file with git add, and then commit it with a message.

Terminal output

$ git add hello.txt
$ git commit -m "Add hello.txt"
[main (root-commit) 7757fcb] Add hello.txt
1 file changed, 1 insertion(+)
create mode 100644 hello.txt

To commit tracked files without a separate add, use git commit -a -m "message". To commit only one specific file, you can write its name: git commit hello.txt -m "message". A commit stays on your computer until you push it. See Git Commit.

How can you push changes to a remote branch in Git?

The git push command uploads your local commits to the remote repository.

git push origin main
git push -u origin feature-login
git push
git push origin --all
  • git push origin main pushes the local main branch to the remote named origin.
  • The option -u links your local branch with the remote branch. After this you can use only git push.
  • --all pushes all local branches.

If Git rejects the push, the remote has new commits. Run git pull first and push again. See Git Push.

What is the difference between git fetch and git pull?

git pull is git fetch followed by git merge.

git fetchgit pull
Downloads new commits from the remoteDownloads and merges them into your current branch
Your files and branch do not changeYour files and branch change
Safe, you can review first with git log HEAD..origin/mainCan create a merge commit or a conflict
Works for all branches (--all)Works for the current branch

Use fetch when you want to see what changed before merging, and pull when you just want to update your branch. See Git Fetch and Pull.

What is git revert?

git revert undoes the changes of a previous commit by adding a new commit that does the opposite. The old commit stays in the history, so nothing is rewritten.

git revert HEAD
git revert <commit-hash>

This is the safe way to undo a commit which is already pushed, because other people’s history is not affected. Compare with git reset, which moves the branch back and removes commits. See Git Revert.

How to undo last commit using git reset?

Use git reset with HEAD~1, which means “one commit before the current one”. The option decides what happens to your changes.

Terminal output

$ git reset –soft HEAD~1
$ git status -s
M file.txt
$ git log –oneline
3b28266 (HEAD -> main) First

CommandResult
git reset --soft HEAD~1Commit is removed, changes stay staged
git reset HEAD~1Commit is removed, changes stay in files, not staged
git reset --hard HEAD~1Commit is removed and the changes are deleted

Use this only for commits which are not pushed. For pushed commits use git revert. See Git Reset.

How to merge one branch to another in Git?

First move to the branch where you want the changes (the target), and then run git merge with the name of the branch to bring in.

Example: merge dev into main.

Terminal output

$ git switch main
Already on 'main'
$ git merge dev
Updating a17f7f4..5a62a93
Fast-forward
new.html | 1 +
1 file changed, 1 insertion(+)
create mode 100644 new.html

If both branches changed the same lines, Git shows a conflict which you must solve by hand. See Git Merge and Git Merge Conflict.

How to rename a local Git branch?

Use git branch -m (move).

Terminal output

$ git branch -m old-name new-name
$ git branch
* main
new-name

To rename the branch you are currently on, give only the new name: git branch -m new-name.

If the branch is already pushed, rename it on the remote too: push the new name and delete the old one.

git push origin -u new-name
git push origin --delete old-name

Why SSH key is used in Git?

We need a secure way to talk to the Git server (GitHub, GitLab or Bitbucket) when we push and pull code. An SSH key gives this authentication without entering a username and password every time.

  • The communication is encrypted.
  • The private key never leaves your computer, only the public key is shared.
  • You can use a different key for each computer, and remove one key if the computer is lost.

See Git SSH Key to create one.

How to create SSH key and use for GitLab?

SSH is a better way to connect to Git servers than HTTPS, because you do not enter your password every time. An SSH key is a pair: a private key stays on your computer and a public key is added to GitLab.

  • Open the terminal (Git Bash or PowerShell on Windows).
  • Create a key: ssh-keygen -t ed25519 -C "your-email@example.com". Press Enter to accept the default file, and set a passphrase if you like. (For old systems use ssh-keygen -t rsa -b 4096 -C "your-email@example.com".)
  • Show the public key: cat ~/.ssh/id_ed25519.pub and copy the full line.
  • In GitLab go to Preferences, then SSH Keys, paste the key, write a title and click Add key.
  • Test the connection: ssh -T git@gitlab.com.
  • Clone with the SSH URL, like git clone git@gitlab.com:username/project.git.

The same steps work for GitHub (Settings, then SSH and GPG keys). Detailed steps are in Git SSH Key.

What is a git submodule?

A Git submodule is a Git repository placed inside another Git repository. The main project keeps only a link to a specific commit of the submodule, and both have their own separate history.

It is useful when a project uses a shared library or a component which has its own repository and its own team. The links are saved in the .gitmodules file.

git submodule add https://github.com/user/library.git libs/library
git clone --recurse-submodules <url>
git submodule update --init --recursive

The common problem is that people forget to initialize or update the submodule after a clone. See Git Submodule.

What are the common files to ignore in Git?

Some files are not part of your source code, and you should not commit them. You list them in a file named .gitignore, one pattern per line. A line starting with # is a comment.

TypeExamples
Dependenciesnode_modules/, vendor/
Logs and temporary files*.log, *.tmp, *.cache
Secrets and local settings.env, *.pem, config.local.php
Build outputdist/, build/, *.class
System files.DS_Store, Thumbs.db
Editor files.idea/, .vscode/, *.swp
# dependencies
node_modules/
vendor/

# logs and secrets
*.log
.env

# system files
.DS_Store
Thumbs.db

Note that .gitignore does not work for files which are already tracked. See Git Ignore.

How to remove untracked files in Git?

Use the git clean command. It deletes untracked files, so first do a dry run with -n.

Terminal output

$ git clean -n
Would remove temp1.txt
Would remove temp2.txt
$ git clean -f
Removing temp1.txt
Removing temp2.txt
$ git status -s
?? cache/

The first command shows what will be deleted. The second deletes the files. The folder cache is left, because folders need the option -d:

Terminal output

$ git clean -fd
Removing cache/
$ git status -s

OptionMeaning
-nDry run, only show
-fForce, delete the files
-dAlso delete untracked folders
-xAlso delete ignored files

Warning: deleted files do not go to the recycle bin and cannot be recovered by Git.

What is the difference between Git and GitHub?

GitGitHub
A command line tool for version controlA website (service) that hosts Git repositories
Installed and used on your computerUsed in the browser, in the cloud
Works offlineNeeds internet
Made by Linus Torvalds in 2005A company (now owned by Microsoft) started in 2008

GitHub adds pull requests, issues, code review and automation on top of Git. GitLab and Bitbucket are similar services. See Git and GitHub.

What is a repository in Git?

A repository (or repo) is a project folder which Git tracks. It contains your files and the full history of all changes. The history is stored in a hidden folder named .git, which Git creates with git init or git clone.

  • Local repository: the one on your computer.
  • Remote repository: a copy on a server such as GitHub or GitLab.
  • Bare repository: a repository without working files, used on servers (git init --bare).

See Git Init.