Essential Git Commands Cheat Sheet for Version Control
This guide provides a comprehensive collection of Git commands covering diff, branch management, file handling, remote repository operations, stash usage, configuration, and advanced techniques, enabling developers to efficiently compare changes, manage branches, clean workspaces, and interact with remote repositories.
1. Git diff operations
Show differences between the working directory and the index (staging area) git diff Show differences between the index and the latest commit (HEAD) git diff --cached Show differences among working directory, index, and the latest commit git diff HEAD Show file changes between any two commits
git diff <commit-id-1> <commit-id-2>2. Git branch management
List local branches with upstream tracking information git branch -vv List all remote branches git branch -r List both local and remote branches git branch -a Show the relationship between a local branch and its remote counterpart git remote show origin Delete a local branch git branch -d <local-branch-name> Create and switch to a new local branch git checkout -b <branch-name> Rename a local branch git branch -m <new-branch-name> Switch back to the previous branch git checkout - Cherry‑pick a commit onto another branch
git checkout <target-branch> && git cherry-pick <commit-id>Delete a remote branch
git push origin --delete <remote-branch-name>Prune stale remote‑tracking branches git remote prune origin Set upstream for a local branch
git branch -u origin/<branch-name>3. Git file handling
List all tracked files (including their status flags) git ls-files -t List all untracked files git ls-files --others Show ignored files
git status --ignored
git ls-files --others -i --exclude-standardForce‑delete a specific untracked file git clean -f <file-name> Force‑delete all untracked files and directories git clean -df Remove only files ignored by .gitignore git clean -X -f Recover a deleted file
# Find the commit that removed the file
git rev-list -n 1 HEAD -- <path/to/file>
# Checkout the file from the commit before the deletion
git checkout <deleting-commit>^ -- <path/to/file>4. Git remote repository operations
List all configured remotes git remote Change the URL of an existing remote git remote set-url origin <new-url> Add a new remote
git remote add origin <remote-url>5. Git stash operations
Save current changes without committing git stash Include untracked files in the stash git stash -u List all stash entries git stash list Apply a specific stash without dropping it git stash apply <stash@{n}> Apply the latest stash and drop it git stash pop Delete all stash entries git stash clear Extract a single file from a stash
git checkout <stash@{n}> -- <file-path>6. Git proxy configuration
Configure SSH proxy (e.g., via Shadowsocks SOCKS5)
# ~/.ssh/config
Host gitlab.com
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p
Host github.com
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %pConfigure global HTTP and SOCKS proxies
git config --global socks.proxy '127.0.0.1:1080'
git config --global http.proxy 'http://127.0.0.1:8001'
git config --global https.proxy 'http://127.0.0.1:8001'7. Advanced Git operations
Create a bundle from a branch (useful for offline transfer)
git bundle create <bundle-file> <branch-name>Clone a bundle as a new repository and check out a specific branch
git clone <bundle-file> <repo-dir> -b <branch-name>Amend the most recent commit (e.g., to edit the commit message or add staged changes) git commit --amend Show line‑by‑line attribution for a file git blame <file-name> Reset to a specific commit
Mixed (default) – resets HEAD and index, leaves working tree untouched: git reset <commit-id> Soft – moves HEAD only, keeping index and working tree as‑is: git reset --soft <commit-id> Hard – resets HEAD, index, and working tree to the given commit: git reset --hard <commit-id> Force local repository to match a remote branch
git fetch --all && git reset --hard origin/masterDelete the entire commit history (useful for starting a fresh history) git update-ref -d HEAD Find commits that deleted a specific file
git log --diff-filter=D --summary | grep delete
# For a precise path use:
git log --diff-filter=D -- <path/to/file>8. GitHub RSS feed URLs
Repository releases: https://github.com/:owner/:repo/releases.atom Repository commits: https://github.com/:owner/:repo/commits.atom Private feed (requires a personal token): https://github.com/:user.private.atom?token=:secret Repository tags: https://github.com/:user/:repo/tags.atom User activity:
https://github.com/:user.atomSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
