Git Rules
Commit Reference Notation
| Notation | Meaning | Example |
|---|---|---|
HEAD |
The current commit (tip of the checked-out branch) | git show HEAD |
HEAD~1 / HEAD~ |
1st ancestor following the first parent chain | git diff HEAD~1 HEAD |
HEAD~N |
Nth ancestor following the first parent chain | git diff HEAD~3 HEAD |
HEAD^1 / HEAD^ |
1st parent of HEAD (identical to HEAD~1 for linear history) |
git show HEAD^ |
HEAD^2 |
2nd parent of HEAD — only exists on merge commits | git show HEAD^2 |
HEAD^^ |
Grandparent via first-parent chain (same as HEAD~2) |
git show HEAD^^ |
<ref>^{commit} |
Dereference a tag or ref to its underlying commit object | git cat-file -e v1.0^{commit} |
~ vs ^ — when they differ
~N always follows the first parent N times (useful for linear history and rebase workflows). ^N selects the Nth parent of a single commit (only relevant for merge commits, where ^1 is the branch you merged into and ^2 is the branch you merged from).
# On a merge commit M with two parents A and B:
git show M^1 # → A (the branch that was merged into)
git show M^2 # → B (the branch that was merged from)
git show M~1 # → A (same as ^1, first-parent)Git command
Basic Commands
| Category | Command Example | Description |
|---|---|---|
| Check Git Version | git --version |
E.g. git version 2.43.0 |
| Clone withou files | git clone --no-checkout <repository-url> |
clones the repository without checking out the files |
| Status | git status -sb |
Short and branch-aware status (great for quick checks) |
| Rename | git mv <oldfilename> <newfilename> |
Rename or move a file and stage the change for commit |
| Remove | git rm --cached <FILE_NAME> |
Stop tracking a file in Git without deleting it from local |
| Log | git log --pretty=format:'%Cgreen%cI%Creset,%C(yellow)%h%Creset,%C(bold blue)%an%Creset,%s' |
Display commit log with ISO date, short commit ID, author, and commit message |
Staging Commands
| Category | Command Example | Description |
|---|---|---|
| Add all updates | git add -u |
Stage modified and deleted tracked files (exclude new untracked files) |
Add updates except .qmd |
git add -u -- . ':!*.qmd' |
Stage modified and deleted tracked files while excluding .qmd files |
git add -u -- . ':(exclude)*.qmd' |
Same as above using explicit pathspec exclusion syntax | |
Preview updates except .qmd |
git add -u -n -- . ':!*.qmd' |
Dry-run to preview which tracked file updates would be staged, excluding .qmd files |
File Modification Commands
| Category | Command Example | Description |
|---|---|---|
| Find file first commit | git log --diff-filter=A -- file.py |
Find the commit in which a file was first added to the repository |
| Unstage files | git rm --cached -r . |
Remove files from the Git index while keeping them in the working tree |
| Discard file changes | git restore <file-path> |
Discard local changes and restore the file to the current HEAD state |
git rm --cached -r . (before the initial commit)
This command is used when you are in a repository that has no commits yet (i.e., HEAD does not exist), and you have accidentally staged all files using:
git add .In this situation, normal commands like:
git restore --staged .
git reset HEAD .But these commands will fail because there is no HEAD in an initial repository state.
commands for branch
| Category | Command Example | Description |
|---|---|---|
| Create Branch | git switch -c <branch-name> |
Create and checkout a new branch locally |
| Checkout Previous Branch | git switch - |
Switch back to the previously checked-out branch |
| Pull From All Remotes | git pull --all |
Update all local tracking branches from all configured remotes |
| Delete Local Branch | git branch -d <branch-name> |
Delete a local branch safely (only if already merged) |
| Force Delete Local Branch | git branch -D <branch-name> |
Force delete a local branch even if unmerged |
| Delete Remote Branch | git push origin --delete <branch-name> |
Delete a branch from the remote repository |
commands for commits
| Category | Command Example | Description |
|---|---|---|
| Amend commit | git commit --amend |
Modify the most recent commit (message, content, or both) with editor starting up |
| Commit all changes with message | git commit -am "<commit message>" |
Stage all tracked changes and commit with a message in one step |
| Interactive rebase | git rebase -i <base> |
Rewrite commit history interactively — squash, reorder, edit, or drop commits since <base> (e.g. HEAD~3 or a branch name) |
Squash combines multiple commits into one. During git rebase -i, marking a commit as squash (or s) folds it into the commit above it, letting you rewrite a single combined message. This keeps history clean by collapsing WIP commits before merging.
pick a1b2c3 Add feature skeleton
squash d4e5f6 Fix typo
squash 7g8h9i Add tests
# → becomes one commit with a merged message
# a1b2c3 is the oldest commit
diff check
| Category | Command Example | Description |
|---|---|---|
| check staged diff | git diff --cached <branch> |
Compare staged changes in the index against the specified branch |
| check diff between branches | git diff <base-branch>..<target-branch> |
Compare changes between two branches |
Custom Git Commands
| Category | Command Example | Description |
|---|---|---|
| Convert a specific issue to PR | git issue2pr |
Create a Pull Request from the current branch and link it to a specific GitHub Issue. |
Config command
| Category | Command Example | Description |
|---|---|---|
| Default branch name | git config --global init.defaultBranch <default branch name> |
Set default branch when initializing new repos |
| Enable color output | git config --global color.ui auto |
Automatically color Git output in the terminal. |
GitHub CLI command
general
| Category | Command Example | Description |
|---|---|---|
| Check Organization | gh org list |
Show a list of organizations you belong to |
| Check Organization Repo | gh repo list <ORG> |
Show a list of repositories under a specific organization you belong to |
| Create Repo | gh repo create <REPONAME> --public --source=. --remote=origin |
Create a public repository under your GitHub account |
gh repo create <REPONAME> --public --source=. --remote=origin --org <ORG_NAME> |
Create a public repository under the specified organization | |
gh repo create <REPONAME> --private --source=. --remote=origin |
Create a private repository under your GitHub account | |
| Delete Repo | gh repo delete <OWNER>/<REPONAME> |
Delete the target repository from GitHub while keeping the local repository intact |
| Create Pull Request | gh pr create --base <BASE_BRANCH> --head <HEAD_BRANCH> --title "<TITLE>" --body "<BODY>" |
Create a pull request from the specified source branch to the target branch |
Gist
Even if a Gist is set to Private, it can still be accessed by anyone with the link. Do not store sensitive information such as passwords, API keys, or personal data in a Gist. For secure storage of secrets, consider using a proper secrets manager.
| Category | Command Example | Description |
|---|---|---|
| Create Secret Gist from File | gh gist create <filename> -d "<gist title>" |
Create a secret gist with the given file and description |
| Create Gist from standard output | cat cool.txt | gh gist create -d "<gist title>" |
Create a gist directly from command output or piped content |