Last active
March 5, 2026 23:19
-
-
Save aleksbelic/e2c44514232fdf9b6de823cec61c3ac3 to your computer and use it in GitHub Desktop.
Git commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # initialize a new Git repository in the current directory | |
| git init | |
| # show working tree status (staged, unstaged, untracked files) | |
| git status | |
| /*** CLONE ***/ | |
| # download (clone) the remote git repository | |
| git clone <REPO_URL> | |
| /*** STAGING ***/ | |
| # stage a specific file | |
| git add <FILE_NAME> | |
| # stage all changes (including deletions) | |
| git add -A | |
| # stage changes in the current directory and below | |
| git add . | |
| /*** DIFF ***/ | |
| # show all unstaged changes in the repo | |
| git diff | |
| # 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 (skips staging area) | |
| # 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>" | |
| /*** LOG ***/ | |
| # show commit history | |
| git log | |
| # show concise commit history | |
| git log --oneline | |
| # show commit history with branch graph | |
| 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 "r<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> | |
| /*** BRANCH ***/ | |
| # list local branches | |
| git branch | |
| # show current branch | |
| git branch --show-current | |
| # list local and remote branches | |
| git branch -a | |
| # list remote branches only | |
| git branch -r | |
| # 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 | |
| git branch -d <BRANCH_NAME> | |
| # force delete a local branch | |
| 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 ***/ | |
| # delete a remote branch | |
| git push <REMOTE_NAME> --delete <BRANCH_NAME> |
Comments are disabled for this gist.