Git Junior cheat sheet: interview answers
Every question of this level with its correct answer and explanation, on one page for a quick review before the interview.
1. What makes Git a distributed version control system?
Correct answer: Every clone has the full history and can work offline
A clone contains every commit, so you can commit, branch and view history without a server. A remote like GitHub is just another copy that the team agrees to sync with.
2. What are the three main areas a file moves through in Git?
Correct answer: Working tree, staging area (index), repository
You edit files in the working tree, choose what goes into the next commit with
git add(the index), andgit commitstores that snapshot in the repository.3. What does
git add file.jsdo?Correct answer: Stages the file's current content for the next commit
git addcopies the file's current state into the index. If you edit the file again after that, the new changes aren't staged until you rungit addagain.4. What does a Git commit store?
Correct answer: A snapshot of the project plus author, message and parent
Conceptually each commit is a full snapshot (unchanged files are reused, not duplicated) with metadata and a link to its parent. Diffs are computed when you ask for them.
5. What identifies a commit in Git?
Correct answer: A hash of its content and metadata
A commit's ID is a SHA-1 (or SHA-256) hash of its tree, parents, author and message. Change anything and the hash changes, which is why rewriting history creates new commits.
6. Which command shows modified, staged and untracked files?
Correct answer:
git statusgit statuscompares the working tree, the index and the last commit.git loglists commits, andgit showdisplays one commit.7. What does plain
git diffshow?Correct answer: Changes in the working tree that are not staged yet
git diffcompares the working tree with the index. Usegit diff --stagedto see what will go into the next commit, andgit diff HEADfor both.8. How do you see what is staged for the next commit?
Correct answer:
git diff --staged--staged(or its synonym--cached) compares the index with the last commit. It's worth checking before committing to make sure nothing extra is included.9. You added
.envto.gitignore, but Git still tracks changes to it. Why?Correct answer: The file was already committed; ignore rules apply only to untracked files
Run
git rm --cached .envto stop tracking it while keeping the file on disk, then commit. If it contained secrets, they are still in history, so rotate them.10. What does
git rm --cached filedo?Correct answer: Stops tracking the file but keeps it on disk
The file is removed from the index, so the next commit records its deletion from the repository, while the working copy stays. Without
--cachedit would be deleted from disk too.11. Which
.gitignoreline ignores everynode_modulesfolder in the project?Correct answer:
node_modules/A pattern without a leading slash matches at any depth, and the trailing
/limits it to directories. A leading/would match only at the repository root.12. Why doesn't Git track an empty directory?
Correct answer: Git tracks files, and a directory exists only through its files
A tree object lists files and subtrees, so an empty folder has nothing to record. The usual workaround is a placeholder file such as
.gitkeep.13. What does
git clone <url>do?Correct answer: Copies the whole repo and checks out the default branch
Clone downloads all commits, sets up a remote called
originand checks out the default branch. Shallow clones (--depth) are an option when history isn't needed.14. What is
origin?Correct answer: The default name of the remote you cloned from
originis just a conventional name for a remote URL. You can rename it or add others, for exampleupstreamfor the original repository of a fork.15. What is the difference between
git fetchandgit pull?Correct answer:
fetchonly downloads;pulldownloads and integrates into your branchgit pullisgit fetchfollowed by a merge (or a rebase, if configured). Fetching first lets you inspect incoming commits before changing your branch.16.
git pushis rejected with "non-fast-forward". What does it mean?Correct answer: The remote has commits you don't have; integrate them first
Someone pushed new commits, and your branch doesn't include them. Pull (merge or rebase), resolve any conflicts, then push. Forcing would overwrite their work.
17. What is a branch in Git?
Correct answer: A movable pointer to a commit
A branch is a tiny file containing a commit hash; when you commit on it, the pointer moves forward. That's why creating and switching branches is instant.
18. What is
HEAD?Correct answer: A pointer to what is checked out, usually a branch
HEADnormally points to a branch, which points to a commit. New commits go to the branchHEADrefers to.19. How do you create a branch and switch to it in one command?
Correct answer:
git switch -c featuregit switch -c(or the oldergit checkout -b) creates the branch at the current commit and switches to it.git branch featurealone only creates it.20. What is a fast-forward merge?
Correct answer: Moving the branch pointer ahead when there are no divergent commits
If
mainhasn't moved sincefeaturebranched off, merging just movesmaintofeature's commit, with no merge commit.--no-ffforces a merge commit anyway.21. When does Git create a merge commit?
Correct answer: When both branches have new commits since they diverged
With divergent histories Git combines them in a commit with two parents. Conflicts or not, that commit is needed; with a linear history a fast-forward is enough.
22. What do the markers
<<<<<<<,=======and>>>>>>>in a file mean?Correct answer: An unresolved merge conflict: your version and theirs
Edit the file to the desired result, remove the markers,
git addit and continue the merge (git commitorgit merge --continue).23. How do you abort a merge that has conflicts and go back to the previous state?
Correct answer:
git merge --abort--abortrestores the branch and working tree to how they were before the merge started. For a rebase the equivalent isgit rebase --abort.24. How do you delete a local branch that has been merged?
Correct answer:
git branch -d feature-drefuses to delete a branch with unmerged commits;-Dforces it. Deleting a branch removes only the pointer; commits reachable from other branches stay.25. How do you delete a branch on the remote?
Correct answer:
git push origin --delete featuregit branch -d origin/featurewould only delete your local remote-tracking copy. Hosting platforms can also delete branches automatically after a pull request is merged.26. What is
origin/main?Correct answer: Your local copy of where
mainwas on the remote at the last fetchRemote-tracking branches update only on
fetch,pullorpush. That's whygit statuscan say "up to date" while the server already has newer commits.27. How do you fix the message of the last commit before pushing?
Correct answer:
git commit --amend--amendreplaces the last commit with a new one, so you can also add forgotten files. Since it rewrites history, avoid it on commits that others already pulled.28. How do you discard uncommitted changes to one file?
Correct answer:
git restore file.jsgit restore(or the oldergit checkout -- file) replaces the working copy with the version from the index. The discarded changes can't be recovered by Git.29. How do you unstage a file but keep its changes?
Correct answer:
git restore --staged file.js--stagedresets the index entry to the last commit, while the working copy stays as is.git reset file.jsdoes the same.30. What does
git revert <commit>do?Correct answer: Creates a new commit that undoes that commit's changes
History stays intact and the fix is just another commit, so revert is safe on shared branches such as
main. Reset, by contrast, moves the branch and rewrites history.31. When is
git resetrisky andgit revertpreferable?Correct answer: When the commits were already pulled by others
Reset rewrites history; teammates who have the old commits will get confusing divergence. For published history add a reverting commit instead.
32. What does
git stashdo?Correct answer: Saves uncommitted changes aside and cleans the working tree
Use it to switch tasks quickly;
git stash popreapplies the changes. By default untracked files aren't stashed; add-ufor them.33. What is the difference between
git stash popandgit stash apply?Correct answer:
popapplies and removes the stash;applykeeps itIf applying causes conflicts,
popkeeps the stash anyway so nothing is lost.applyis handy when you want the same changes on several branches.34. What is a tag in Git usually used for?
Correct answer: Marking a commit, such as a release
v1.2.0A tag is a fixed name for a commit that doesn't move like a branch. Annotated tags (
git tag -a) also store a message, author and date.35. Are tags pushed by a plain
git push?Correct answer: No, push them with
git push origin v1.0or--tagsTags must be pushed explicitly.
git push --follow-tagspushes annotated tags that point to the commits being pushed.36. Which command shows a compact one-line history?
Correct answer:
git log --oneline--onelineprints a short hash and the subject. Add--graph --allto see how branches diverge and merge.37. How do you find who last changed each line of a file?
Correct answer:
git blame file.jsgit blameannotates each line with the commit and author that last touched it. It's for understanding context, not for blaming people.38. What does
git commit -a -m "msg"do?Correct answer: Stages changes to tracked files and commits them
-astages modifications and deletions of already tracked files. New, untracked files still need an explicitgit add.39. Why should you set
user.nameanduser.emailin Git?Correct answer: They are written into every commit as the author
Authentication to a server uses SSH keys or tokens; name and email only label commits. Platforms link commits to accounts by email.
40. What does
git initdo?Correct answer: Creates a new repository in the current folder
It creates the
.gitdirectory with an empty history. Files in the folder stay untracked until you add and commit them.41. What is in the
.gitfolder?Correct answer: The whole repository: objects, refs and configuration
Deleting
.gitdeletes all history and branches, leaving just the current files. Never edit it by hand unless you know what you're doing.42. What is a "detached HEAD"?
Correct answer:
HEADpoints to a commit directly, not to a branchIt happens after checking out a commit or tag. You can look around and commit, but new commits belong to no branch; create one with
git switch -cto keep them.43. What is a pull request (merge request)?
Correct answer: A proposal to merge a branch, with review
Pull requests are a feature of GitHub, GitLab and similar platforms, not of Git itself. They host code review, CI results and the merge button.
44. What is a fork?
Correct answer: Your server-side copy of another repository
You push to your fork and open a pull request to the original repository. It's the usual way to contribute to open source without write access.
45. What makes a good commit message?
Correct answer: A short imperative summary, then the why
For example, "Fix crash when cart is empty", then a blank line and an explanation. The diff already shows what changed; the message should say why.
46. What is the main idea of small, focused commits?
Correct answer: Each commit does one thing, easy to review and revert
Mixing a refactor, a bug fix and formatting in one commit makes review hard and turns a targeted revert into a big mess.
47. You accidentally committed an API key and pushed it. What must you do first?
Correct answer: Revoke and rotate the key, then clean it out of history
Once pushed, assume it's compromised: bots scan public repositories within minutes. A new commit doesn't remove the key from history, and rewriting history doesn't help clones others already have.
48. What does
git log main..featureshow?Correct answer: Commits in
featurethat are not inmainThe two-dot range means "reachable from the right side but not from the left". It's a quick way to see what a branch would bring in a merge.
49. Which command shows the changes introduced by a single commit?
Correct answer:
git show <commit>git showprints the commit's metadata and its diff against the parent. It also works with tags and files at a given commit, such asgit show HEAD:package.json.50. What does
HEAD~1refer to?Correct answer: The parent of the current commit
~ngoes back n generations along first parents:HEAD~2is the grandparent.HEAD^2means the second parent of a merge commit.51. What does
git reset --soft HEAD~1do?Correct answer: Undoes the last commit but keeps its changes staged
The branch moves back one commit while the index and files stay as they were. It's a quick way to redo a commit, for example to split it.
52. What is the danger of
git reset --hard?Correct answer: Uncommitted changes in the working tree are lost
--hardoverwrites the index and working tree. Committed work can usually be recovered through the reflog, but uncommitted edits are gone.53. Why is
node_modulesusually not committed?Correct answer: It's huge and can be rebuilt from the lockfile
Commit
package.jsonand the lockfile instead:npm ciinstalls exactly the same versions on any machine, and the repository stays small.54. What is
git switchfor compared withgit checkout?Correct answer: It only switches branches, a safer subset of checkout
checkoutdid two unrelated jobs: switching branches and restoring files. Git 2.23 split them intogit switchandgit restoreto reduce mistakes.55. What does
git branch -alist?Correct answer: Local branches and remote-tracking branches
Remote-tracking branches appear as
remotes/origin/.... Rungit fetch --pruneto drop ones that were deleted on the server.56. How do you set the upstream branch when pushing a new branch for the first time?
Correct answer:
git push -u origin feature-u(--set-upstream) links the local branch toorigin/feature, so later a plaingit pushorgit pullknows where to go.57. What does
git clean -ndo?Correct answer: Shows which untracked files would be deleted
-nis a dry run;-factually deletes. Untracked files aren't in Git, so once cleaned they can't be recovered from it.58. Why can
git pullcreate unexpected merge commits like "Merge branch 'main' of ..."?Correct answer: By default pull merges when local and remote have both moved
If you committed locally and someone pushed meanwhile, the default pull merges them.
git pull --rebase(orpull.rebase = true) replays your commits on top instead.59. What is a
.gitattributesfile used for?Correct answer: Per-path settings like line endings or LFS tracking
For example
* text=autonormalizes line endings, and*.png filter=lfsstores images in Git LFS. It's committed, so the whole team gets the same rules.60. Why do Windows and macOS users sometimes see every line of a file as changed?
Correct answer: Different line endings (CRLF vs LF) were committed
Configure
core.autocrlfor, better, a.gitattributesrule like* text=auto eol=lf, so the repository stores LF and each OS gets what it needs.61. Which file is a bad idea to commit to a Git repository?
Correct answer: A
.envfile with production passwordsSecrets in Git spread to every clone and stay in history. Commit a
.env.examplewith placeholder values and keep real secrets in a vault or the CI settings.62. What does
git mv old.js new.jsdo?Correct answer: Renames the file and stages the rename
Git doesn't store renames explicitly; it detects them by comparing content.
git mvis a shortcut for renaming plusgit rmandgit add.63. What does
git log -p file.jsshow?Correct answer: The file's history with each commit's diff
-padds patches to the log. Add--followto keep tracking the file across renames.64. What does "Your branch is ahead of 'origin/main' by 2 commits" mean?
Correct answer: You have 2 local commits that aren't pushed yet
Compared with your last fetch, your branch has 2 extra commits. "Behind" means the opposite; "diverged" means both have commits the other lacks.
65. Why should you pull or fetch before starting work on a shared branch?
Correct answer: To build on the latest commits and avoid needless conflicts
Git doesn't lock anything; working from an old base just means more integration work later. Short-lived branches from a fresh
mainkeep conflicts small.66. What does
git diff main featurecompare?Correct answer: The snapshots at the tips of the two branches
It shows how to turn one snapshot into the other.
git diff main...feature(three dots) shows only whatfeaturechanged since it branched off, which is what a pull request displays.67. What is a lockfile conflict in
package-lock.jsonusually best resolved with?Correct answer: Take one side, then run
npm installto regenerate itLockfiles are generated, so hand-merging is error-prone. Keep
package.jsoncorrect and let npm rebuild a consistent lockfile, then review the result.68. What does
git checkout v1.0(a tag) leave you in?Correct answer: A detached HEAD at the tagged commit
Tags don't move, so you can't commit "on" them. To fix an old release, create a branch from the tag:
git switch -c hotfix v1.0.69. What is semantic versioning (
MAJOR.MINOR.PATCH)?Correct answer: Bump MAJOR for breaking changes, MINOR for features, PATCH for fixes
SemVer tells users what to expect from an upgrade. Release tags like
v2.4.1usually follow it, and tools can bump versions from commit messages.70. Which statement about deleting a branch is true?
Correct answer: Commits stay in history if another branch or tag reaches them
A branch is only a pointer. Merged commits are part of
main's history; unmerged ones become unreachable and are eventually garbage-collected, but the reflog keeps them for a while.71. What does
git commit --amend --no-editdo?Correct answer: Adds staged changes to the last commit, same message
Handy when you forgot a file: stage it and amend without retyping the message. The commit still gets a new hash.
72. What is the purpose of a
README.mdin the repository root?Correct answer: It's shown on the repo page to explain the project
Platforms render it on the main page. A good README says what the project is, how to run it and how to contribute.
73. What does
git log --author="Ann"do?Correct answer: Lists only commits whose author matches "Ann"
--authorfilters by a pattern in the author field. Combine it with--since="2 weeks ago"or a path to narrow the history further.74. What is the difference between tracked and untracked files?
Correct answer: Tracked files were in the last commit or are staged
New files are untracked until you
git addthem; Git doesn't include them in commits. Ignored files are a separate category: untracked files thatgit statusdoesn't even show.75. Two people edited different functions in the same file. What happens when their branches merge?
Correct answer: Git usually merges them automatically without a conflict
Merging works on changed regions of lines, not whole files. Conflicts appear only when both sides changed the same or adjacent lines.