The most asked Git interview questions with structured answers — a quick answer, a clear explanation and working code for each. Covers basics for freshers through advanced, scenario and coding questions for experienced developers.
Updated November 2026 Reviewed by Abhishek Kumar 119 min read
99 Git interview questions, grouped by level: freshers, intermediate, experienced and coding.
Every answer starts with a one-line “Quick answer” you can say out loud, followed by the explanation and runnable code.
Use the filter bar to practise only the level you are interviewing for, then test yourself with the free Git quiz.
What is Git, and why do interviewers ask about it?
This page provides a comprehensive collection of Git Interview Questions and Answers designed for developers, DevOps engineers, system administrators, and software engineers preparing for technical interviews.
Git is a distributed version control system that tracks changes in source code during software development. It allows multiple developers to collaborate efficiently, maintain code history, and manage project versions with ease.
This interview guide covers beginner, intermediate, and advanced Git concepts including basic commands, branching, merging, rebasing, workflows, conflict resolution, hooks, submodules, performance optimization, and real-world collaboration scenarios.
Why companies hire for Git
Industry standard – used by millions of developers worldwide
Distributed architecture – every copy is a full repository, enabling offline work and redundancy
Powerful branching and merging – lightweight branches and flexible merge strategies
Rich ecosystem – integrates with GitHub, GitLab, Bitbucket, and countless CI/CD tools
Essential for DevOps – plays a critical role in continuous integration and deployment pipelines
Strong community and support – extensive documentation, tools, and best practices available
99 shown
Basic Git Interview Questions for Freshers
10 questions
Start here if you are a fresher or switching to Git. These fundamentals are asked in almost every first-round and campus interview.
Quick answerGit is a distributed version control system that tracks changes in source code during software development. It allows multiple developers to work on the same project simultaneously.
Distributed: Each developer has a full copy of the repository
Branching: Lightweight and easy branching
Merging: Powerful merge capabilities
Staging Area: Index for preparing commits
History: Complete project history
Shell
# Git Basics# Initialize a new repository
git init
# Check repository status
git status
# Add files to staging area
git add file.txt
git add . # Add all files# Commit changes
git commit -m "Initial commit"# View commit history
git log
git log --oneline --graph --decorate
Quick answerBranches in Git are pointers to commits that allow parallel development. Each branch is an independent line of development.
Create Branch: git branch branch-name
Switch Branch: git checkout branch-name
Create and Switch: git checkout -b branch-name
List Branches: git branch
Delete Branch: git branch -d branch-name
Shell
# Git Branching# Create a new branch
git branch feature-branch
# Switch to a branch
git checkout feature-branch
git switch feature-branch
# Create and switch in one command
git checkout -b new-feature
git switch -c new-feature
# List all branches
git branch
git branch -a # List all including remote# Delete a branch
git branch -d feature-branch # Safely delete
git branch -D feature-branch # Force delete
Quick answerGit rebasing rewrites commit history by applying commits from one branch onto another. It creates a linear history.
Interactive Rebase: git rebase -i HEAD~n
Rebase vs Merge: Linear vs branching history
Rebasing Rules: Don't rebase shared branches
Conflict Resolution: Resolve during rebase
Benefits: Cleaner history, easier to follow
Shell
# Git Rebasing# Rebase current branch onto another branch
git rebase main
# Interactive rebase
git rebase -i HEAD~5
# Rebase with merge conflicts
git rebase --continue# After resolving conflicts
git rebase --skip # Skip conflicting commit
git rebase --abort # Abort rebase# Rebase against specific commit
git rebase --onto main feature-branch
# Rebase preserve merges
git rebase -p main
Quick answerGit stash temporarily saves uncommitted changes, allowing you to switch branches or pull updates without committing incomplete work.
Stash Changes: git stash
List Stashes: git stash list
Apply Stash: git stash apply
Pop Stash: git stash pop
Drop Stash: git stash drop
Shell
# Git Stashing# Save uncommitted changes
git stash
git stash push -m "WIP: feature implementation"# List all stashes
git stash list
# Apply stash (keep stash)
git stash apply
git stash apply stash@{1}
# Apply stash and drop it
git stash pop
git stash pop stash@{1}
# Show stash contents
git stash show -p stash@{0}
# Drop a stash
git stash drop stash@{0}
# Clear all stashes
git stash clear
Quick answerGit reset moves the branch pointer and modifies the commit history. Git revert creates a new commit that undoes changes, preserving history.
Quick answerGit tags mark specific points in history, typically used for releases. Tags can be lightweight or annotated.
Lightweight Tag: git tag v1.0.0
Annotated Tag: git tag -a v1.0.0 -m "Release"
List Tags: git tag
Push Tags: git push origin v1.0.0
Delete Tag: git tag -d v1.0.0
Shell
# Git Tag# Create a tag
git tag v1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"# Create annotated tag
git tag -a v1.0.0 -m "First release"# List tags
git tag
git tag -l "v1.*"# Push tags to remote
git push origin v1.0.0
git push --tags
# Delete a tag
git tag -d v1.0.0
git push origin --delete v1.0.0
# Checkout a tag
git checkout v1.0.0
Quick answerGit submodules allow including other repositories as subdirectories. They maintain references to specific commits in external repositories.
Quick answerGit bisect uses binary search to find the commit that introduced a bug. It systematically narrows down the problematic commit.
Start Bisect: git bisect start
Mark Bad: git bisect bad
Mark Good: git bisect good commit
Test Commit: Check if bug exists
Reset: git bisect reset
Shell
# Git Bisect# Start bisect
git bisect start
# Mark current commit as bad
git bisect bad
# Mark known good commit
git bisect good <commit-hash>
# Test and mark commits
git bisect good # If commit is good
git bisect bad # If commit is bad# End bisect
git bisect reset
# Automate bisect
git bisect run ./test-script.sh
# View bisect log
git bisect log
Quick answerGit patches are files containing changes that can be shared and applied to other repositories. They enable code review and collaboration.
Create Patch: git format-patch -1 commit
Apply Patch: git apply patch.diff
Apply with Commit: git am patch.diff
Generate Patch: git diff > patch.diff
Patch Series: Multiple patches in sequence
Shell
// Git Patches - Working with diffs and patches
package main
import (
"fmt"
)
func main() {
// Git patch commands demonstration
commands := []string{
"git format-patch -1 <commit> # Create a patch from a commit",
"git apply patch.diff # Apply a patch without committing",
"git am patch.diff # Apply a patch with commit",
"git diff > patch.diff # Generate a patch from working directory",
}
// Patch workflow simulation
fmt.Println("=== Git Patch Workflow ===")
for i, cmd := range commands {
fmt.Printf("%d. %s\n", i+1, cmd)
}
// Create a simple patch-like diff
fmt.Println("\n=== Example Patch Format ===")
patch := `diff --git a/file.txt b/file.txt
index 1234567..abcdefg 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
Hello World
-This is the old content
+This is the new content
+Added another line
---
2.0.1`
fmt.Println(patch)
}
Quick answer.gitignore specifies intentionally untracked files that Git should ignore. It prevents committing temporary files, build artifacts, and secrets.
Create File: .gitignore in repository root
Patterns: *.log, node_modules/
Negate: !important.log
Global: core.excludesfile configuration
Check Ignored: git status --ignored
Shell
# Git Ignore# Create .gitignore file# Ignore specific files
*.log
*.tmp
# Ignore directories
build/
dist/
node_modules/
# Ignore files with pattern
*.~
*.swp
# Ignore specific file
config.env
# ! - Negate pattern
!*.js
# Global .gitignore
git config --global core.excludesfile ~/.gitignore_global
# Check ignored files
git status --ignored
Quick answerGit workflows define how teams collaborate using Git. Common workflows include feature branching, Git Flow, and GitHub Flow.
Feature Branch: Each feature gets a branch
Git Flow: Main, develop, feature, release, hotfix
GitHub Flow: Main branch with feature branches
GitLab Flow: Environment branches
Trunk-Based: Short-lived branches
Shell
# Git Hooks Examples# .git/hooks/pre-commit#!/bin/sh# Run linter
npm run lint
# .git/hooks/pre-push#!/bin/sh# Run tests before push
npm test# .git/hooks/commit-msg#!/bin/sh# Validate commit message
COMMIT_MSG=$(cat$1)
if [ ${#COMMIT_MSG} -lt 10 ]; thenecho"Commit message too short"exit 1
fi# .git/hooks/pre-rebase#!/bin/sh# Prevent rebasing on main branchif [ "$(git rev-parse --abbrev-ref HEAD)" = "main" ]; thenecho"Cannot rebase on main branch"exit 1
fi
Quick answerGit LFS replaces large files with text pointers, storing the actual files on a remote server. It optimizes repository size and performance.
Install: git lfs install
Track Patterns: git lfs track "*.psd"
View Tracked: git lfs track
Push LFS: git lfs push origin main
Pull LFS: git lfs pull
Shell
# Git Advanced Commands# Find commit by content
git log -S"functionName"# Find commit by path
git log -- path/to/file
# Show changes in a commit
git show <commit-hash>
# Show commit count
git rev-list --count main
# Show contribution stats
git shortlog -sn
# Show branch divergence
git rev-list --count main..feature
# List all files in a commit
git ls-tree -r main
# Show object details
git cat-file -p <object-hash>
Quick answerGit bisect uses binary search to find the commit that introduced a bug.
Start Bisect: git bisect start
Mark Bad: git bisect bad HEAD
Mark Good: git bisect good commit
Test Commit: Check if bug exists
Mark Result: git bisect good or git bisect bad
Shell
# Git Attributes# .gitattributes file# Set diff for specific files
*.txt diff
# Set merge strategy
*.png binary
*.jpg binary
# Set eol
*.js text eol=lf
*.sh text eol=lf
*.bat text eol=crlf
# Set language
*.js linguist-language=JavaScript
*.rb linguist-language=Ruby
# Export ignoretest/ export-ignore
*.spec.js export-ignore
# Merge driver
*.lock merge=binary
Quick answerGit rebase workflow maintains a linear history by applying changes from one branch onto another.
Rebase Feature: git rebase main
Interactive Rebase: git rebase -i HEAD~n
Conflict Resolution: Resolve conflicts and continue
Skip Commit: git rebase --skip
Abort Rebase: git rebase --abort
Shell
# Git Bisect Example# Start bisect
git bisect start
# Mark current as bad
git bisect bad HEAD
# Mark known good commit
git bisect good <commit-hash>
# Test commit# Build and test
./build.sh
./test.sh
# Mark based on test resultif test_passed; then
git bisect good
else
git bisect bad
fi# Find the commit that introduced the bug# End bisect
git bisect reset
# Automate with script
git bisect start HEAD <good-commit>
git bisect run ./test-script.sh
git bisect reset
# Git Alias Advanced# Alias for custom log
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset - %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"# Alias for status short
git config --global alias.s "status -s"# Alias for amend
git config --global alias.amend "commit --amend --no-edit"# Alias for undo
git config --global alias.undo "reset --soft HEAD^"# Alias for cleanup
git config --global alias.cleanup "!git branch --merged | grep -v '\*' | grep -v main | xargs git branch -d"# Alias for last commit
git config --global alias.last "log -1 HEAD"# Alias for changelog
git config --global alias.changelog "log --oneline --decorate --graph --all"# List all aliases
git config --get-regexp alias
Quick answerAdvanced Git worktree usage includes locking, repairing, and managing multiple worktrees efficiently.
Lock Worktree: git worktree lock path
Unlock Worktree: git worktree unlock path
Repair Worktree: git worktree repair path
List with Details: git worktree list --porcelain
Force Remove: git worktree remove -f path
Shell
# Git Rebase Workflow# Rebase feature branch on main
git checkout feature-branch
git rebase main
# Interactive rebase to squash commits
git rebase -i HEAD~5
# Replace "pick" with "squash" for commits to combine# Rebase with merge conflicts# Resolve conflicts in files
git add <resolved-files>
git rebase --continue# Skip conflict commit
git rebase --skip
# Abort rebase
git rebase --abort
# Rebase with execute commands
git rebase -i HEAD~3
# Add exec command after a commit# exec npm test# Rebase with force push
git push --force-with-lease origin feature-branch
Quick answerAdvanced Git bisect includes using scripts, visualizing, and logging the bisect process.
Script Automation: git bisect run ./test.sh
Visualize: git bisect visualize
Log: git bisect log
Skip: git bisect skip
Status: git bisect status
Shell
# Git Tag Advanced# Create lightweight tag
git tag v1.0.0
# Create annotated tag
git tag -a v1.0.0 -m "Release version 1.0.0"# Create signed tag
git tag -s v1.0.0 -m "Signed release"# List tags matching pattern
git tag -l "v1.*"# Show tag details
git show v1.0.0
# Verify signed tag
git tag -v v1.0.0
# Delete local tag
git tag -d v1.0.0
# Delete remote tag
git push origin --delete v1.0.0
# Push specific tag
git push origin v1.0.0
# Push all tags
git push --tags
# Create branch from tag
git checkout -b new-branch v1.0.0
Quick answerGit shallow clone limits history depth, saving bandwidth and disk space by not downloading the full commit history.
Depth 1: git clone --depth 1 url
Depth N: git clone --depth 50 url
Fetch More: git fetch --depth=100
Unshallow: git fetch --unshallow
Single Branch: git clone --single-branch --branch main url
Shell
# Git Blame Advanced# Show blame with commit info
git blame -c file.txt
# Show blame with line numbers
git blame -L 10,20 file.txt
# Show blame with email
git blame -e file.txt
# Show blame with time
git blame -t file.txt
# Show blame ignoring whitespace
git blame -w file.txt
# Show blame with previous commits
git blame -f file.txt
# Show blame in porcelain format
git blame --porcelain file.txt
# Show blame for specific revision
git blame <commit-hash> file.txt
# Show blame for multiple files
git blame file1.txt file2.txt
# Show blame with score
git blame -s file.txt
# Git Bisect Advanced# Start bisect with good and bad
git bisect start HEAD <good-commit>
# Bisect with skip
git bisect skip
# Bisect with visual
git bisect visualize
# Bisect with log
git bisect log > bisect.log
# Bisect with script
git bisect run ./test.sh
# Bisect with multiple scripts
git bisect run ./build.sh && ./test.sh
# Bisect with branch
git bisect start
git bisect bad feature-branch
git bisect good main
# End bisect with branch
git bisect reset main
# Show bisect status
git bisect status
# Bisect with custom command
git bisect run git grep -q "error"
Quick answerGit describe finds the most recent tag reachable from a commit and describes the commit relative to that tag.
Describe Commit: git describe
With Tags: git describe --tags
With Abbrev: git describe --abbrev=7
With Dirty: git describe --dirty
Exact Match: git describe --exact-match
Shell
# Git Reflog Advanced# View reflog for all branches
git reflog show --all
# View reflog with date
git reflog --date=iso
# View reflog with relative date
git reflog --date=relative
# Show reflog entries
git reflog --oneline
# View reflog for specific branch
git reflog show feature-branch
# Delete reflog entries older than 90 days
git reflog expire --expire=90.days.ago
# Delete reflog entries for stale branches
git reflog expire --expire-unreachable=now --all
# Show reflog with graph
git reflog --graph
# View reflog in porcelain format
git reflog --format=porcelain
Quick answerGit hash-object computes the SHA-1 hash of a file and optionally stores it in the object database.
Compute Hash: git hash-object file.txt
Store Object: git hash-object -w file.txt
Store Type: git hash-object -t blob -w file.txt
From Stdin: echo "content" | git hash-object -w --stdin
No Filters: git hash-object --no-filters file.txt
Shell
# Git Describe# Describe current commit
git describe
# Describe with tags
git describe --tags
# Describe with all refs
git describe --all
# Describe with abbrev
git describe --abbrev=7
# Describe with long format
git describe --long
# Describe with match
git describe --match "v*"# Describe with dirty marker
git describe --dirty
# Describe for specific commit
git describe <commit-hash>
# Describe with exact match
git describe --exact-match
# Describe with first parent
git describe --first-parent
Quick answerGit cat-file displays the contents or type of a Git object, useful for examining the object database.
Show Content: git cat-file -p hash
Show Type: git cat-file -t hash
Show Size: git cat-file -s hash
Batch: git cat-file --batch
Textconv: git cat-file --textconv file.txt
Shell
# Git Shortlog# Show shortlog
git shortlog
# Show shortlog with summary
git shortlog -s
# Show shortlog with email
git shortlog -e
# Show shortlog with count
git shortlog -sn
# Show shortlog with commit count
git shortlog -sn --all
# Show shortlog with file changes
git shortlog -sn -- -- "*.js"# Show shortlog by date
git shortlog -sn --since="2024-01-01"# Show shortlog by author
git shortlog -sn --author="John"# Show shortlog with number of commits
git shortlog -sn | head -10
# Show shortlog with email and count
git shortlog -sen
Quick answerGit ls-files shows information about files in the index and working directory.
List Files: git ls-files
With Stage: git ls-files --stage
With Others: git ls-files --others
With Ignored: git ls-files --ignored
With Modified: git ls-files --modified
Shell
# Git Rev-List# List all commits
git rev-list --all
# List commits in branch
git rev-list main
# Count commits
git rev-list --count main
# List commits with message
git rev-list --oneline main
# List commits with date
git rev-list --date=iso main
# List commits with author
git rev-list --author="John" main
# List commits with grep
git rev-list --grep="fix" main
# List commits with range
git rev-list main..feature
# List commits with first parent
git rev-list --first-parent main
# List commits with objects
git rev-list --objects main
Quick answerGit ls-tree lists the contents of a tree object, showing files and subdirectories at a given commit.
List Tree: git ls-tree HEAD
Recursive: git ls-tree -r HEAD
With Tree: git ls-tree -t HEAD
With Long: git ls-tree -l HEAD
Name Only: git ls-tree --name-only HEAD
Shell
# Git Name-Rev# Show commit reference
git name-rev <commit-hash>
# Show with tags
git name-rev --tags <commit-hash>
# Show with refs
git name-rev --refs=refs/heads/* <commit-hash>
# Show with always
git name-rev --always <commit-hash>
# Show with undefined
git name-rev --undefined <commit-hash>
# Show with name-only
git name-rev --name-only <commit-hash>
# Show with stdinecho <commit-hash> | git name-rev --stdin
# Show with multiple commits
git name-rev <hash1> <hash2> <hash3>
# Show with skip
git name-rev --skip=2 <commit-hash>
Quick answerGit ls-remote lists references in a remote repository without downloading the full repository.
List Remote: git ls-remote origin
Heads Only: git ls-remote --heads origin
Tags Only: git ls-remote --tags origin
Specific Ref: git ls-remote origin main
With Symref: git ls-remote --symref origin
Shell
# Git Show-Ref# Show all refs
git show-ref
# Show heads
git show-ref --heads
# Show tags
git show-ref --tags
# Show specific ref
git show-ref refs/heads/main
# Show with hash
git show-ref --hash=7
# Show with verify
git show-ref --verify refs/heads/main
# Show with heads and tags
git show-ref --heads --tags
# Show with pattern
git show-ref "*feature*"# Show with quiet
git show-ref --quiet refs/heads/main
# Show with exclusive
git show-ref --exclude-existing
Quick answerAdvanced Git diff includes comparing commits, branches, and using various diff options for detailed analysis.
Compare Commits: git diff commit1 commit2
Compare Branches: git diff main..feature
With Stat: git diff --stat
With Patch: git diff --patch
Ignore Whitespace: git diff -w
Shell
# Git Symbolic-Ref# Read symbolic ref
git symbolic-ref HEAD
# Set symbolic ref
git symbolic-ref HEAD refs/heads/main
# Read short ref
git symbolic-ref --short HEAD
# Read with quiet
git symbolic-ref -q HEAD
# Set with force
git symbolic-ref -f HEAD refs/heads/main
# Read for specific ref
git symbolic-ref refs/remotes/origin/HEAD
# Delete symbolic ref
git symbolic-ref -d HEAD
# Show with message
git symbolic-ref -m "Switch to main" HEAD refs/heads/main
# Read with show-ref
git symbolic-ref --show-ref HEAD
# Git Cat-File# Show object content
git cat-file -p <hash>
# Show object type
git cat-file -t <hash>
# Show object size
git cat-file -s <hash>
# Show pretty content
git cat-file -p HEAD
# Show tree content
git cat-file -p HEAD^{tree}
# Show commit content
git cat-file -p HEAD
# Show blob content
git cat-file -p HEAD:file.txt
# Show with batch
git cat-file --batch
# Show with batch-check
git cat-file --batch-check
# Show with textconv
git cat-file --textconv file.txt
Quick answerAdvanced Git revert includes reverting ranges, using strategies, and handling complex reverts.
Revert Commit: git revert commit-hash
With Edit: git revert -e commit-hash
Without Commit: git revert -n commit-hash
With Mainline: git revert -m 1 merge-commit
Revert Range: git revert start..end
Shell
# Git Ls-Files# List tracked files
git ls-files
# List with stage
git ls-files --stage
# List with others
git ls-files --others
# List with ignored
git ls-files --ignored
# List with exclude
git ls-files --exclude-standard
# List with modified
git ls-files --modified
# List with deleted
git ls-files --deleted
# List with cached
git ls-files --cached
# List with error
git ls-files --error-unmatch file.txt
# List with full name
git ls-files --full-name
Quick answerAdvanced Git reset includes various reset modes and handling complex reset scenarios.
Soft Reset: git reset --soft HEAD~1
Mixed Reset: git reset --mixed HEAD~1
Hard Reset: git reset --hard HEAD~1
With Merge: git reset --merge HEAD~1
With Keep: git reset --keep HEAD~1
Shell
# Git Ls-Tree# List tree contents
git ls-tree HEAD
# List recursively
git ls-tree -r HEAD
# List with tree only
git ls-tree -t HEAD
# List with long format
git ls-tree -l HEAD
# List with name only
git ls-tree --name-only HEAD
# List with full tree
git ls-tree --full-tree HEAD
# List with path
git ls-tree HEAD:src/
# List with object ID
git ls-tree -r HEAD --object-only
# List with commit
git ls-tree HEAD -- src/ docs/
# List with abbrev
git ls-tree --abbrev=7 HEAD
Quick answerAdvanced Git checkout includes orphan branches, tracking, and various checkout options.
Orphan Branch: git checkout --orphan new-branch
Track Remote: git checkout --track origin/feature
With Merge: git checkout --merge main
With Force: git checkout -f main
With Quiet: git checkout -q main
Shell
# Git Ls-Remote# List remote branches
git ls-remote origin
# List remote heads
git ls-remote --heads origin
# List remote tags
git ls-remote --tags origin
# List with refs
git ls-remote origin refs/heads/*
# List with specific ref
git ls-remote origin main
# List with upload-pack
git ls-remote --upload-pack
# List with exit code
git ls-remote --exit-code origin
# List with quiet
git ls-remote --quiet origin
# List with symref
git ls-remote --symref origin
# List with sort
git ls-remote --sort=refname origin
Quick answerGit switch is a modern command for switching branches, introduced as a safer alternative to checkout.
Switch Branch: git switch main
Create Branch: git switch -c new-branch
With Force: git switch -f main
With Merge: git switch --merge main
With Track: git switch --track origin/feature
Shell
# Git Diff Advanced# Show diff between commits
git diff <commit1> <commit2>
# Show diff between branches
git diff main..feature
# Show diff with 3-dots
git diff main...feature
# Show diff with stat
git diff --stat# Show diff with patch
git diff --patch
# Show diff with color
git diff --color
# Show diff with ignore spaces
git diff -w
# Show diff with name-only
git diff --name-only
# Show diff with find-copies
git diff -C
# Show diff with find-renames
git diff --find-renames
Quick answerAdvanced Git stash includes stashing with untracked files, keeping index, and applying with options.
Stash with Untracked: git stash push -u
Stash with All: git stash push -a
Stash with Keep-Index: git stash push --keep-index
Stash with Patch: git stash push -p
Apply with Index: git stash apply --index
Shell
# Git Am (Apply Mailbox)# Apply patch series
git am patches/*.patch
# Apply with 3-way merge
git am -3 patch.diff
# Apply with directory
git am --directory=src/ patch.diff
# Apply with signoff
git am --signoff patch.diff
# Apply with keep
git am --keep patch.diff
# Apply with ignore
git am --ignore-whitespace patch.diff
# Apply with fallback
git am --fallback patch.diff
# Continue after conflict
git am --continue# Skip patch
git am --skip
# Abort am
git am --abort
Quick answerGit rev-parse parses revision specifiers and returns object names, useful for scripts and plumbing commands.
Parse HEAD: git rev-parse HEAD
Short Hash: git rev-parse --short HEAD
Branch Name: git rev-parse --abbrev-ref HEAD
Git Dir: git rev-parse --git-dir
Verify: git rev-parse --verify HEAD
Shell
# Git Switch# Switch branch
git switch main
# Switch with create
git switch -c new-branch
# Switch with force
git switch -f main
# Switch with discard
git switch --discard-changes main
# Switch with merge
git switch --merge main
# Switch with progress
git switch --progress main
# Switch with quiet
git switch -q main
# Switch with detach
git switch --detach <commit-hash>
# Switch with orphan
git switch --orphan new-branch
# Switch with track
git switch --track origin/feature
Quick answerGit instaweb starts a web server with Gitweb interface for browsing the repository.
Start: git instaweb
With Port: git instaweb --port=8080
With Browser: git instaweb --browser=firefox
Stop: git instaweb --stop
Restart: git instaweb --restart
Shell
# Git Log Advanced# Log with graph
git log --graph --oneline --decorate
# Log with pretty format
git log --pretty=format:"%h - %s (%cr) <%an>"# Log with date format
git log --date=iso
# Log with author
git log --author="John"# Log with grep
git log --grep="fix"# Log with file
git log -- file.txt
# Log with full diff
git log -p
# Log with stats
git log --stat# Log with first parent
git log --first-parent
# Log with range
git log HEAD~5..HEAD
Quick answerAdvanced Git pre-commit hook runs comprehensive checks including linting, formatting, and debugging before commits.
Lint Check: Run ESLint or similar
Format Check: Auto-format code
Debug Check: Check for debugger statements
Console Check: Check for console.log
Test Run: Run unit tests
Shell
# Git Show Advanced# Show commit
git show <commit-hash>
# Show with format
git show --pretty=format:"%h - %s"# Show with oneline
git show --oneline
# Show with diff
git show --diff
# Show with stat
git show --stat# Show with raw
git show --raw
# Show with patch
git show --patch
# Show with no-patch
git show --no-patch
# Show with quiet
git show -q
# Show with abbrev
git show --abbrev-commit
# Git Help# Show help
git help# Show help for command
git help commit
# Show help with verbose
git help -v
# Show help with guide
git help -g
# Show help with web
git help -w commit
# Show help with info
git help -i commit
# Show help with man
git help -m commit
# Show help with all
git help -a
# Show help with user-manual
git help user-manual
# Show help with tutorial
git help tutorial
Quick answerDetached HEAD occurs when checking out a commit directly. It can be fixed by creating a branch or switching back.
Create Branch: git branch new-branch
Switch to Branch: git switch new-branch
Reset to Branch: git reset --hard main
Checkout Branch: git checkout main
Merge Changes: git merge <commit-hash>
Shell
// Git Detached HEAD - How to Fix
# Scenario: You're in detached HEAD state# This happens when you checkout a specific commit
git checkout abc1234
# Option 1: Create a new branch from the detached HEAD
git branch new-branch
git checkout new-branch
# Or combine both:
git checkout -b new-branch
# Option 2: Switch back to a branch
git checkout main
# This will show a warning if you have uncommitted changes# Option 3: Keep changes by creating a branch
git switch -c my-new-branch
git add .
git commit -m "Saved changes from detached HEAD"# Option 4: Reset to a branch (discard changes)
git reset --hard main
# Option 5: Merge the commit into an existing branch
git checkout main
git merge abc1234
# Option 6: Cherry-pick the commit
git checkout main
git cherry-pick abc1234
# Check current state
git status
git log --oneline
Quick answerGit with CI/CD integrates Git with continuous integration and deployment pipelines for automated testing and deployment.
GitHub Actions: Automate workflows
GitLab CI: Built-in CI/CD
Jenkins: Git integration with Jenkins
Webhooks: Trigger builds on push
Status Checks: Required checks before merge
Shell
# Git Advanced Workflows# Monorepo with Git# Use submodules for large projects
git submodule add https://github.com/user/lib.git lib/
# Use subtree for better integration
git subtree add --prefix=lib https://github.com/user/lib.git main
# Git with CI/CD# .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: npm test# Git with semantic versioning# Use tags for releases
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0
# Git with conventional commits# Commit format: type(scope): subject# Example: feat(auth): Add login feature# Example: fix(api): Fix user validation
Git is a distributed version control system used to track code changes, collaborate with teams, and maintain project history.
What is the difference between Git and GitHub?
Git is a version control system, while GitHub is a cloud platform that hosts Git repositories and provides collaboration features.
What is a Git branch?
A Git branch is an independent line of development that allows developers to work on features or fixes without affecting the main codebase.
What is Git merge?
Git merge combines changes from one branch into another while preserving the commit history.
What is Git rebase?
Git rebase moves or reapplies commits onto another base branch to create a cleaner project history.
What is Git stash?
Git stash temporarily saves uncommitted changes so you can switch branches or perform other tasks without committing incomplete work.
How do you resolve merge conflicts in Git?
Merge conflicts are resolved by editing conflicting files, selecting the correct changes, staging the resolved files, and completing the merge with a commit.
What is a Git repository?
A Git repository is a storage location that contains project files, commit history, branches, and version tracking information.
Why is Git important for software development?
Git enables version tracking, team collaboration, safe experimentation with branches, efficient code reviews, and reliable project management.
AK
Written and reviewed by Abhishek Kumar
Founder, InterviewPitch · 10+ years in IT. Questions are collected from real interview rounds and refreshed regularly. Spotted an error? Tell us.