Professional Git Command Reference

From basic to advanced professional usage

A comprehensive guide to Git commands for developers of all skill levels. Master everything from basic version control to advanced professional workflows.

Repository & Setup

Initialization Basic
git init

Initialize a new Git repository in the current directory

git clone <url>

Clone an existing repository from a remote URL

git clone --depth 1 <url>

Shallow clone (only recent history, saves space)

Configuration Basic
git config --global user.name "Your Name"

Set your global username for commits

git config --global user.email "your@email.com"

Set your global email for commits

git config --global core.editor "code --wait"

Set VS Code as your default Git editor

git config --list

List all current Git configurations

Remote Management Intermediate
git remote add origin <url>

Add a new remote repository

git remote -v

List all remote repositories with URLs

git remote set-url origin <new-url>

Change the URL of a remote repository

git remote show origin

Show detailed information about a remote

Branching & Merging

Basic Branching Basic
git branch

List all local branches

git branch <branch-name>

Create a new branch

git checkout <branch-name>

Switch to a branch

git checkout -b <branch-name>

Create and switch to a new branch

Advanced Branching Intermediate
git branch -d <branch-name>

Delete a branch (safe)

git branch -D <branch-name>

Force delete a branch (even if not merged)

git branch -m <new-name>

Rename the current branch

git branch --set-upstream-to=origin/<branch>

Set upstream branch for tracking

Merging & Rebasing Intermediate
git merge <branch-name>

Merge a branch into the current branch

git merge --no-ff <branch-name>

Merge and always create a merge commit

git rebase <branch-name>

Rebase current branch onto another branch

git rebase -i HEAD~5

Interactive rebase of last 5 commits

Commits & History

Making Changes Basic
git status

Show the working tree status

git add <file>

Add a file to the staging area

git add .

Add all changes to the staging area

git commit -m "message"

Commit staged changes with a message

Commit History Intermediate
git log

Show commit history

git log --oneline --graph --all

Compact history with branch visualization

git log --since="2023-01-01"

Show commits since a specific date

git log -p <file>

Show change history for a specific file

Undoing Changes Advanced
git restore <file>

Discard changes in working directory

git restore --staged <file>

Unstage a file while keeping changes

git commit --amend

Modify the most recent commit

git revert <commit>

Create a new commit that undoes a previous commit

Remote & Collaboration

Basic Remote Operations Basic
git fetch

Download objects and refs from remote

git pull

Fetch from and integrate with remote branch

git pull --rebase

Pull with rebase instead of merge

git push

Push local commits to remote

Advanced Remote Operations Advanced
git push -u origin <branch>

Push and set upstream tracking

git push --force-with-lease

Force push safely (won't overwrite others' work)

git push origin --delete <branch>

Delete a remote branch

git fetch --prune

Remove remote-tracking references that no longer exist

Collaboration Workflows Professional
git cherry-pick <commit>

Apply a specific commit to current branch

git stash

Temporarily stash changes

git stash pop

Apply stashed changes and remove from stash

git bisect start

Binary search to find which commit introduced a bug

Advanced Tools

Debugging & Investigation Professional
git blame <file>

Show who last modified each line of a file

git show <commit>

Show various types of objects (commits, tags, etc.)

git diff

Show changes between commits, commit and working tree, etc.

git reflog

Show a log of where HEAD and branch references have been

Submodules & Subtrees Professional
git submodule add <url> <path>

Add a submodule to the repository

git submodule update --init --recursive

Initialize and update submodules

git subtree add --prefix=<dir> <repo> <ref>

Add a subtree to the repository

git subtree pull --prefix=<dir> <repo> <ref>

Update a subtree with latest changes

Maintenance & Optimization Professional
git gc

Cleanup unnecessary files and optimize repository

git fsck

Verify the integrity of the Git file system

git repack -ad

Repack repository for better performance

git filter-repo --path <file> --invert-paths

Remove sensitive data from history (requires git-filter-repo)

Repository & Setup

Initialization Basic
git init

Initialize a new Git repository in the current directory

git clone <url>

Clone an existing repository from a remote URL

git clone --depth 1 <url>

Shallow clone (only recent history, saves space)

Configuration Basic
git config --global user.name "Your Name"

Set your global username for commits

git config --global user.email "your@email.com"

Set your global email for commits

git config --global core.editor "code --wait"

Set VS Code as your default Git editor

git config --list

List all current Git configurations

Remote Management Intermediate
git remote add origin <url>

Add a new remote repository

git remote -v

List all remote repositories with URLs

git remote set-url origin <new-url>

Change the URL of a remote repository

git remote show origin

Show detailed information about a remote

Branching & Merging

Basic Branching Basic
git branch

List all local branches

git branch <branch-name>

Create a new branch

git checkout <branch-name>

Switch to a branch

git checkout -b <branch-name>

Create and switch to a new branch

Advanced Branching Intermediate
git branch -d <branch-name>

Delete a branch (safe)

git branch -D <branch-name>

Force delete a branch (even if not merged)

git branch -m <new-name>

Rename the current branch

git branch --set-upstream-to=origin/<branch>

Set upstream branch for tracking

Merging & Rebasing Intermediate
git merge <branch-name>

Merge a branch into the current branch

git merge --no-ff <branch-name>

Merge and always create a merge commit

git rebase <branch-name>

Rebase current branch onto another branch

git rebase -i HEAD~5

Interactive rebase of last 5 commits

Commits & History

Making Changes Basic
git status

Show the working tree status

git add <file>

Add a file to the staging area

git add .

Add all changes to the staging area

git commit -m "message"

Commit staged changes with a message

Commit History Intermediate
git log

Show commit history

git log --oneline --graph --all

Compact history with branch visualization

git log --since="2023-01-01"

Show commits since a specific date

git log -p <file>

Show change history for a specific file

Undoing Changes Advanced
git restore <file>

Discard changes in working directory

git restore --staged <file>

Unstage a file while keeping changes

git commit --amend

Modify the most recent commit

git revert <commit>

Create a new commit that undoes a previous commit

Remote & Collaboration

Basic Remote Operations Basic
git fetch

Download objects and refs from remote

git pull

Fetch from and integrate with remote branch

git pull --rebase

Pull with rebase instead of merge

git push

Push local commits to remote

Advanced Remote Operations Advanced
git push -u origin <branch>

Push and set upstream tracking

git push --force-with-lease

Force push safely (won't overwrite others' work)

git push origin --delete <branch>

Delete a remote branch

git fetch --prune

Remove remote-tracking references that no longer exist

Collaboration Workflows Professional
git cherry-pick <commit>

Apply a specific commit to current branch

git stash

Temporarily stash changes

git stash pop

Apply stashed changes and remove from stash

git bisect start

Binary search to find which commit introduced a bug

Advanced Tools

Debugging & Investigation Professional
git blame <file>

Show who last modified each line of a file

git show <commit>

Show various types of objects (commits, tags, etc.)

git diff

Show changes between commits, commit and working tree, etc.

git reflog

Show a log of where HEAD and branch references have been

Submodules & Subtrees Professional
git submodule add <url> <path>

Add a submodule to the repository

git submodule update --init --recursive

Initialize and update submodules

git subtree add --prefix=<dir> <repo> <ref>

Add a subtree to the repository

git subtree pull --prefix=<dir> <repo> <ref>

Update a subtree with latest changes

Maintenance & Optimization Professional
git gc

Cleanup unnecessary files and optimize repository

git fsck

Verify the integrity of the Git file system

git repack -ad

Repack repository for better performance

git filter-repo --path <file> --invert-paths

Remove sensitive data from history (requires git-filter-repo)

Pro Tip

Create aliases for frequently used Git commands to save time. For example: git config --global alias.co checkout lets you use git co instead of git checkout.

Safety Measures

Always use git push --force-with-lease instead of --force. It will fail if the remote has new commits that you haven't fetched, preventing you from overwriting your teammates' work.

Pro Tip

Create aliases for frequently used Git commands to save time. For example: git config --global alias.co checkout lets you use git co instead of git checkout.

Safety Measures

Always use git push --force-with-lease instead of --force. It will fail if the remote has new commits that you haven't fetched, preventing you from overwriting your teammates' work.