Git and GitHub course menu
Git Interview Questions for Beginners and Freshers
Beginner Git and GitHub interview questions with answers: Git vs GitHub, repositories, commits, the staging area, branches, merge, pull vs fetch, .gitignore and basic undo commands.
On this page
- Basics
- 1. What is Git?
- 2. What is the difference between Git and GitHub?
- 3. What does "distributed" mean?
- 4. What is a repository?
- 5. What is a commit?
- Everyday commands
- 6. Explain the working tree, staging area and repository.
- 7. Why have a staging area at all?
- 8. What does git status do?
- 9. What is .gitignore?
- 10. What is the difference between git add . and git commit -a?
- Branches and merging
- 11. What is a branch?
- 12. How do you create and switch to a branch?
- 13. What is a merge?
- 14. What is a merge conflict, and how do you resolve it?
- Working with GitHub
- 15. What is the difference between git clone and git fork?
- 16. What is the difference between git fetch and git pull?
- 17. What is origin?
- 18. What is a pull request?
- Undoing
- 19. How do you undo the last commit?
- 20. How do you change the last commit message?
- 21. What does HEAD mean?
These questions come up in most interviews for fresher and junior roles. Try answering aloud before you read each answer.
Basics#
1. What is Git?#
Show solution
Git is a free, distributed version control system. It records the history of a project as a series of commits, so you can see what changed, go back to earlier versions, and work with others on the same code using branches.
2. What is the difference between Git and GitHub?#
Show solution
Git is the tool on your computer that tracks versions, and it works offline. GitHub is a website that hosts Git repositories and adds pull requests, issues and automation. GitLab and Bitbucket are alternatives.
3. What does "distributed" mean?#
Show solution
Every clone contains the full history of the project, not just the latest files. You can commit and browse history offline, and any clone can restore the project if the server is lost.
4. What is a repository?#
Show solution
A project folder tracked by Git. The hidden .git folder inside it contains all the commits, branches and settings.
5. What is a commit?#
Show solution
A saved snapshot of the project, with an author, a date, a message, and a link to the previous commit. Each commit has a unique ID, a hash calculated from its content.
Everyday commands#
6. Explain the working tree, staging area and repository.#
Show solution
The working tree is the files you edit. The staging area (index) is a draft of the next commit, filled with git add. The repository is the saved history, added to with git commit.
7. Why have a staging area at all?#
Show solution
It lets you choose exactly what goes into a commit. If you changed several things, you can stage and commit them separately, which gives small, clear commits.
8. What does git status do?#
Show solution
It shows the current branch, which files are staged, modified or untracked, and how the branch compares with its remote. It changes nothing.
9. What is .gitignore?#
Show solution
A file listing patterns of files that Git should not track, such as node_modules/, build output and .env. It only affects untracked files, so a file that is already tracked needs git rm --cached.
10. What is the difference between git add . and git commit -a?#
Show solution
git add . stages all changes in the current folder and below, including new files, but does not commit. git commit -a commits changes to already tracked files, and it skips new untracked files.
Branches and merging#
11. What is a branch?#
Show solution
A separate line of development. Internally it is a small pointer to a commit, which moves forward when you commit. Branches let you work on a feature without affecting main.
12. How do you create and switch to a branch?#
Show solution
git switch -c feature creates a branch and moves to it. The older form is git checkout -b feature. To move to an existing branch, use git switch feature.
13. What is a merge?#
Show solution
Combining another branch into the current one, with git merge other. If the current branch has not moved, Git fast-forwards the pointer. Otherwise it creates a merge commit with two parents.
14. What is a merge conflict, and how do you resolve it?#
Show solution
It happens when two branches change the same lines. Git marks the file with <<<<<<<, ======= and >>>>>>>. You edit the file to the correct version, delete the markers, run git add, then git commit. git merge --abort cancels the merge.
Working with GitHub#
15. What is the difference between git clone and git fork?#
Show solution
git clone copies a repository to your computer. A fork is a GitHub feature, not a Git command: it creates your own copy of someone's repository on GitHub, so you can change it and send a pull request.
16. What is the difference between git fetch and git pull?#
Show solution
git fetch downloads new commits and updates remote-tracking branches such as origin/main, without changing your work. git pull does a fetch and then merges (or rebases) those changes into your current branch.
17. What is origin?#
Show solution
The default name for the remote that a repository was cloned from. It is only a name, and git remote -v shows its address.
18. What is a pull request?#
Show solution
A request on GitHub to merge one branch into another. It shows the changes, allows code review and runs automated checks before the code is merged.
Undoing#
19. How do you undo the last commit?#
Show solution
To keep the changes, git reset --soft HEAD~1. To discard them, git reset --hard HEAD~1. If the commit has been pushed to a shared branch, use git revert HEAD, which adds a new commit that undoes it.
20. How do you change the last commit message?#
Show solution
git commit --amend -m "New message". This replaces the commit, so only do it before pushing.
21. What does HEAD mean?#
Show solution
A pointer to your current position, normally the current branch, and so the latest commit on it. HEAD~1 is its parent.
Next: Intermediate Questions.
Finished this lesson?