git init
Initializes a repository in the current directory
Practical example
git init # or with a name
git init myproject
developer reference
A practical Git reference with search, one-click copy and printable PDF output.
42 commands visible
git init
Initializes a repository in the current directory
git init # or with a name
git init myproject
git clone <url>
Clones a remote repository locally
git clone https://github.com/user/repo.git
# into a specific directory
git clone <url> myfolder
# latest commit only
git clone --depth 1 <url>
git remote add origin <url>
Links a local repository to a remote
git remote add origin https://github.com/user/repo.git
git status
Shows modified, staged and untracked files
git status
git status -s # compact output
git add <file>
Adds files to the staging area
git add index.html
git add . # all changes
git add -p # interactive, hunk by hunk
git commit -m "<msg>"
Commits staged changes with a message
git commit -m "feat: add login page"
git commit -am "fix: typo" # stage and commit tracked changes
git commit --amend
Edits the last commit message or content
Replaces the last commit. Coordinate before amending a published commit.
git commit --amend -m "new message"
# add forgotten files
git add file.txt && git commit --amend --no-edit
git rm --cached <file>
Removes a file from Git’s index; the local file remains. Commit to stop tracking it.
The next commit removes this file from the repository; the local copy remains.
git rm --cached secret.env
git stash
Temporarily shelves uncommitted changes
git stash
git stash push -m "wip: login form"
git stash list
Lists all stashes
git stash list
git stash pop
Applies a stash and removes it only if application succeeds
Check conflicts before continuing. Git retains the stash when application fails.
git stash pop
git stash pop stash@{2}
git stash apply [stash@{n}]
Applies a stash without removing it
git stash apply stash@{1}
git stash drop [stash@{n}]
Deletes a specific stash
Deletes the selected stash. Recovery is not guaranteed.
git stash drop stash@{0}
git stash branch <branch>
Creates a branch from a stash and applies it
git stash branch feature/login stash@{0}
git branch
Lists local branches (-a includes remotes)
git branch
git branch -a
git switch -c <name>
Creates and switches to a new branch
git switch -c feature/auth
# legacy syntax
git checkout -b feature/auth
git switch <branch>
Switches to an existing branch
git switch main
git switch - # previous branch
git merge <branch>
Merges a branch into the current branch
git switch main
git merge feature/auth
git merge --no-ff feature/auth # preserve merge commits
git rebase <branch>
Rewrites branch history onto a new base
Rewrites commit history. Coordinate before rebasing a shared branch.
git switch feature/auth
git rebase main
git rebase -i HEAD~3 # interactive: squash or reorder
git cherry-pick <hash>
Applies a specific commit to the current branch
git cherry-pick a3f1c2e
git cherry-pick a3f1c2e^..b7d9e0f # commit range
git branch -d <branch>
Deletes a branch only if already merged
git branch -d feature/auth
git branch -D feature/auth # force deletion
git remote -v
git fetch [remote]
Downloads updates without merging
git fetch origin
git fetch --all
git pull
Fetches and merges the remote branch
git pull
git pull --rebase # with rebase
git push <remote> <branch>
Pushes commits to the remote
git push origin main
git push -u origin feature/auth # set upstream tracking
git push --force-with-lease
Safer force push that fails if remote has new commits
Rewrites remote history. Verify the branch and expected remote state before pushing.
git push --force-with-lease
git log --oneline --graph
Compact history with branch graph
git log --oneline --graph --decorate
git log -5 --oneline # latest 5 commits
git log -S "<string>"
Finds commits that add or remove a string
git log -S "apiKey" --oneline
git blame <file>
Shows who last changed each line of a file
git blame src/main.py
git blame -L 10,20 src/main.py
git reflog
# recover a lost commit
git checkout HEAD@{2}
git restore <file>
Discards local unstaged changes
Discards unstaged edits to the selected file. Git may not be able to recover them.
git restore index.html
git restore . # all files
git restore --staged <file>
Unstages changes while keeping edits
git restore --staged secret.env
git reset HEAD~1
Undoes the last commit while keeping changes
The default mixed reset keeps working files but clears staging. --hard also discards tracked edits.
git reset --soft HEAD~1 # keep changes staged
git reset HEAD~1 # unstage, keep files
git reset --hard HEAD~1 # DESTROYS local changes
git revert <hash>
Creates a commit that reverses another commit, safe for remotes
git revert a3f1c2e --no-edit
git clean -fd
Deletes untracked files and directories, irreversible
Permanently deletes untracked files and directories. Preview with git clean -nd first.
git clean -fdn # preview
git clean -fd
git diff
Changes not staged yet
git diff
git diff src/main.py
git diff --staged
Staged changes ready to commit
git diff --staged
git diff <branch1> <branch2>
Compares two branches
git diff main feature/auth
git diff --name-only main feature/auth
git config --global user.name "<name>"
Sets the global user name
git config --global user.name "Enzo"
git config --global user.email "<email>"
Sets the global email
git config --global user.email "enzo@example.com"
git config --global alias.<name> "<cmd>"
Creates an alias for a command
# git lg = graphical log
git config --global alias.lg "log --oneline --graph --decorate"
git config --list
Shows the active configuration
git config --list --show-origin
Use git restore --staged <file>. To stop tracking a file while keeping the local copy, use git rm --cached <file> and commit the removal.
Use Print PDF. The printable version includes all commands and practical examples, including sections hidden by a search.
Yes. Serve the static files with a local HTTP server. Search, filters and copying run in your browser; the Regex tester requires a worker-enabled HTTP context.