πŸ’‘ Share CareerRaah β€” it's 100% free! πŸš€

Back to Home Dashboard| Git Version Control

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.

Experience:
πŸ™

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.

Setup & Config

Start a New Local Repository

Beginner

Initializes a blank Git repository in the current directory. Creates the hidden '.git' directory containing all history and tracking databases.

git init
πŸ’‘

Pro-Tip: Check if the directory is already version-controlled by verifying the absence of a hidden .git folder first to avoid nested repositories.

ID: #1
Setup & Config

Clone an Existing Remote Repository

Beginner

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.

ID: #2
Setup & Config

Set Up Remote Repository Link

Intermediate

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.

ID: #3
Working Directory

Prepare Files to Commit

Beginner

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.

ID: #4
Working Directory

Check Repository Status

Beginner

Displays the status of the working tree and staging area. Shows which changes are staged, modified but unstaged, or untracked by Git.

git status
πŸ’‘

Pro-Tip: Use the short-format option 'git status -s' for a quick, highly readable view of modified/added files.

ID: #5
Working Directory

Commit Staged Changes

Beginner

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').

ID: #6
Working Directory

Check Differences

Intermediate

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.

ID: #7
Working Directory

Check Repository Activities & Commit Logs

Beginner

Displays the chronological commit history of the active branch, showing details like commit hash, author, date, and commit messages.

git log
πŸ’‘

Pro-Tip: For a clean, visual representation of branch splits and merges in a single line, run: git log --oneline --graph --all.

ID: #8
Branches & Merging

Create and Switch to a New Branch

Beginner

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>.

ID: #9
Branches & Merging

Switch to a Branch

Beginner

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.

ID: #10
Branches & Merging

List All Repository Branches

Intermediate

Lists all local branches in the active repository. Identifies the currently checked-out branch with an asterisk (*).

git branch
πŸ’‘

Pro-Tip: To see all local AND remote branches currently tracked from the remote servers, use: git branch -a.

ID: #11
Branches & Merging

Delete a Branch

Intermediate

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>.

ID: #12
Branches & Merging

Merge a Branch into Current Branch

Intermediate

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.

ID: #13
Syncing & Remote

Send Commits to Remote Repository

Beginner

Uploads local commits from your local 'master' branch (or any specified branch) up to the default remote repository ('origin').

git push origin master
πŸ’‘

Pro-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.

ID: #14
Syncing & Remote

Update with Master Code (Fetch & Merge)

Beginner

Fetches changes from the remote repository's 'master' branch and immediately merges them into your active branch.

git pull origin master
πŸ’‘

Pro-Tip: Rely on 'git pull --rebase origin master' to fetch changes and replay your local commits on top of them, keeping commit histories linear.

ID: #15
Syncing & Remote

Tag Code at Specific Commits

Advanced

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".

ID: #16
πŸ™

Git & Version Control FAQ Prep

Quick review of standard git workflow concepts, branching merges, and config parameters commonly asked in technical rounds.

GIT-1

What is the difference between 'git init' and 'git clone', and when would you use each?

Direct Answer

'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.

git.commands.sh
# 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.git

Deep 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.

GIT-2

How does the 'git status' command help you manage your working directory and staging area?

Direct Answer

'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.

git.commands.sh
# Check current staging and working directory status
git status

Deep 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.

GIT-3

Explain the differences between staging files with 'git add <file>', 'git add .', and 'git add -A'.

Direct Answer

'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.

git.commands.sh
# 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 -A

Deep 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.

GIT-4

What is the purpose of 'git commit', and how does the '-m' flag work?

Direct Answer

'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.

git.commands.sh
# 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.

GIT-5

How do 'git push' and 'git pull' commands sync code with a remote repository?

Direct Answer

'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.

git.commands.sh
# Push local commits to remote branch
git push origin feature-login

# Pull latest changes from remote tracking branch
git pull origin main

Deep 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.

GIT-6

How do you use 'git log' and 'git diff' to inspect past commits and review uncommitted modifications?

Direct Answer

'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).

git.commands.sh
# 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 --staged

Deep 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.

Ask AI Coach