Follow AiTechWorlds on LinkedIn for professional AI content!Follow Now →
🔀
Programming

Git Commands Cheat Sheet

All git commands you actually need — init, commit, branch, merge, rebase, reset and more.

Back to Notes Library

Git Commands Cheat Sheet

Setup & Configuration

bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"
git config --list                 # view all config

Starting a Repository

bash
git init                          # new repo in current folder
git clone <url>                   # clone remote repo
git clone <url> my-folder         # clone into specific folder

Core Workflow

bash
git status                        # check what changed
git diff                          # unstaged changes
git diff --staged                 # staged changes

git add file.txt                  # stage specific file
git add .                         # stage all changes
git add -p                        # stage hunks interactively

git commit -m "message"           # commit with message
git commit --amend                # edit last commit (local only!)

Branching

bash
git branch                        # list branches
git branch -a                     # list all (including remote)
git branch feature/login          # create branch
git checkout feature/login        # switch to branch
git checkout -b feature/login     # create + switch

# Modern syntax (Git 2.23+)
git switch feature/login          # switch branch
git switch -c feature/login       # create + switch

git branch -d feature/login       # delete merged branch
git branch -D feature/login       # force delete

Merging & Rebasing

bash
# Merge
git checkout main
git merge feature/login           # merge feature into main
git merge --no-ff feature/login   # preserve merge commit

# Rebase
git checkout feature/login
git rebase main                   # rebase onto main

# Interactive rebase (last 3 commits)
git rebase -i HEAD~3
# Options: pick, squash, reword, drop

Remote Operations

bash
git remote -v                     # list remotes
git remote add origin <url>       # add remote
git remote remove origin          # remove remote

git fetch origin                  # download without merge
git pull origin main              # fetch + merge
git pull --rebase origin main     # fetch + rebase

git push origin main              # push to remote
git push -u origin feature/login  # push + set upstream
git push --force-with-lease       # safer force push

Undoing Changes

bash
# Unstage file (keep changes)
git restore --staged file.txt

# Discard changes in working directory
git restore file.txt

# Undo last commit (keep changes staged)
git reset --soft HEAD~1

# Undo last commit (keep changes unstaged)
git reset HEAD~1

# Undo last commit (discard changes!)
git reset --hard HEAD~1

# Revert a commit (creates new commit)
git revert abc1234

Stashing

bash
git stash                         # save changes temporarily
git stash push -m "work in progress"
git stash list                    # view stashes
git stash pop                     # apply + remove stash
git stash apply stash@{0}         # apply without removing
git stash drop stash@{0}          # delete a stash
git stash clear                   # delete all stashes

Log & History

bash
git log                           # full history
git log --oneline                 # compact view
git log --oneline --graph         # with branch graph
git log -10                       # last 10 commits
git log --author="John"           # filter by author
git log --since="2024-01-01"      # since date
git log -- file.txt               # history of a file

git show abc1234                  # show commit details
git blame file.txt                # line-by-line authorship

Tags

bash
git tag                           # list tags
git tag v1.0.0                    # lightweight tag
git tag -a v1.0.0 -m "Release"   # annotated tag
git push origin v1.0.0            # push tag
git push origin --tags            # push all tags
git tag -d v1.0.0                 # delete local tag

Useful Shortcuts

bash
# Search commits
git log --grep="fix bug"

# Cherry-pick a commit
git cherry-pick abc1234

# Clean untracked files
git clean -fd

# Show file at specific commit
git show abc1234:src/file.ts

.gitignore Patterns

gitignore
node_modules/       # ignore folder
*.log               # ignore by extension
.env                # ignore specific file
dist/               # ignore build output
!important.log      # exception: track this

Common Workflows

Feature Branch Workflow

bash
git checkout -b feature/new-feature main
# ... make changes ...
git add . && git commit -m "Add new feature"
git push -u origin feature/new-feature
# Create PR on GitHub/GitLab
git checkout main && git pull

Fix a Mistake on Last Commit

bash
git add forgotten-file.txt
git commit --amend --no-edit
📱

Get more notes like this daily on Telegram!

Free study notes, cheat sheets & AI tips

Join Free →
10K+ Members Growing Daily

Get Free AI Notes Daily

Join AiTechWorlds on Telegram and get daily AI tips, prompt engineering templates, coding resources, and exclusive content — 100% free!

📚 Free Study Notes🤖 AI Tips Daily⚡ Prompt Templates💻 Coding Resources
Join Free Channel

No spam. Leave anytime.

!