Git 16+ Core Commands Cheat Sheet
An interactive, production-ready reference catalog. Speed up version control tasks, branch management, and repository syncing with detailed descriptions and optimization tips.
Git & DevOps Master Q&A Hub
Complete your interview preparation! Pair this quick command cheat sheet with our master-level **Scenario-Based Git Q&A** to ace code review & system deployment rounds.
Start a New Local Repository
Initializes a blank Git repository in the current directory. Creates the hidden '.git' directory containing all history and tracking databases.
git initPro-Tip: Check if the directory is already version-controlled by verifying the absence of a hidden .git folder first to avoid nested repositories.
Clone an Existing Remote Repository
Downloads an existing repository from a remote hosting provider (like GitHub, GitLab, or Bitbucket) and sets up a local copy linked to the remote source.
git clone <url>Pro-Tip: You can clone to a specific directory name by adding it to the end of the command: git clone <url> my-custom-folder.
Set Up Remote Repository Link
Links a local Git repository with a remote repository URL. The keyword 'origin' serves as an alias/pointer to this remote URL.
git remote add origin <url>Pro-Tip: Verify your configured remote connections and their fetch/push URLs at any time using: git remote -v.
Prepare Files to Commit
Stages all new, modified, and deleted files in the current directory and its subdirectories, preparing them for the next commit.
git add .Pro-Tip: Use 'git add <file_path>' to stage files selectively, or 'git add -p' to interactively stage specific modifications (hunks) within a single file.
Check Repository Status
Displays the status of the working tree and staging area. Shows which changes are staged, modified but unstaged, or untracked by Git.
git statusPro-Tip: Use the short-format option 'git status -s' for a quick, highly readable view of modified/added files.
Commit Staged Changes
Records the staged changes as a new commit snapshot in the local repository's history, using the descriptive message provided.
git commit -m "message"Pro-Tip: Keep your commit messages clean and descriptive: use the imperative mood (e.g. 'Add login validation' instead of 'Added login validation').
Check Differences
Shows line-by-line differences between commits, branches, or working directory files. Typically compares files between two branches.
git diff <branch_1> <branch_2>Pro-Tip: To compare your current working directory changes against the staging area, just run: git diff.
Check Repository Activities & Commit Logs
Displays the chronological commit history of the active branch, showing details like commit hash, author, date, and commit messages.
git logPro-Tip: For a clean, visual representation of branch splits and merges in a single line, run: git log --oneline --graph --all.
Create and Switch to a New Branch
Creates a new branch pointing to the current HEAD commit and immediately switches the working directory onto that branch.
git checkout -b <branch_name>Pro-Tip: In modern Git (v2.23+), you can use the more specific switch command: git switch -c <branch_name>.
Switch to a Branch
Switches the active workspace context to an existing branch. Updates files in the working directory to match the target branch's commit history.
git checkout <branch_name>Pro-Tip: Ensure your active changes are committed or stashed before switching branches, otherwise Git may block you or merge them into the destination branch.
List All Repository Branches
Lists all local branches in the active repository. Identifies the currently checked-out branch with an asterisk (*).
git branchPro-Tip: To see all local AND remote branches currently tracked from the remote servers, use: git branch -a.
Delete a Branch
Deletes a local branch. Git checks if the branch's changes have been fully merged before allowing deletion as a safety measure.
git branch -d <branch_name>Pro-Tip: If you want to discard changes in an unmerged branch and delete it anyway, use the force deletion flag: git branch -D <branch_name>.
Merge a Branch into Current Branch
Integrates the commit history of the specified branch into your current active branch. Resolves history conflicts or triggers fast-forward merges.
git merge <branch_name>Pro-Tip: Use 'git merge --no-ff <branch_name>' to force Git to create a merge commit, preserving a clear historical record of the branch's integration.
Send Commits to Remote Repository
Uploads local commits from your local 'master' branch (or any specified branch) up to the default remote repository ('origin').
git push origin masterPro-Tip: For the first push of a new branch, use the -u option: 'git push -u origin <branch>' to link local and remote tracking branches.
Update with Master Code (Fetch & Merge)
Fetches changes from the remote repository's 'master' branch and immediately merges them into your active branch.
git pull origin masterPro-Tip: Rely on 'git pull --rebase origin master' to fetch changes and replay your local commits on top of them, keeping commit histories linear.
Tag Code at Specific Commits
Creates a tag reference pointing to a specific commit hash (identified by its first 10 characters). Useful for highlighting release versions.
git tag <tag_name> <10_chars_commit_id>Pro-Tip: Create annotated tags with release comments using: git tag -a v1.0.0 <commit_id> -m "Release version 1.0.0".
Git & Version Control FAQ Prep
Quick review of standard git workflow concepts, branching merges, and config parameters commonly asked in technical rounds.
What is the difference between 'git init' and 'git clone', and when would you use each?
'git init' creates a blank, local Git repository in an existing project directory, while 'git clone' copies an existing remote Git repository onto your local machine, automatically setting up a remote tracking link.
# Start a new local repository
mkdir my-new-project
cd my-new-project
git init
# Clone an existing remote repository
git clone https://github.com/careerraah/playwright-tests.gitDeep Dive Explanation
Use 'git init' when you are starting a new project locally from scratch or taking an existing unversioned folder and converting it into a Git repository. Use 'git clone' when the repository already exists on a hosting provider (like GitHub or Bitbucket) and you want to download it and start contributing.
How does the 'git status' command help you manage your working directory and staging area?
'git status' displays the state of your working directory and staging area, listing files that are modified but unstaged, files staged for the next commit, and untracked files.
# Check current staging and working directory status
git statusDeep Dive Explanation
'git status' is a safe, non-destructive inspection tool. It tells you which local changes are currently ready to be committed, which modified files still need to be staged, and which files are ignored or untracked. It also shows whether your local branch is ahead, behind, or synced with its remote counterpart.
Explain the differences between staging files with 'git add <file>', 'git add .', and 'git add -A'.
'git add <file>' stages a single specific file, 'git add .' stages all new and modified files in the current folder and subfolders (but ignores deleted files in older Git versions), while 'git add -A' stages all changes globally, including modifications, additions, and deletions.
# Stage a single file
git add src/tests/login.spec.ts
# Stage all files in the current directory tree
git add .
# Stage all changes in the entire repository (including deletions)
git add -ADeep Dive Explanation
Staging prepares changes for the next commit. In modern Git (version 2.x+), both '.' and '-A' stage deletions, but 'git add .' is restricted to the current directory and its descendants, whereas 'git add -A' stages all changes in the entire working repository regardless of your current directory location.
What is the purpose of 'git commit', and how does the '-m' flag work?
'git commit' saves your staged changes to the local repository's version history, creating a unique commit hash. The '-m' flag allows you to write the commit message directly in the terminal instead of opening a default text editor.
# Commit staged changes with a inline message
git commit -m "feat(auth): add email verification to login tests"Deep Dive Explanation
A commit is a snapshot of your project's index at a specific moment. Writing clean, descriptive commit messages is vital for teamwork. If you run 'git commit' without the '-m' flag, Git will open your system's default text editor (like vim or nano) to write a multi-line commit message.
How do 'git push' and 'git pull' commands sync code with a remote repository?
'git push' uploads your local branch commits to the remote repository (e.g. GitHub), while 'git pull' downloads (fetches) the latest changes from the remote repository and automatically merges them into your active local branch.
# Push local commits to remote branch
git push origin feature-login
# Pull latest changes from remote tracking branch
git pull origin mainDeep Dive Explanation
Working in teams requires syncing histories. 'git push' pushes local commits to update the remote. 'git pull' is actually a combination of two operations: first, it runs 'git fetch' to download remote updates, and second, it automatically runs 'git merge' to merge those updates into your working branch.
How do you use 'git log' and 'git diff' to inspect past commits and review uncommitted modifications?
'git log' displays the chronological history of commits on your active branch, while 'git diff' displays the line-by-line differences between your current working directory and the staged index (unstaged changes).
# View commit history
git log
# View simplified one-liner history
git log --oneline
# View unstaged code changes
git diff
# View staged changes preparing for commit
git diff --stagedDeep Dive Explanation
'git log' shows commit metadata (author, date, message, hash) so you can review history. 'git diff' displays additions (green `+` lines) and deletions (red `-` lines). To inspect staged changes, use 'git diff --staged'.
Want to test your full capability?
Try our comprehensive scenario-based questions page to test your knowledge on rebasing squashes, GitOps deployments, pre-commit config, and reflog recoveries.
Have a specific question? Ask our AI Coach!
Explore custom roadmaps, ask technical questions, and design your QA and AI interview preparation path.