TechSchool
Home / Interview questions / Git

Git interview questions

45 questions about Git with short answers and code. Each answer links to a full lesson on this site. Free, with no signup and no ads.

Basics

1. What does the git init command do?

The git init command creates a new Git repository. It adds a hidden .git folder to your project. Git then tracks changes in that folder.

git init

Full lesson: Git init

2. How do you add files to the staging area?

The git add command puts file changes into the staging area. Git includes these changes in the next commit. You can add one file or all files.

git add [file1] [file2]

Full lesson: Git add

3. How do you save changes to the local repository?

The git commit command moves the staging area content to the local repository. The -m option adds a short message. The message describes the change.

git commit -m "fix login bug"

Full lesson: Git commit

4. How do you see the commit history?

The git log command shows the commit history. It lists all commit information from newest to oldest. You can filter the output with options.

git log

Full lesson: Git commit history

5. How do you create a new branch?

The git checkout -b command creates a branch and switches to it. A branch is an independent line of development. Work on the branch does not affect the main line.

git checkout -b <branchname>

Full lesson: Git branch

6. How do you compare unstaged changes with git diff?

The git diff command shows changes that are not yet staged. It compares the working directory with the staging area. Run it before you run git add.

git diff

Full lesson: Git diff

7. How do you show only a summary of changes?

The git diff --stat option shows a summary instead of the full diff. It lists changed files and line counts. This output is short and easy to read.

git diff --stat

Full lesson: Git diff

8. How do you copy a remote repository to your computer?

The git clone command copies a remote repository to your computer. It downloads the files and the full history. Git also sets the remote name to origin.

git clone <repository-url>

Full lesson: Git clone

9. How do you download new commits without merging them?

The git fetch command downloads new commits from the remote. It does not change your working files. You can review the changes before you merge.

git fetch origin

Full lesson: Git fetch

10. How do you get remote changes and merge them into your branch?

The git pull command downloads remote changes and merges them. It combines git fetch and git merge in one step. Your branch then has the latest commits.

git pull origin main

Full lesson: Git pull

11. How do you rename a file with Git?

The git mv command renames or moves a file. Git records the change in one step. The -f option replaces an existing target file.

git mv README README.md

Full lesson: Git mv

12. How do you remove a file from Git and the disk?

The git rm command deletes a file from the staging area and the working directory. Use the -f option if the file has staged changes. A plain delete leaves the change unstaged.

git rm example.txt

Full lesson: Git rm

13. How do you compare two commit ranges?

The git range-diff command compares two commit ranges. It is useful for code review across branches or versions. You give an old range and a new range.

git range-diff <old-range> <new-range>

Full lesson: Git range diff

14. What mistake happens when you commit without git add?

The commit skips your new changes. The staging area is still empty for those files. Run git add first, then run git commit.

git add .
git commit -m "add files"

Full lesson: Git basic operations

15. What mistake happens when you delete a file by hand and then commit?

Git shows the delete as an unstaged change. The commit does not include the delete. Use git rm to stage the delete in one step.

git rm example.txt
git commit -m "remove file"

Full lesson: Git rm

Intermediate

16. How do you send your local commits to a remote repository?

Use the git push command. It uploads your local branch to the remote and merges it there.

git push origin master

Full lesson: Git push

17. How do you create a tag for a release?

Use git tag with the -a option to make an annotated tag. Annotated tags record the tagger and date.

git tag -a v1.0

Full lesson: Git tag

18. How do you see the details of a specific commit?

Use git show with the commit hash. It displays the commit message and the changes.

git show a1b2c3d

Full lesson: Git show

19. How do you move a file to a new name and stage the change?

Use git mv with the old and new file names. Git moves the file and stages the rename.

git mv README README.md

Full lesson: Git mv

20. How do you remove a file that is already staged?

Use git rm with the -f option to force deletion. The -f option is necessary if the file was modified and staged.

git rm -f example.txt

Full lesson: Git rm

21. What is Git Flow and what branches does it use?

Git Flow is a branching model for teams. It uses master, develop, feature, release, and hotfix branches.

Full lesson: Git flow

22. How do you install Git Flow on Debian or Ubuntu?

Use the apt-get package manager. The command installs the git-flow package.

sudo apt-get install git-flow

Full lesson: Git flow

23. How do you fetch from a remote and then merge the changes?

First run git fetch to download the remote changes. Then run git merge to merge them into your current branch.

git fetch origin
git merge origin/master

Full lesson: Git fetch

24. How do you see the changes between two commits with git diff?

Use git diff with the two commit hashes. It shows the differences between those commits.

git diff a1b2c3d e4f5g6h

Full lesson: Git diff

25. How do you use an external tool to view differences?

Use git difftool instead of git diff. It opens the configured difftool for each file.

git difftool

Full lesson: Git difftool

26. How do you add a submodule to your repository?

Use git submodule add with the repository URL. It adds the submodule and records it in .gitmodules.

git submodule add https://github.com/example/lib.git

Full lesson: Git submodule

27. What mistake happens when you force push without checking the remote?

Force push can overwrite remote commits that you do not have. This can cause lost work for other team members.

git push --force origin master

Full lesson: Git push

28. What mistake happens when you use git rm on a modified file without -f?

Git refuses to delete the file and shows an error. You must use -f to force deletion.

git rm example.txt

Full lesson: Git rm

29. What mistake happens when you rename a file with git mv to an existing name?

Git stops and reports that the target exists. Use the -f option to overwrite the existing file.

git mv -f old.txt new.txt

Full lesson: Git mv

30. What mistake happens when you pull without committing local changes?

Git can refuse to merge and warn about local changes. Commit or stash your changes before you pull.

git pull origin master

Full lesson: Git pull

Common mistakes

31. What does the git clone command do?

The git clone command copies a remote repository to your computer. It copies all code and history. You get a local copy that matches the remote.

git clone https://github.com/tianqixin/example-git-test

Full lesson: Git clone

32. How do you see the full history of a file?

Use git log with the file path. Git shows only commits that changed that file. Add --follow to track renames.

git log --follow -- README.md

Full lesson: Git commit history

33. How do you view the changes in a single file before you stage it?

Run git diff with the file path. Git shows the unstaged changes for that file only. This helps you check your edits.

git diff app.js

Full lesson: Git diff

34. How do you list all local branches?

Run git branch with no arguments. Git prints the local branches. The current branch has a star.

git branch

Full lesson: Git branch

35. How do you delete a local branch that is already merged?

Use git branch -d with the branch name. Git deletes the branch only if it is merged. This protects unmerged work.

git branch -d old-feature

Full lesson: Git branch

36. How do you show the details of a tag?

Run git show with the tag name. Git prints the tag message and the commit it points to. This helps you check a release.

git show v1.0.0

Full lesson: Git tag

37. How do you set your user name and email for Git?

Use git config with the --global flag. Set user.name and user.email. Git uses these values in every commit.

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Full lesson: Git install setup

38. How do you list the submodules in your repository?

Run git submodule status. Git prints each submodule and its current commit. A minus sign means the submodule is not initialized.

git submodule status

Full lesson: Git submodule

39. How do you update a submodule to the commit recorded in the parent repository?

Run git submodule update. Git checks out the commit that the parent repository records. Add --init on the first run.

git submodule update --init

Full lesson: Git submodule

40. What does the git range-diff command compare?

The git range-diff command compares two versions of a commit series. It shows how the patches changed between the two ranges. This helps after you rebase a branch.

git range-diff main..old main..new

Full lesson: Git range diff

41. What mistake happens when you clone into a folder that already has files?

Git stops and reports that the target directory is not empty. The clone does not start. Choose a new folder name or empty the folder first.

git clone https://github.com/tianqixin/example-git-test my-copy

Full lesson: Git clone

42. What mistake happens when you run git fetch and then forget to merge?

Your branch does not change. The new commits stay in the remote-tracking branch. Run git merge to bring them into your branch.

git fetch origin
git merge origin/main

Full lesson: Git fetch

43. What mistake happens when you delete a branch with -d before you merge it?

Git refuses to delete the branch. It warns that the branch is not fully merged. Merge the branch first or use -D to force the delete.

git branch -d feature-x

Full lesson: Git branch

44. What mistake happens when you run git submodule update without --init on a fresh clone?

The submodule folders stay empty. Git does not fetch the submodule content. Run the command again with --init.

git submodule update --init

Full lesson: Git submodule

45. What mistake happens when you compare the wrong ranges with git range-diff?

The output shows unrelated patches and confuses you. The two ranges must describe the same logical series. Check the branch names and the base commit first.

git range-diff main..feature-v1 main..feature-v2

Full lesson: Git range diff

More on TechSchool
See the Git cheat sheet Python JavaScript TypeScript HTML CSS React Vue 3 Node.js Java PHP C C++ C# Go SQL MongoDB Redis Linux Docker Data Structures