Beginner Level
Q: What is Git and how is it different from GitHub?
A: Git is a distributed version control system used to track changes in source code. GitHub is a
cloud-based hosting service that lets you manage Git repositories.
Q: What is a commit in Git?
A: A commit in Git is a snapshot of your changes. It records the current state of the repository.
Q: What does git init do?
A: `git init` initializes a new Git repository in your current directory.
Q: How do you check the current status of your Git repository?
A: Use `git status` to view the current status, including staged, unstaged, and untracked files.
Q: What is the difference between git add and git commit?
A: `git add` stages changes for the next commit, while `git commit` saves the staged changes to the
repository.
Q: What is a branch in Git? Why do we use it?
A: A branch in Git is an independent line of development. It helps in working on features or fixes in
isolation.
Q: How do you create a new branch?
A: Use `git branch branch_name` to create a new branch.
Q: How do you switch to another branch?
A: Use `git checkout branch_name` or `git switch branch_name`.
Q: What does git clone do?
A: `git clone` copies an existing Git repository to your local machine.
Q: What is the use of .gitignore file?
A: It tells Git which files or folders to ignore in a project.
Intermediate Level
Q: What is the difference between git fetch and git pull?
A: `git fetch` downloads changes but doesn't apply them. `git pull` fetches and merges the changes
into your branch.
Q: How does git merge work? What happens if there is a conflict?
A: `git merge` combines changes from one branch into another. If both branches changed the same
part of a file, a conflict occurs.
Q: How do you resolve merge conflicts?
A: Manually edit the files to resolve conflicts, then use `git add` and `git commit`.
Q: What does git rebase do? When would you use it instead of git merge?
A: `git rebase` moves or combines commits from one branch to another. Use it to create a linear
history.
Q: What is the purpose of git stash?
A: `git stash` temporarily saves changes that are not ready to be committed.
Q: How do you view the commit history of a repository?
A: Use `git log` to view commit history.
Q: What is a detached HEAD in Git?
A: It means you are not on any branch and are viewing an old commit.
Q: How do you undo the last commit?
A: Use `git reset --soft HEAD~1` to undo the last commit and keep changes staged.
Q: What is the difference between git reset, git revert, and git checkout?
A: `git reset` undoes commits, `git revert` creates a new commit to reverse changes, `git checkout`
switches branches or restores files.
Q: What is a remote repository? How do you push code to it?
A: A remote repo is hosted on a server like GitHub. Use `git push origin branch_name` to push
code.
Advanced Level
Q: What happens under the hood when you run git commit?
A: Git records changes in the staging area by writing a new commit object to the repository.
Q: Explain the difference between origin/main and main.
A: `main` is your local branch; `origin/main` is the remote-tracking branch.
Q: How do you squash multiple commits into one?
A: Use `git rebase -i HEAD~n`, then change `pick` to `squash` for all but the first commit.
Q: How do you force push a branch? When should you avoid it?
A: Use `git push --force`. Avoid it on shared branches to prevent overwriting others' work.
Q: What is a rebase conflict and how is it different from a merge conflict?
A: Both occur when Git cannot automatically reconcile changes, but rebase replays commits while
merge combines branches.
Q: How would you recover a deleted branch in Git?
A: Find its commit using `git reflog`, then recreate it with `git checkout -b branch_name commit_id`.
Q: What does the HEAD pointer in Git refer to?
A: HEAD points to the current branch reference or commit.
Q: How do tags work in Git? How do you create and delete them?
A: Tags mark specific points. Create with `git tag tagname`, delete with `git tag -d tagname`.
Q: What are Git hooks?
A: Git hooks are scripts that run automatically on certain events like commit or push.
Q: How do you secure your code using GitHub settings (branch protection, access control, etc.)?
A: Use branch protection rules, enforce reviews, require status checks, and limit push access.