超全 Git 命令速查手册
一、配置类
bash
git config --global user.name "姓名"
git config --global user.email "邮箱"
git config --global core.editor vim
git config --global color.ui true
git config --global --list
git config --local --list
git config --system --list
git config --global --unset user.name
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.unstage 'reset HEAD --'
二、初始化 / 克隆
bash
git init
git init --bare # 裸仓库
git clone <url>
git clone --depth=1 <url> # 浅克隆
git clone -b 分支名 <url> # 克隆指定分支
三、查看信息
bash
git status
git status -s
git log
git log --oneline
git log --graph
git log --stat
git log -p
git log --name-only
git log --author="xxx"
git log --since="2025-01-01"
git log --grep="关键字"
git reflog
git show HEAD
git show <commit-id>
git show <commit-id>:文件名
git blame 文件名 # 看谁改的每一行
git diff
git diff --staged
git diff HEAD
git diff --cached
git diff 分支1 分支2
git diff commit1 commit2
git diff --name-only
git diff --name-status
git diff --word-diff
git remote -v
git remote show origin
四、暂存与提交
bash
git add .
git add 文件名
git add *.java
git add -u # 只跟踪已修改、删除的文件
git add -A # 所有变更
git commit -m "msg"
git commit -am "msg" # add + commit
git commit --amend # 追加到上一次提交
git commit --amend --no-edit
git commit --allow-empty # 空提交
五、分支管理
bash
git branch
git branch -a
git branch -r
git branch 新分支
git branch -m 旧名 新名
git branch -M 强制重命名
git branch -d 分支
git branch -D 强制删除
git checkout 分支
git checkout -b 新分支
git checkout -b 本地分支 origin/远程分支
git switch 分支
git switch -c 新分支
git branch --set-upstream-to=origin/分支
git push -u origin 分支
git branch -vv
六、拉取、推送、同步
bash
git fetch
git fetch origin
git fetch --prune # 清理无效远程分支
git pull
git pull --rebase
git pull --no-rebase
git pull origin 分支
git push
git push origin 分支
git push -f # 强制推送
git push --force-with-lease # 安全强制
git push origin --delete 远程分支
git push origin --tags
七、合并与变基
bash
git merge 分支
git merge --no-ff 分支
git merge --abort
git rebase 分支
git rebase --continue
git rebase --abort
git rebase --skip
git cherry-pick <commit-id>
git cherry-pick -m 1 <commit-id>
git cherry-pick --abort
八、暂存(stash)
bash
git stash
git stash save "备注"
git stash list
git stash show
git stash show -p
git stash apply
git stash apply stash@{1}
git stash pop
git stash drop
git stash drop stash@{0}
git stash clear
git stash branch 新分支 stash@{0}
九、撤销、回退、重置
bash
git restore 文件名
git restore --staged 文件名
git checkout -- 文件名
git reset HEAD 文件名
git reset HEAD^
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
git reset --hard <commit-id>
git revert <commit-id>
git revert -m 1 <merge-commit-id>
git clean -fd # 删除未跟踪文件
git clean -nfd # 预览删除
十、标签 tag
bash
git tag
git tag v1.0
git tag -a v1.0 -m "msg"
git show v1.0
git tag -d v1.0
git push origin v1.0
git push origin --tags
git push origin --delete tag v1.0
git checkout -b 分支 v1.0
十一、远程仓库管理
bash
git remote add origin <url>
git remote rename origin old-origin
git remote set-url origin <url>
git remote remove origin
git remote prune origin
git ls-remote
十二、子模块 submodule
bash
git submodule add <url> 路径
git submodule init
git submodule update
git submodule update --init --recursive
git submodule foreach git pull
十三、归档 / 导出
bash
git archive master --format=zip -o latest.zip
git archive HEAD --prefix=项目/ -o project.tar.gz
十四、查找与定位
bash
git grep "关键字"
git grep -n "关键字"
git grep -i "关键字"
git fsck # 检查仓库完整性
git gc # 垃圾回收
git prune
十五、冲突处理
bash
git merge --abort
git rebase --abort
git cherry-pick --abort
git diff --ours
git diff --theirs
git checkout --ours 文件名
git checkout --theirs 文件名
git add . && git merge --continue
十六、历史修改(高级)
bash
git rebase -i HEAD~3 # 合并/编辑提交
git filter-branch --tree-filter ... # 全局修改历史
十七、.gitignore 相关
bash
cat .gitignore
echo "*.log" >> .gitignore
echo "/target/" >> .gitignore
git rm --cached 文件名
git ls-files --ignored --exclude-standard
Git 高频 20 条
(你日常开发 99% 场景都够用,直接收藏)
一、必背高频 20 条(最简版)
bash
1. git status # 看状态
2. git add . # 全添加
3. git commit -m "备注" # 提交
4. git pull --rebase # 拉代码(推荐)
5. git push # 推代码
6. git log --oneline # 简洁日志
7. git reflog # 找回丢失提交
8. git branch # 看分支
9. git checkout 分支 # 切分支
10. git checkout -b 分支 # 新建+切分支
11. git branch -M 旧名 新名 # 重命名分支
12. git remote -v # 看远程地址
13. git remote set-url origin SSH地址 # 改远程
14. git diff # 对比修改
15. git stash # 暂存修改
16. git stash pop # 恢复暂存
17. git merge 分支 # 合并分支
18. git cherry-pick 提交ID # 只合并某条提交
19. git restore 文件 # 丢弃本地修改
20. git reset --hard 提交ID # 强制回退
二、最常见报错 + 10 秒解决
1. 权限问题:Permissions 0777 for 'id_rsa' are too open
原因 :私钥权限太松,SSH 拒绝使用
解决
bash
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
2. SSH 失败:Host key verification failed
原因 :未信任服务器指纹
解决
bash
ssh-keyscan code.tiancloud.com >> ~/.ssh/known_hosts
3. SSH 问密码:git@xxx password:
原因 :密钥没配置 / 没加到 GitLab / 用户不对
解决
-
公钥粘贴到 GitLab SSH Keys
-
root 下用:
bashsudo cp -r ~/.ssh /root/ sudo chown -R root:root /root/.ssh
4. 冲突:Automatic merge failed; fix conflicts
解决
- 打开文件手动删
<<<<<<标记
bash
git add .
git rebase --continue
# 或 merge 场景:git merge --continue
5. detached HEAD (游离 HEAD)
原因 :切到了历史提交,不在分支上
解决
bash
git checkout master
# 想保留修改:git checkout -b 新分支
6. pull 失败:error: Your local changes to the following files would be overwritten by merge
解决
bash
git stash
git pull --rebase
git stash pop
7. push 失败:error: failed to push some refs
原因 :远程比你新
解决
bash
git pull --rebase
git push
8. 想放弃所有本地修改,直接和远程一致
bash
git reset --hard HEAD
git checkout .
git clean -fd
9. .gitignore 不生效
原因 :文件已被跟踪
解决
bash
git rm --cached 文件名
git add .gitignore
git commit -m "fix ignore"
10. 提交错了,想撤销上一次 commit(代码保留)
bash
git reset --soft HEAD^
11. 彻底回退到某个版本(代码不要了)
bash
git reset --hard 提交ID
git push -f
12. 报错:fatal: not a git repository
原因 :不在项目目录
解决
bash
cd 到项目文件夹
# 或 git init
三、你这套 root + SSH 专用排错命令(万能)
bash
# 权限修复
sudo chmod 700 /root/.ssh
sudo chmod 600 /root/.ssh/id_ed25519
sudo chmod 600 /root/.ssh/config
# 免验主机
sudo bash -c 'echo "StrictHostKeyChecking no" > /root/.ssh/config'
# 测试SSH
sudo ssh -T git@code.tiancloud.com
一键修复脚本:git_fix.sh
适配:
root 权限 + SSH 拉代码 + 之前遇到的所有坑(权限、指纹、计数器、.log 忽略、分支 master)
bash
#!/bin/bash
# ==================== 项目专属信息 ====================
REPO_URL="git@code.tiancloud.com:bas-data/cmi_ah_settle.git"
BRANCH="master"
PROJECT_DIR="/root/cmi_ah_settle"
# ==================== 1. SSH 权限修复 ====================
echo "===== 修复 SSH 权限 ====="
mkdir -p /root/.ssh
chmod 700 /root/.ssh
if [ -f /root/.ssh/id_ed25519 ]; then
chmod 600 /root/.ssh/id_ed25519
fi
# 免主机验证
cat > /root/.ssh/config <<EOF
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
EOF
chmod 600 /root/.ssh/config
# 加入服务器指纹
ssh-keyscan code.tiancloud.com > /root/.ssh/known_hosts
chmod 600 /root/.ssh/known_hosts
# ==================== 2. 进入项目目录 ====================
echo "===== 进入项目目录 ====="
mkdir -p "$PROJECT_DIR"
cd "$PROJECT_DIR" || exit 1
# ==================== 3. Git 初始化/关联 ====================
echo "===== 初始化 Git 仓库 ====="
if [ ! -d .git ]; then
git init
git remote add origin "$REPO_URL"
fi
# 强制使用 SSH 地址
git remote set-url origin "$REPO_URL"
# ==================== 4. 配置 .gitignore 忽略日志 ====================
echo "===== 配置 .gitignore ====="
cat > .gitignore <<EOF
# 日志文件
*.log
*.tmp
*.temp
*.bak
.DS_Store
EOF
# 清理已跟踪的 log
git rm --cached *.log 2>/dev/null
# ==================== 5. 拉取最新代码 ====================
echo "===== 拉取 $BRANCH 代码 ====="
git fetch origin
git checkout "$BRANCH"
git pull --rebase origin "$BRANCH"
# ==================== 完成 ====================
echo -e "\n===== ✅ 项目修复完成 ====="
echo "项目目录:$PROJECT_DIR"
echo "当前分支:$(git branch --show-current)"
echo "远程地址:$(git remote get-url origin)"
使用方法
bash
# 1. 创建脚本
vi git_fix.sh
# 2. 粘贴上面全部内容,保存退出
# 3. 加执行权限
chmod +x git_fix.sh
# 4. 运行
./git_fix.sh
问题整理
- SSH 权限 600/700
- 免指纹验证,不再弹
yes/no - 自动配置
.gitignore忽略*.log - 自动切到
master分支 - 自动拉最新代码
- 自动修复远程地址
- 再也不会出现:权限不够、Host key 失败、SSH 要密码、日志被提交等问题