git
Git Command Cheat Sheet
| Category | Command | Purpose | Example |
|---|---|---|---|
| Repository Setup | sudo git init --bare /opt/games.git |
To create the bare repository | |
git init |
Initialize a new Git repository | git init |
|
git clone <repository_url> |
Clone an entire repository | git clone https://github.com/user/repo.git |
|
git clone --branch <branch> --single-branch <repository_url> |
Clone only a specific branch | git clone --branch develop --single-branch https://github.com/user/repo.git |
|
| Configuration | git config --global user.name "Your Name" |
Set global username | git config --global user.name "John Doe" |
git config --global user.email "[email protected]" |
Set global email | git config --global user.email "[email protected]" |
|
git config --global --list |
View global Git configuration | git config --global --list |
|
git config --list --show-origin |
Show configuration and source file | git config --list --show-origin |
|
git config user.name "Project User" |
Set repository-specific username | git config user.name "Project User" |
|
git config user.email "[email protected]" |
Set repository-specific email | git config user.email "[email protected]" |
|
| Repository Status | git status |
Show working tree and staging status | git status |
| Staging | git add <file> |
Stage a specific file | git add app.py |
git add . |
Stage all changes | git add . |
|
| Unstaging | git reset HEAD <file> |
Unstage a specific file | git reset HEAD app.py |
git restore --staged <file> |
Unstage a specific file (recommended) | git restore --staged app.py |
|
git reset HEAD |
Unstage all files | git reset HEAD |
|
git restore --staged . |
Unstage all files | git restore --staged . |
|
| Commit | git commit -m "message" |
Commit staged changes | git commit -m "Initial commit" |
git commit --amend |
Modify the latest commit | git commit --amend |
|
git commit --amend --no-edit |
Amend commit without changing message | git commit --amend --no-edit |
|
git commit --amend -C HEAD |
Reuse previous commit message | git commit --amend -C HEAD |
|
| Uncommit / Reset | git reset --soft HEAD~1 |
Remove latest commit and move changes to staging area | git reset --soft HEAD~1 |
git reset --mixed HEAD~1 |
Remove latest commit and move changes to working directory (unstaged) | git reset --mixed HEAD~1 |
|
git reset --hard HEAD~1 |
Remove latest commit and discard all changes. Refer section [To update the remote repository to match your local hard reset] if needed | git reset --hard HEAD~1 |
|
git rebase -i HEAD~2 |
Delete or edit older commits interactively | git rebase -i HEAD~2 |
|
git update-ref -d HEAD |
Delete the only commit in repository | git update-ref -d HEAD |
|
git commit --allow-empty -m "Reset history" |
Create a new empty commit | git commit --allow-empty -m "Reset history" |
|
| To update the remote repository to match your local hard reset | git push origin <branch-name> --force-with-lease |
Push with Force-With-Lease. --force-with-lease. This stops the push if someone else has pushed new commits to the remote branch in the meantime, preventing you from accidentally destroying their work. | git push origin <branch-name> --force-with-lease |
git push origin <branch-name> -f |
Push with Standard Force | git push origin <branch-name> -f |
|
| History | git log |
View commit history | git log |
git log --grep="text" |
Search commit messages | git log --grep="fix" |
|
git log --author="Author" |
Search commits by author | git log --author="Alice" |
|
git log -- <file> |
Show history of a specific file | git log -- README.md |
|
git log --graph --oneline --decorate --all |
Show commit graph | git log --graph --oneline --decorate --all |
|
git show <commit> |
Show commit details | git show a1b2c3d |
|
git show --name-only <commit> |
Show files modified in a commit | git show --name-only a1b2c3d |
|
git diff <commit1> <commit2> |
Compare two commits | git diff HEAD^ HEAD |
|
git show :<file> |
View staged version of a file | git show :README.md |
|
git show <commit-hash>:<path/to/file.ext> |
To see a file's content in a specific commit without switching branches | ||
git show branch-name:<path to file> |
View a file from a different branch | ||
| Clean Working Tree | git clean -n |
Preview files that will be removed | git clean -n |
git clean -f |
Remove untracked files | git clean -f |
|
git clean -fd |
Remove untracked files and directories | git clean -fd |
|
git clean -X |
Remove ignored files only | git clean -X |
|
git clean -Xd |
Remove ignored files and directories | git clean -Xd |
|
git clean -x |
Remove all untracked files (including ignored) | git clean -x |
|
git clean -xd |
Remove all untracked files/directories | git clean -xd |
|
git check-ignore -v <file> |
Verify why a file is ignored | git check-ignore -v app.log |
|
| Branch Management | git switch --orphan new-branch |
Create an orphan branch | |
git branch <branch> |
Create a branch | git branch feature |
|
git checkout -b <branch> |
Create and switch to a branch | git checkout -b feature |
|
git switch <branch> |
Switch branches | git switch develop |
|
git checkout <branch> |
Switch branches (older command) | git checkout develop |
|
git branch -a |
List all branches | git branch -a |
|
git branch -v |
Shows local branches with the latest commit (SHA + commit message). | ||
git branch -vv |
Shows everything from -v plus the upstream tracking branch and ahead/behind status. | ||
git branch -m <old> <new> |
Rename a branch | git branch -m dev develop |
|
git branch -d <branch> |
Delete local branch | git branch -d feature |
|
git push origin --delete <branch> |
Delete remote branch | git push origin --delete feature |
|
| Merge & Rebase | git merge <branch> |
Merge another branch | git merge develop |
git rebase <branch> |
Reapply commits on another branch | git rebase main |
|
git rebase --onto <newbase> <upstream> <branch> |
Advanced rebase | git rebase --onto dev B main |
|
| Stash | |||
| Saving Changes | git stash |
Stashes modified and staged tracked files. | |
git stash push -m "message" |
Recommended. Stashes changes with a description. | ||
git stash -u |
Stashes tracked and untracked files. | ||
git stash -a |
Stashes all files, including ignored ones. | ||
git stash -p |
Interactively choose code hunks to stash. | ||
| Viewing Stashes | git stash list |
Lists all saved stashes with indexes. | |
git stash show |
Summarizes changes in the latest stash. | ||
git stash show stash@{N} |
Summarizes changes for specific stash index N. |
||
git show stash@{N}:<filepath> |
Show contents of specific file present in specific stash | ||
git stash show -p |
Shows full diff of latest stash. | ||
| Applying Changes | git stash pop |
Applies latest stash and deletes it. | |
git stash pop stash@{N} |
Applies specific stash N and deletes it. |
||
git stash apply |
Applies latest stash but keeps it. | ||
git stash apply stash@{N} |
Applies specific stash N but keeps it. |
||
git stash apply --index |
Reinstates original staged/unstaged file statuses. | ||
| Deleting Stashes | git stash drop |
Deletes the most recent stash. | |
git stash drop stash@{N} |
Deletes specific stash index N. |
||
git stash clear |
Deletes all stashes. Cannot be undone. | ||
| Advanced Workflows | git stash branch <name> |
Creates branch and pops latest stash. | |
git checkout stash@{N} -- <file> |
Restores a single file from a stash. | ||
| Remote Repository | git remote add <name> <url> |
Add a remote | git remote add origin <url> |
git remote -v |
List remotes | git remote -v |
|
git remote set-url <name> <url> |
Change remote URL | git remote set-url origin <url> |
|
git remote remove <name> |
Remove remote | git remote remove origin |
|
| Fetch / Pull | git fetch |
Download remote changes | git fetch |
git pull |
Fetch and merge | git pull |
|
git pull --rebase |
Fetch and rebase | git pull --rebase |
|
| Push | git push |
push the current selected branch | |
git push origin <branch> |
Push branch | git push origin main |
|
git push --all origin |
Push all branches | git push --all origin |
|
git push origin --tags |
Push all tags | git push origin --tags |
|
git push -u origin <branch> |
Push and set upstream | git push -u origin main |
|
git push --force |
Force push | git push --force |
|
git push --dry-run |
Preview push | git push --dry-run |
|
| Cherry Pick | git cherry-pick <commit> |
Apply a specific commit | git cherry-pick a1b2c3d |
git cherry-pick -n <commit> |
Cherry-pick without committing | git cherry-pick -n a1b2c3d |
|
| Tags | git tag |
List tags | git tag |
git tag -a <tag> -m "message" |
Create annotated tag | git tag -a v1.0.0 -m "Release" |
|
git show <tag> |
View tag details | git show v1.0.0 |
|
git tag -d <tag> |
Delete local tag | git tag -d v1.0.0 |
|
git push origin <tag> |
Push a tag | git push origin v1.0.0 |
|
git push origin --tags |
Push all tags | git push origin --tags |
|
git fetch --tags |
Fetch tags | git fetch --tags |
|
git switch -c <branch> <tag> |
Create branch from tag | git switch -c release/v1.0 v1.0.0 |
|
| Repository Inspection | git ls-tree <ref> --name-only |
View contents of a branch or tree | git ls-tree origin/dev --name-only |
| Tracked Files | git rm --cached <file> |
Remove a tracked file without deleting it locally | git rm --cached app.log |
| Gerrit | git push origin HEAD:refs/for/master |
Push changes for Gerrit code review | git push origin HEAD:refs/for/master |
Notes:
1) A bare repository is a shared storage folder that acts exactly like your own private GitHub, allowing multiple people to clone and push code
Sceanrios
1) Create a post-update hook in the bare repository to automatically create a release tag when changes are pushed to the master branch
# Go to the bare repository
cd /path/to/repo.git
# Verify it is a bare repository (should return 'true')
git rev-parse --is-bare-repository
# Create/Edit the post-update hook
cd hooks
vi post-update
Add the following content in post-update file
#!/bin/sh
TAG="release-$(date +%F)"
# Create the tag only if it doesn't already exist
if git show-ref --verify --quiet refs/heads/master &&
! git show-ref --verify --quiet "refs/tags/$TAG"; then
git tag "$TAG" refs/heads/master
fi
# Make the hook executable
chmod +x post-update
# From your local repository, push changes to master
git push origin master
# Verify the tag on the remote (bare repository)
cd /path/to/repo.git
git tag
# Fetch the newly created tag to your local repository
git fetch --tags
# Verify the tag locally
git tag