Git 命令速查表
Git Commands Cheat Sheet
Git 常用命令速查表,包括仓库初始化、提交、分支管理、远程操作等常用命令,以及常见问题的解决方案。
一、常用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| git init
git config --global user.name <your-name> git config --global user.email <your-email> git config --global init.defaultBranch main
git clone <repository-url>
git add <file>
git add
git diff
git commit -m "Message" git commit --amend --no-edit
git reset
git status
git rm <file>
git log
git log --graph
git show <commit-hash>
git branch -a
git branch <branch-name>
git branch -m <new-branch-name>
git branch -d <branch-name>
git checkout <branch-name>
git checkout -b <branch-name>
git merge <branch-name>
git remote add <name> <repository-url>
git push <remote> <branch>
git pull <remote>
git gc
git stash
git stash apply
git fetch --all --prune
|
二、使用技巧

三、常见问题
3.1 修复 SSH 连接超时问题
try to create or update ~/.ssh/config as followed:
1 2 3
| Host github.com Hostname ssh.github.com Port 443
|
Then, run the command ssh -T git@github.com to confirm if the issue is fixed.
3.2 无法获取所有远程分支
try to open your git config by git config -e (--global), and then modify relevant configuration:
1 2 3
| [remote "origin"] url = https://git.example.com/example.git (you can omit this URL) fetch = +refs/heads/*:refs/remotes/origin/*
|
after that, you can run git fetch --all.
3.3 生成或应用补丁文件
1 2 3 4 5 6 7 8 9
| git diff > changes.patch
git diff --cached > changes.patch
git diff commit1 commit2 > changes.patch
git apply changes.patch
|