Git Cheat Sheet

Git is a distributed version control system that tracks changes to files, allowing multiple people to collaborate on a project safely.


Configuration

Set your identity.

git config --global user.name "John Doe"
git config --global user.email "john@example.com"

View your configuration.

git config --list

Edit the configuration manually.

git config --global --edit

Create a Repository

Initialize a new repository.

git init

Clone an existing repository.

git clone <repository-url>

Clone into a specific directory.

git clone <repository-url> my-project

Repository Status

See modified, staged, and untracked files.

git status

Short version.

git status -s

Staging Files

Stage one file.

git add file.txt

Stage multiple files.

git add file1 file2

Stage everything.

git add .

Stage modified and deleted files.

git add -u

Interactively choose changes.

git add -p

Committing

Create a commit.

git commit -m "Add login page"

Commit staged changes using your editor.

git commit

Stage and commit tracked files.

git commit -am "Fix bug"

Amend the last commit.

git commit --amend

Viewing History

Compact history.

git log --oneline

Graph view.

git log --graph --oneline --all

Show commit details.

git show <commit>

See who modified each line.

git blame file.txt

Branches

List branches.

git branch

Create a branch.

git branch feature

Switch branches.

git switch feature

Create and switch.

git switch -c feature

Old equivalent.

git checkout feature

Delete a merged branch.

git branch -d feature

Force delete.

git branch -D feature

Rename current branch.

git branch -m new-name

Merging

Merge another branch.

git merge feature

Abort a conflicted merge.

git merge --abort

Rebasing

Rebase onto another branch.

git rebase main

Interactive rebase.

git rebase -i HEAD~5

Abort.

git rebase --abort

Continue after conflicts.

git rebase --continue

Remote Repositories

List remotes.

git remote -v

Add a remote.

git remote add origin <url>

Change remote URL.

git remote set-url origin <url>

Remove a remote.

git remote remove origin

Fetch & Pull

Download remote changes.

git fetch

Download and merge.

git pull

Rebase instead of merge.

git pull --rebase

Push

Push current branch.

git push

Push and set upstream.

git push -u origin main

Force push (use carefully).

git push --force-with-lease

Undo Changes

Discard unstaged changes.

git restore file.txt

Unstage a file.

git restore --staged file.txt

Restore both.

git restore --source=HEAD --staged --worktree file.txt

Reset last commit (keep files).

git reset --soft HEAD~1

Reset and unstage.

git reset HEAD~1

Delete last commit completely.

git reset --hard HEAD~1

Stashing

Save work temporarily.

git stash

List stashes.

git stash list

Restore latest stash.

git stash pop

Restore without deleting.

git stash apply

Delete stash.

git stash drop

Tags

Create a lightweight tag.

git tag v1.0

Annotated tag.

git tag -a v1.0 -m "Version 1.0"

List tags.

git tag

Push tags.

git push --tags

Comparing Changes

Working tree vs staged.

git diff

Staged vs last commit.

git diff --cached

Compare two commits.

git diff commit1 commit2

Cleaning

Remove untracked files.

git clean -f

Remove directories too.

git clean -fd

Preview before deleting.

git clean -n

Ignore Files

Create a .gitignore.

Example:

node_modules/
dist/
.env
*.log
*.tmp

Useful Inspection Commands

Current branch.

git branch --show-current

Repository root.

git rev-parse --show-toplevel

Tracked files.

git ls-files

Recent commits.

git reflog

Common Workflows

Create a New Feature

git switch -c feature/navbar
 
git add .
 
git commit -m "Implement navigation bar"
 
git push -u origin feature/navbar

Update From Main

git switch main
 
git pull
 
git switch feature/navbar
 
git rebase main

Resolve a Conflict

git status
 
# Edit conflicting files
 
git add .
 
git rebase --continue

or

git merge --continue

Git Areas

Working Directory

       │ git add

Staging Area (Index)

       │ git commit

Local Repository

       │ git push

Remote Repository

Most Useful Commands

git init
 
git clone
 
git status
 
git add
 
git commit
 
git log
 
git diff
 
git switch
 
git branch
 
git merge
 
git rebase
 
git fetch
 
git pull
 
git push
 
git stash
 
git restore
 
git reset
 
git tag
 
git remote
 
git clean

Git Lifecycle

Create file


Modified

git add

Staged

git commit

Committed

git push

Remote Repository

Golden Rule

Before making major changes, remember this sequence:

git status
 
git pull --rebase
 
# Work on your code
 
git add .
 
git commit -m "Describe your changes"
 
git push

Checking git status frequently is one of the best habits you can develop—it tells you exactly where your files are in Git’s workflow.