工具 & 效率8 分钟阅读
Git 常用命令 100 条
本文整理 Git 日常开发、分支、合并、远程协作、排错、恢复、历史分析和仓库维护常用的 100 条命令。示例中的分支名、提交和远程地址均需替换。
2026年8月11日阅读—点赞—收藏—
dba100git墨力计划
100 条命令系列文章专栏
本文整理 Git 日常开发、分支、合并、远程协作、排错、恢复、历史分析和仓库维护常用的 100 条命令。示例中的分支名、提交和远程地址均需替换。
本文整理 Git 日常开发、分支、合并、远程协作、排错、恢复、历史分析和仓库维护常用的 100 条命令。示例中的分支名、提交和远程地址均需替换。
reset --hard、强制推送、清理未跟踪文件和历史重写可能丢失工作,执行前应查看状态、创建备份分支,并优先使用 --force-with-lease。
1git --version1git config --global user.name 'Your Name'1git config --global user.email 'you@example.com'1git config --list --show-origin1git config --global init.defaultBranch main1git init1git clone https://example.com/team/repo.git1git clone --depth 1 https://example.com/team/repo.git1git clone --branch release --single-branch https://example.com/team/repo.git1git help commit1git status1git status --short --branch1git add app.go1git add -A1git add -p1git diff1git diff --cached1git commit -m 'feat: add health endpoint'1git commit --amend1git show --stat1git branch1git branch -a1git branch feature/login1git switch -c feature/login1git switch main1git switch -1git branch -m feature/auth1git branch -d feature/auth1git branch --merged1git branch -vv1git remote -v1git remote add origin https://example.com/team/repo.git1git remote show origin1git fetch origin1git fetch --all --prune1git pull --rebase1git push -u origin feature/login1git push1git push --force-with-lease1git push origin --delete feature/login1git merge feature/login1git merge --no-ff feature/login1git merge --abort1git rebase main1git rebase -i HEAD~51git rebase --continue1git rebase --skip1git rebase --abort1git cherry-pick COMMIT1git cherry-pick --abort1git restore app.go1git restore --staged app.go1git revert COMMIT1git reset --soft HEAD~11git reset --mixed HEAD~11git clean -nd1git clean -fd1git reflog1git branch recovery HEAD@{2}1git fsck --lost-found1git stash push -m 'wip login'1git stash push -u -m 'wip'1git stash list1git stash show -p stash@{0}1git stash apply stash@{0}1git stash pop1git stash branch recover-work stash@{0}1git tag -a v1.2.3 -m 'release v1.2.3'1git push origin v1.2.31git tag -v v1.2.31git log --graph --decorate --oneline --all1git log --follow -- app.go1git log --author='Alice' --oneline1git log --grep='fix' --oneline1git log -S 'oldFunction' --oneline1git log -G 'timeout\\s*=' --oneline1git blame -L 20,40 app.go1git diff main...feature/login1git log --left-right --cherry-pick --oneline main...feature/login1git bisect start1git bisect bad1git bisect good v1.2.01git bisect run ./test.sh1git bisect reset1git submodule add https://example.com/team/lib.git libs/lib1git submodule update --init --recursive1git submodule update --remote --merge1git worktree add ../repo-hotfix -b hotfix main1git worktree list1git worktree remove ../repo-hotfix1git fsck --full1git maintenance run1git count-objects -vH1git gc1git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | sort -k3nr | head1git archive --format=tar.gz -o release.tar.gz HEAD1git bundle create repo.bundle --all1git bundle verify repo.bundle1git check-ignore -v path/to/file1git status --short --branch && git diff --check && git diff --cachedGit 的恢复能力很强,但前提是对象尚未被清理。历史重写前创建备份分支,团队分支优先用 revert,强推使用 --force-with-lease,并在提交前检查暂存差异和空白错误。