Skip to content

Instantly share code, notes, and snippets.

@aleksbelic
Last active March 22, 2026 10:08
Show Gist options
  • Select an option

  • Save aleksbelic/e2c44514232fdf9b6de823cec61c3ac3 to your computer and use it in GitHub Desktop.

Select an option

Save aleksbelic/e2c44514232fdf9b6de823cec61c3ac3 to your computer and use it in GitHub Desktop.
Git commands
# initialize a new Git repository in the current directory
git init
# show working tree status (staged, unstaged, untracked files)
git status
/*** CLONE ***/
# clone a remote repository into a new directory (named after the repo by default)
git clone <REPO_URL>
/*** ADD ***/
# stage a specific file
git add <FILE_NAME>
# stage all changes in entire repository (all folders & subfolders)
git add -A
# OR
git add --all
# stage all changes in current directory and its subfolders
git add .
# ⚠️ NOT RECOMMENDED (shell expansion, not true Git behavior)
# stages files matched by shell — skips hidden files/dirs, does not stage deletions
git add *
# stage modified & deleted files (no new files - only tracked), across the repository
git add -u
# interactively review and stage changes (hunks) from files
git add -p
/*** DIFF ***/
# show all unstaged changes in the repo
git diff
# show a summary of unstaged changes (files changed, insertions, deletions)
git diff --stat
# show all staged changes in the repo
git diff --staged
# show unstaged changes in a file
git diff <FILE_NAME>
# show staged changes in a file
git diff --staged <FILE_NAME>
# OR
git diff --cached <FILE_NAME>
/*** COMMIT ***/
# create a commit from staged changes
# note: opens text editor for commit message
git commit
# commit staged changes with a message
git commit -m "<MESSAGE>"
# stage modified/deleted tracked files AND commit them (does NOT include new untracked files)
# note: opens text editor for commit message
git commit -a
# stage modified/deleted tracked files AND commit them with a message (skips staging area)
git commit -a -m "<MESSAGE>"
# OR
git commit -am "<MESSAGE>"
# modify the most recent commit — opens editor to change the message (includes any currently staged changes into that commit)
git commit --amend
# amend commit WITHOUT changing the message
git commit --amend --no-edit
# amend ONLY the message (no file changes)
git commit --amend -m "New message"
/*** LOG ***/
# show commit history
git log
# show commit history with per-file change statistics (lines added/removed, files changed)
git log --stat
# show concise commit history
git log --oneline
# show commit history with branch graph (all branches)
git log --oneline --graph --all
# show commit history + exact code changes
git log -p
# show last 2 commits history + exact code changes
git log -p -n 2
# OR
git log -p -2
# show commit history + exact code changes + ignore whitespace differences
git log -p -w
/*** CONFIG ***/
# list all Git config values (local + global + system)
git config --list
# OR
git config -l
# list all global config values
git config --global --list
# list all local config values
git config --local --list
# set your global Git username
git config --global user.name "<YOUR_NAME>"
# set your global Git email
git config --global user.email "<YOUR_EMAIL>"
# set your local Git username (repo specific)
git config --local user.name "<YOUR_LOCAL_NAME>"
# set your local Git email (repo specific)
git config --local user.email "<YOUR_LOCAL_EMAIL>"
# show globally set default text editor used by Git (if not set - Git falls back to system default)
git config --global core.editor
# show locally set default text editor used by Git (if not set - Git falls back to system default)
git config --local core.editor
# set globally the default branch name for newly initialized repositories (mostly main, master, trunk, develop etc.)
git config --global init.defaultBranch <BRANCH_NAME>
# cache Git credentials in memory for a short period (avoids re-entering password)
git config --global credential.helper cache
/*** BRANCH ***/
# list local branches (highlights the current branch with an asterisk *)
git branch
# show current branch
git branch --show-current
# list local and remote branches
git branch -a
# list all remote-tracking branches
git branch -r
# create a new branch without switching to it
git branch <BRANCH_NAME>
# rename the current branch
git branch -m <NEW_BRANCH_NAME>
# or rename a specific branch
git branch -m <OLD_BRANCH_NAME> <NEW_BRANCH_NAME>
# delete a local branch (only if it has been fully merged)
git branch -d <BRANCH_NAME>
# force delete a local branch (even if it has unmerged changes)
git branch -D <BRANCH_NAME>
/*** SWITCH ***/
# switch to another branch
git switch <BRANCH_NAME>
# create and switch to a new branch
git switch -c <NEW_BRANCH_NAME>
/*** PUSH ***/
# push local branch to remote and set it as the upstream tracking branch (after this, you can use just 'git push' / 'git pull' with no arguments)
git push -u <REMOTE_NAME> <REMOTE_BRANCH>
# delete a remote branch
git push <REMOTE_NAME> --delete <BRANCH_NAME>
# OR
git push <REMOTE_NAME> :<BRANCH_NAME>
/*** PULL ***/
# fetch and merge changes from the tracked upstream branch into the current branch
git pull
# fetch and merge a specific remote branch into the current branch
git pull <REMOTE_NAME> <BRANCH>
/*** FETCH ***/
# download changes from the remote (updates remote-tracking branches) but does NOT merge them into your local branches
git fetch
/*** REMOTE ***/
# list all remote connections with their fetch/push URLs
git remote -v
# show detailed information about a specific remote (tracked branches, fetch/push URLs)
git remote show <REMOTE_NAME>
# fetch updates from all remotes (updates all remote-tracking branches)
git remote update
/*** SHOW ***/
# show metadata and full changes introduced by a specific commit (you can use a short commit hash as long as it's unique)
git show <COMMIT_ID>
# show only commit metadata (no diff)
git show --no-patch <COMMIT_ID>
# show metadata and file change statistics of the latest commit
git show --stat
# show stats (files changed, insertions, deletions) for specific commit
git show --stat <COMMIT_ID>
# show only file names changed for specific commit
git show --name-only <COMMIT_ID>
/*** RM ***/
# remove a file from the working tree and stage its deletion
git rm <FILE_NAME>
/*** MV ***/
# rename or move a file and stage the change
git mv <FILE_NAME_OLD> <FILE_NAME_NEW>
/*** CHECKOUT ***/
# discard unstaged changes in a file (restores it to the last committed state)
git checkout <FILE_NAME>
# OR
git restore <FILE_NAME>
# interactively discard specific chunks of unstaged changes in a file
git checkout -p <FILE_NAME>
# OR
git restore -p <FILE_NAME>
# switch to an existing branch
git checkout <BRANCH_NAME>
# OR
git switch <BRANCH_NAME>
# detach HEAD and move to a specific commit (puts repo in 'detached HEAD' state)
git checkout <COMMIT_ID>
# create and switch to a new branch
git checkout -b <BRANCH_NAME>
# OR
git switch -c <BRANCH_NAME>
/*** RESET ***/
# unstage a specific file (moves it from staging area back to working directory, opposite of "add")
git reset HEAD <FILE_NAME>
# OR
git restore --staged <FILE_NAME>
# interactively unstage specific chunks of staged changes
git reset HEAD -p
# OR
git restore -p --staged
/*** REVERT ***/
# create a new commit that undoes the changes introduced by the last commit
git revert HEAD
# create a new commit that undoes the changes introduced by a specific commit
git revert <COMMIT_ID>
/*** MERGE ***/
# merge the specified branch into the current branch
git merge <BRANCH_NAME>
# abort an in-progress merge and restore the repo to its pre-merge state
git merge --abort
/*** RESTORE ***/
# discard unstaged changes in a file
git restore <FILE_NAME>
# unstage a file
git restore --staged <FILE_NAME>
/*** CLEAN ***/
# remove untracked files (⚠️ destructive — cannot be undone)
git clean -f
# remove untracked files & directories
git clean -fd
# preview what will be deleted
git clean -n
/*** STASH ***/
# stash current changes (tracked files only)
git stash
# stash with a custom message (recommended)
git stash push -m "<MESSAGE>"
# stash including untracked files (not ignored)
git stash push -u
# stash including untracked and ignored files
git stash push -a
# list all stashes
git stash list
# apply the most recent stash (keeps it in stash list)
git stash apply
# apply and remove the most recent stash
git stash pop
# apply a specific stash
git stash apply stash@{2}
# apply and remove a specific stash
git stash pop stash@{2}
# delete a specific stash
git stash drop stash@{1}
# delete the most recent stash
git stash drop
# clear all stashes (⚠️ destructive)
git stash clear
# show summary of a stash
git stash show
# show full diff of a stash
git stash show -p
# show full diff of a specific stash
git stash show -p stash@{0}
# stash specific files only
git stash push <FILE1> <FILE2>
# stash specific files with message
git stash push -m "stash only these files" <FILE1> <FILE2>
# interactively select hunks to stash (very powerful)
git stash push -p
# creates a new branch, applies the stash, and drops it (recommended for bigger work)
git stash branch <BRANCH_NAME> stash@{0}
/*** BLAME ***/
# show who changed each line in a file
git blame <FILE_NAME>

Comments are disabled for this gist.