Git and GitHub are fundamental tools for developers, and interviewers often test your knowledge of them. This blog covers the most commonly asked Git and GitHub interview questions, explained in a crisp and practical way.
πΉ Git vs GitHub
- Git: A distributed version control system (VCS) used to track source code changes.
- GitHub: A cloud-based hosting platform for Git repositories with collaboration features like pull requests, issues, and CI/CD.
πΉ Repo vs Branch
- Repository (Repo): A collection of files and their entire version history.
- Branch: A lightweight movable pointer to a commit, allowing parallel development.
πΉ Push vs Pull
- Push: Uploads your local commits to a remote repository.
- Pull: Fetches updates from the remote and merges them into your local branch.
πΉ Pull vs Fetch
- Pull = Fetch + Merge.
- Fetch: Downloads changes from remote but does not merge automatically.
πΉ Clone vs Remote
- Clone: Creates a local copy of a remote repo.
-
Remote: A reference (like
origin
) to the remote repository URL.
πΉ Diff vs Status
- git diff: Shows actual changes (line-by-line) not staged or committed.
- git status: Shows the state of working directory and staging area.
πΉ Merge vs Rebase
- Merge: Combines histories of two branches with a new merge commit.
- Rebase: Reapplies commits of one branch onto another, resulting in a linear history.
πΉ Whatβs a Git repo & how to create one?
A Git repository is a directory where Git tracks files and history.
git init # Create a local repo
git clone <url> # Clone a remote repo
πΉ Fix a Broken Commit
git commit --amend # Fix last commit
git reset --soft HEAD~1 # Undo last commit, keep changes
πΉ What happens if .git
folder gets deleted?
- The repo loses its history and version control, as
.git
contains all commit objects, refs, and configurations.
πΉ Use of Staging Area (Indexing)
- Temporary area where changes are reviewed before committing.
- Allows selective commit (
git add <file>
).
πΉ Git Repo Hosting Functions
Platforms like GitHub, GitLab, Bitbucket provide:
- Remote hosting
- Collaboration (pull requests, issues)
- CI/CD pipelines
- Access control
πΉ VCS (Version Control System)
Tracks changes in code over time, supports collaboration, and helps rollback or branch development.
πΉ Features/Advantages of Git
- Distributed VCS (no single point of failure).
- Lightweight branching and merging.
- Offline work support.
πΉ Git vs SVN
- Git: Distributed, faster, branching-friendly.
- SVN: Centralized, slower for branching, single point of failure.
πΉ git config
Sets user info and preferences.
git config --global user.name "Ankush"
git config --global user.email "ankush@example.com"
πΉ git ls-tree
Shows content of a tree object (commits, blobs, trees).
πΉ Git Stash Commands
- Save work:
git stash
- List stashes:
git stash list
- Apply latest:
git stash apply
- Drop stash:
git stash drop
πΉ Revert vs Reset
- Revert: Creates a new commit that undoes changes. Safe for shared repos.
- Reset: Moves HEAD to a previous commit, can discard changes.
πΉ HEAD in Git
- HEAD is the current branch pointer (latest commit).
- Normally 1 active HEAD, but multiple branch heads can exist.
πΉ Git Username and Email
Each commit uses configured username & email (git config
).
πΉ Commit History
git log
git log --oneline --graph
πΉ Branch Operations
- New branch:
git branch new-feature
- Switch branch:
git checkout main
ORgit switch main
- List branches:
git branch
- Create + switch:
git checkout -b feature
- Delete branch:
git branch -d feature
- Merge branch:
git merge feature
πΉ Remote Tracking
Check which branch is being tracked:
git branch -vv
πΉ Force Push
git push origin main --force
πΉ Add Remote Repo
git remote add origin <url>
πΉ Rename File in Git
git mv old.txt new.txt
git commit -m "Renamed file"
πΉ .gitignore
Defines files/folders Git should ignore. Example:
node_modules/
*.log
.env
πΉ Rollback Last Commit
git reset --soft HEAD~1 # Keep changes
git reset --hard HEAD~1 # Discard changes
β Final Thoughts
These questions cover core Git and GitHub concepts often asked in interviews. Knowing both commands and theory will help you stand out in backend or full-stack interviews.
Top comments (0)