Freshers Git Interview Questions

What is the difference between git add and git commit?

git addgit commit
Moves changes to the staging areaSaves the staged changes as a snapshot in the repository
Can be used many times before a commitCreates a permanent point in the history
Has no messageNeeds a message (-m)
Changes can be unstaged with git restore --stagedCan 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.

Terminal output

$ git log –oneline
f411fbd (HEAD -> main) Second
0057196 First
$ git show –oneline –no-patch HEAD~1
0057196 First

  • HEAD is the current commit.
  • HEAD~1 (or HEAD^) is its parent, HEAD~2 is two commits back.
  • If HEAD points to a commit and not a branch, it is called a detached HEAD.

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 status to 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.

Terminal output

$ 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 pop brings the changes back and removes the stash.
  • git stash apply brings them back and keeps the stash.
  • git stash -u also stashes untracked files.

See Git Stash.

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:

Terminal output

$ 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?

Terminal output

$ 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?

CloneFork
A Git command (git clone)A feature of GitHub or GitLab (button)
Copies a repository to your computerCopies a repository to your account on the server
You must have read accessWorks on any public repository
Used to work locallyUsed 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 git blame?

Terminal output

$ git blame page.txt
^555961f (Robin Sharma 2026-01-10 10:42:00 +0000 1) Title
a88d5160 (Robin Sharma 2026-01-10 10:56:00 +0000 2) Line 2 updated
a88d5160 (Robin Sharma 2026-01-10 10:56:00 +0000 3) Line 3

Each line shows the short commit hash, the author, the date and the line. With the hash, run git show <hash> to see the full change and message. In VS Code and GitHub, the “blame” view shows the same.