All Git Interview Questions
What is the staging area in Git?
The staging area, also called the index, is a waiting room between your working directory and the repository. You put changes in it with git add, and git commit saves only what is staged.
It is useful because you can choose exactly what goes into a commit. If you changed five files but only two are for one fix, stage those two, commit them, and leave the rest for another commit.
$ git add a.txt
$ git status -s
A a.txt
?? b.txt
Here A means a.txt is staged, and ?? means b.txt is not. See Git Workflow.
What is the difference between git add and git commit?
| git add | git commit |
|---|---|
| Moves changes to the staging area | Saves the staged changes as a snapshot in the repository |
| Can be used many times before a commit | Creates a permanent point in the history |
| Has no message | Needs a message (-m) |
Changes can be unstaged with git restore --staged | Can be undone with git reset or git revert |
Normal order: edit files, git add, then git commit. See Git Add and Git Commit.
What is HEAD in Git?
HEAD is a special pointer which tells Git where you are now. Usually it points to the current branch, and that branch points to its latest commit. When you make a new commit, the branch and HEAD move forward.
$ git log –oneline
f411fbd (HEAD -> main) Second
0057196 First
$ git show –oneline –no-patch HEAD~1
0057196 First
HEADis the current commit.HEAD~1(orHEAD^) is its parent,HEAD~2is two commits back.- If
HEADpoints to a commit and not a branch, it is called a detached HEAD.
What is the difference between git merge and git rebase?
| git merge | git rebase |
|---|---|
| Joins two branches, usually with a merge commit | Moves your commits on top of another branch |
| Keeps the real history as it happened | Rewrites commits (new hashes) for a straight line |
| Safe on shared branches | Should not be used on pushed, shared commits |
| Conflicts are solved once | Conflicts may appear for each replayed commit |
A common rule: rebase your own local feature branch to update it, and merge when the branch goes back to main. See Git Merge and Git Rebase.
What is the difference between git reset and git revert?
| git reset | git revert |
|---|---|
| Moves the branch pointer back, removing commits from the history | Creates a new commit which cancels the old commit |
| Rewrites history | History is kept |
| Use for local commits not pushed yet | Use for commits already pushed and shared |
| Has modes: soft, mixed, hard | Can undo any older commit, even in the middle |
See Git Reset and Git Revert.
What is the difference between soft, mixed and hard reset in Git?
| Mode | Staging area | Working files |
|---|---|---|
--soft | Keeps the changes staged | Not changed |
--mixed (default) | Cleared, changes unstaged | Not changed |
--hard | Cleared | Changes deleted |
$ git reset –soft HEAD~1
$ git status -s
M f.txt
$ git commit -q -C ORIG_HEAD
$ git reset HEAD~1
Unstaged changes after reset:
M f.txt
$ git status -s
M f.txt
$ git commit -q -a -C ORIG_HEAD
$ git reset –hard HEAD~1
HEAD is now at 2875ee0 First
$ git status -s
$ cat f.txt
one
Look at the M (staged) after soft, M (not staged) after mixed, and the clean status with the line “two” gone after hard. See Git Reset.
What is a merge conflict and how do you resolve it?
A merge conflict happens when Git cannot join two changes automatically, mostly because two branches changed the same line of a file differently (or one deleted a file the other changed).
Git stops the merge and puts markers in the file:
<<<<<<< HEAD
Title: Welcome to Tutorials Class
=======
Title: Welcome to our shop
>>>>>>> feature
Steps to resolve:
- Run
git statusto see the files which are “both modified”. - Open each file, choose the final text, and delete the three marker lines.
- Stage the file with
git add file. - Finish with
git commit.
To cancel the merge, use git merge --abort. Full example: Git Merge Conflict.
What is git stash and when do you use it?
git stash takes your uncommitted changes and keeps them in a stack, giving you a clean working directory. You use it when you need to switch to another branch quickly, for example for an urgent bug fix, but your current work is not ready for a commit.
$ echo "unfinished" >> app.txt
$ git stash
Saved working directory and index state WIP on main: 0f2bd93 First
$ git stash list
stash@{0}: WIP on main: 0f2bd93 First
$ git stash pop
On branch main
Changes not staged for commit:
(use "git add <file>…" to update what will be committed)
(use "git restore <file>…" to discard changes in working directory)
modified: app.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (f42418453db53316b813fc611460606e4c2b9896)
git stash popbrings the changes back and removes the stash.git stash applybrings them back and keeps the stash.git stash -ualso stashes untracked files.
See Git Stash.
What is git cherry-pick?
git cherry-pick takes a single commit from anywhere in the repository and applies it on top of your current branch as a new commit (with a new hash).
git switch main
git cherry-pick a3f9c1e
Typical use: take one bug fix from the develop branch to a release branch, without taking the other unfinished work. Use -x to add the original hash to the message. See Git Cherry Pick.
What is the difference between a lightweight tag and an annotated tag in Git?
| Lightweight tag | Annotated tag |
|---|---|
| Only a name pointing to a commit | A full object with tagger, date and message |
git tag v1.0 | git tag -a v1.0 -m "Version 1.0" |
| No extra information | git show v1.0 shows who created it, when and why |
| Good for private or temporary marks | Recommended for public releases |
See Git Tag.
How do you delete a branch in Git (local and remote)?
# delete a local branch (safe, only if merged)
git branch -d feature-login
# force delete a local branch (even if not merged)
git branch -D feature-login
# delete a remote branch
git push origin --delete feature-login
You cannot delete the branch you are on. Switch to another branch first. See Git Branch.
How do you unstage a file in Git?
You added a file with git add by mistake. To remove it from the staging area:
$ git status -s
M a.txt
$ git restore –staged a.txt
$ git status -s
M a.txt
The file stays changed in your folder. The old way, git reset HEAD a.txt, does the same. See Git Restore.
How do you change the last commit message in Git?
$ git commit –amend -m "Add first file"
[main 48a597b] Add first file
Date: Sat Jan 10 10:42:00 2026 +0000
1 file changed, 1 insertion(+)
create mode 100644 a.txt
$ git log –oneline
48a597b (HEAD -> main) Add first file
Amend creates a new commit, so the hash changes. If the commit was already pushed, you need a force push, which is risky on shared branches. See Git Commit Amend.
What is the difference between git clone and fork?
| Clone | Fork |
|---|---|
A Git command (git clone) | A feature of GitHub or GitLab (button) |
| Copies a repository to your computer | Copies a repository to your account on the server |
| You must have read access | Works on any public repository |
| Used to work locally | Used to suggest changes to a project you do not own |
A normal open source flow is: fork the project, clone your fork, push your changes to it, and open a pull request. See Git and GitHub.
What is origin in Git?
origin is just a short name (alias) for the remote repository URL. When you run git clone, Git automatically names the source origin. So git push origin main means “push the main branch to the remote called origin”.
git remote -v
The name is not special. You can rename it or add other remotes (like upstream for a fork). See Git Remote.
What is a pull request?
A pull request (PR) is a feature of GitHub, GitLab and Bitbucket (GitLab calls it a merge request). You push your branch to the server and ask the team to review it and merge it into main.
- The team can read the changes, comment on lines and ask for corrections.
- Automatic tests can run on the PR.
- After approval, the branch is merged.
A pull request is not a Git command, it is a feature of the hosting website. See Git Branching Strategy.
What is a detached HEAD in Git?
Normally HEAD points to a branch. When you check out a commit, a tag or a remote branch directly, HEAD points to that commit. This is the detached HEAD state.
$ git switch –detach HEAD~1
HEAD is now at 0057196 First
$ git status
HEAD detached at 0057196
nothing to commit, working tree clean
You can look around, but new commits there belong to no branch, and Git may delete them later. To keep them, create a branch: git switch -c new-branch. To leave, use git switch main.
What is a fast-forward merge in Git?
If the target branch has no new commits since your branch was created, Git does not need a merge commit. It just moves the pointer of the target branch forward to the last commit of your branch.
$ git merge feature
Updating 0057196..22ea2c5
Fast-forward
g.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 g.txt
To force a merge commit even in this case, use git merge --no-ff. See Git Merge.
How do you squash commits in Git?
Squashing means joining several small commits into a single clean commit. There are three common ways.
1. Interactive rebase (change “pick” to “squash” for the commits to join):
git rebase -i HEAD~3
2. Reset soft: go back and make one new commit.
$ git reset –soft HEAD~3
$ git commit -q -m "Add feature"
$ git log –oneline
e56c328 (HEAD -> main) Add feature
310ae0b Base
3. Squash merge: git merge --squash feature stages all the changes of a branch, and then you commit once.
Do not squash commits which are already pushed and shared.
What is git reflog and how can it help?
The reflog records every move of HEAD and branch tips in your local repository. Even if a commit is not in any branch anymore, you can find it there.
$ git reflog
0057196 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~1
f411fbd HEAD@{1}: commit: Second
0057196 (HEAD -> main) HEAD@{2}: commit (initial): First
$ git reset –hard HEAD@{1}
HEAD is now at f411fbd Second
$ git log –oneline
f411fbd (HEAD -> main) Second
0057196 First
The reflog is local only, and entries expire after some time (90 days by default). See Git Reflog.
- « Previous
- 1
- 2
- 3
- Next »