DEV Community

Cover image for πŸš€ Essential Git and GitHub Interview Questions
Ankush Singh Gandhi
Ankush Singh Gandhi

Posted on

πŸš€ Essential Git and GitHub Interview Questions

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  
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Fix a Broken Commit

git commit --amend        # Fix last commit  
git reset --soft HEAD~1   # Undo last commit, keep changes  
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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"  
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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  
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Branch Operations

  • New branch: git branch new-feature
  • Switch branch: git checkout main OR git 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  
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Force Push

git push origin main --force  
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Add Remote Repo

git remote add origin <url>  
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Rename File in Git

git mv old.txt new.txt  
git commit -m "Renamed file"  
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή .gitignore

Defines files/folders Git should ignore. Example:

node_modules/  
*.log  
.env  
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Rollback Last Commit

git reset --soft HEAD~1     # Keep changes  
git reset --hard HEAD~1     # Discard changes  
Enter fullscreen mode Exit fullscreen mode

βœ… 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)