Git & GitHub Interview Questions and Answers
Q: What is Git?
A: Git is a distributed version control system used to track changes in source code during software
development.
Q: What is GitHub?
A: GitHub is a web-based hosting service for Git repositories that provides tools for version control
and collaboration.
Q: What is the difference between Git and GitHub?
A: Git is a version control system; GitHub is a platform for hosting Git repositories with additional
collaboration features.
Q: What are the advantages of using Git?
A: Git offers distributed development, data redundancy, high performance, and support for
non-linear workflows.
Q: What is a repository in Git?
A: A repository is a storage space for your project where Git tracks all changes to files and
directories.
Q: What is a commit in Git?
A: A commit is a snapshot of the project's currently staged changes, recorded in the Git history.
Q: How do you initialize a Git repository?
A: Use the command `git init` to create a new Git repository.
Q: How do you check the current status of your repository?
A: Use `git status` to see staged, unstaged, and untracked files.
Q: What command is used to stage files for a commit?
A: `git add <file>` stages a file for the next commit.
Q: How do you commit changes in Git?
A: Use `git commit -m "message"` to save staged changes.
Q: How do you view the commit history?
A: Use `git log` to view the history of commits.
Q: What does `git clone` do?
A: `git clone <url>` creates a local copy of a remote repository.
Q: What does `git pull` do?
A: `git pull` fetches and merges changes from the remote repository into the current branch.
Q: What does `git push` do?
A: `git push` uploads local commits to a remote repository.
Q: What is the `.gitignore` file?
A: `.gitignore` specifies intentionally untracked files that Git should ignore.
Q: What is the difference between `git fetch` and `git pull`?
A: `git fetch` retrieves changes without merging; `git pull` fetches and merges them.
Q: What is a branch in Git?
A: A branch is a pointer to a specific commit, used to develop features separately from the main
codebase.
Q: How do you create a new branch?
A: Use `git branch <branch-name>` to create a new branch.
Q: How do you switch between branches?
A: Use `git checkout <branch-name>` or `git switch <branch-name>`.
Q: What is a merge in Git? How is it done?
A: Merging integrates changes from one branch into another using `git merge <branch-name>`.
Q: What is the difference between a fast-forward and a three-way merge?
A: Fast-forward moves the pointer forward; three-way merge creates a new merge commit.
Q: How do you resolve merge conflicts in Git?
A: Edit conflicted files, mark them as resolved with `git add`, then commit.
Q: What is a detached HEAD in Git?
A: A detached HEAD means you're on a specific commit, not a branch. Changes won't be saved
unless you create a new branch.
Q: How can you undo the last commit?
A: Use `git reset --soft HEAD~1` to undo the last commit but keep changes staged.
Q: How do you delete a branch locally and remotely?
A: `git branch -d <branch>` deletes locally; `git push origin --delete <branch>` deletes remotely.
Q: What is the staging area/index in Git?
A: It's where you prepare changes before committing them.
Q: What is the difference between `git reset`, `git revert`, and `git checkout`?
A: `reset` undoes commits, `revert` creates new commits to undo, `checkout` switches branches or
files.
Q: What is the difference between a local and remote repository?
A: A local repository exists on your machine; a remote is hosted on services like GitHub.
Q: What is the purpose of `git stash`?
A: Temporarily shelves changes without committing them.
Q: How do you apply and drop a stash?
A: `git stash apply` re-applies changes; `git stash drop` removes the stash.
Q: What is rebasing in Git? How is it different from merging?
A: Rebasing re-applies commits on top of another base, resulting in a linear history. Merging
combines histories.
Q: What is the difference between `git rebase` and `git merge`?
A: `rebase` rewrites history, `merge` preserves history by creating a merge commit.
Q: What is cherry-picking in Git?
A: It applies a specific commit from one branch onto another using `git cherry-pick <commit>`.
Q: What are Git hooks? Have you used any?
A: Git hooks are scripts that run automatically on Git events, like pre-commit or post-merge.
Q: What is a tag in Git? How is it created and deleted?
A: Tags mark specific points in history, often for releases. Use `git tag` to create and `git tag -d` to
delete.
Q: What is the difference between `origin` and `upstream` in GitHub?
A: `origin` is your fork; `upstream` is the original repo you forked from.
Q: How do pull requests work in GitHub?
A: They let you propose changes, review code, and merge updates into a main branch.
Q: What is a fork in GitHub and how is it different from a clone?
A: A fork creates a copy of a repo on GitHub under your account; a clone copies it locally.
Q: How do you squash commits in Git?
A: Use `git rebase -i HEAD~n` and mark commits as `squash`.
Q: What is the purpose of `.git/config`, `.gitignore`, and `.gitattributes`?
A: `config` stores settings, `gitignore` excludes files, `gitattributes` defines how Git handles files.