|
| 1 | +# Git 命令 |
| 2 | + |
| 3 | +## 设置本地仓库 |
| 4 | + |
| 5 | +| 命令 | 说明 | |
| 6 | +|-------------------|-------| |
| 7 | +| `git init` | 初始化仓库 | |
| 8 | +| `git clone <url>` | 克隆仓库 | |
| 9 | + |
| 10 | +## 配置 Git |
| 11 | + |
| 12 | +| 命令 | 说明 | |
| 13 | +|--------------------------------------------|----------------| |
| 14 | +| `git config --global user.name <name>` | 设置用户名 | |
| 15 | +| `git config --global user.email <email>` | 设置用户邮箱 | |
| 16 | +| `git config --global color.ui true` | 设置 Git 命令行颜色显示 | |
| 17 | +| `git config --global core.editor <editor>` | 设置默认文本编辑器 | |
| 18 | +| `git config --list` | 查看配置信息 | |
| 19 | + |
| 20 | +## 基本快照 |
| 21 | + |
| 22 | +| 命令 | 说明 | |
| 23 | +|-----------------------------|----------------| |
| 24 | +| `git add <file>` | 添加文件到暂存区 | |
| 25 | +| `git add .` | 添加所有文件到暂存区 | |
| 26 | +| `git commit -m "<message>"` | 提交暂存区文件并添加提交信息 | |
| 27 | +| `git status` | 查看仓库状态 | |
| 28 | +| `git diff` | 查看文件差异 | |
| 29 | +| `git diff --staged` | 查看暂存区文件差异 | |
| 30 | +| `git log` | 查看提交历史 | |
| 31 | +| `git rm <file>` | 删除文件 | |
| 32 | + |
| 33 | +## 分支和合并 |
| 34 | + |
| 35 | +| 命令 | 说明 | |
| 36 | +|-----------------------------|---------| |
| 37 | +| `git branch` | 查看分支列表 | |
| 38 | +| `git branch <new-branch>` | 创建分支 | |
| 39 | +| `git checkout <branch>` | 切换分支 | |
| 40 | +| `git checkout -b <branch>` | 创建并切换分支 | |
| 41 | +| `git merge <branch>` | 合并分支 | |
| 42 | +| `git branch -d <branch>` | 删除分支 | |
| 43 | +| `git branch -D <branch>` | 强制删除分支 | |
| 44 | +| `git branch -m <old> <new>` | 重命名分支 | |
| 45 | + |
| 46 | +## 撤销更改 |
| 47 | + |
| 48 | +| 命令 | 说明 | |
| 49 | +|-----------------------------|----------------| |
| 50 | +| `git reset --hard HEAD` | 撤销所有更改并重置到最新提交 | |
| 51 | +| `git checkout -- <file>` | 放弃对文件的修改 | |
| 52 | +| `git revert <commit>` | 撤销指定提交 | |
| 53 | +| `git reset <commit>` | 重置到指定提交 | |
| 54 | +| `git reset --hard <commit>` | 重置到指定提交并删除工作区 | |
| 55 | + |
| 56 | +## 远程仓库 |
| 57 | + |
| 58 | +| 命令 | 说明 | |
| 59 | +|---------------------------------------|------------------| |
| 60 | +| `git remote add <name> <url>` | 添加远程仓库 | |
| 61 | +| `git remote -v` | 查看远程仓库信息 | |
| 62 | +| `git fetch` | 拉取远程仓库更新 | |
| 63 | +| `git pull` | 拉取远程仓库更新并合并到当前分支 | |
| 64 | +| `git push origin <branch>` | 推送本地分支到远程仓库 | |
| 65 | +| `git push origin --delete <branch>` | 删除远程分支 | |
| 66 | +| `git remote remove <name>` | 删除远程仓库 | |
| 67 | +| `git remote set-url <name> <url>` | 修改远程仓库地址 | |
| 68 | +| `git remote update <name>` | 更新远程仓库 | |
| 69 | +| `git remote show <name>` | 查看远程仓库详细信息 | |
| 70 | +| `git remote prune <name>` | 删除远程仓库中已经不存在的分支 | |
| 71 | +| `git remote rename <old> <new>` | 重命名远程仓库 | |
| 72 | +| `git remote set-head <name> <branch>` | 设置远程仓库的默认分支 | |
| 73 | + |
| 74 | +## 隐藏更改 |
| 75 | + |
| 76 | +| 命令 | 说明 | |
| 77 | +|-------------------------------------|---------------| |
| 78 | +| `git stash` | 暂存当前工作区更改 | |
| 79 | +| `git stash list` | 查看暂存列表 | |
| 80 | +| `git stash apply <stash>` | 应用指定暂存更改 | |
| 81 | +| `git stash pop <stash>` | 应用并删除指定暂存更改 | |
| 82 | +| `git stash drop <stash>` | 删除指定暂存更改 | |
| 83 | +| `git stash clear` | 清空暂存列表 | |
| 84 | +| `git stash branch <branch> <stash>` | 从指定暂存创建新分支 | |
| 85 | +| `git stash show <stash>` | 查看指定暂存更改的详细信息 | |
| 86 | + |
| 87 | +## 标签 |
| 88 | + |
| 89 | +| 命令 | 说明 | |
| 90 | +|----------------------------------------|-------------| |
| 91 | +| `git tag` | 查看所有标签 | |
| 92 | +| `git tag <tagname>` | 创建标签 | |
| 93 | +| `git tag -a <tagname> -m "message"` | 创建带有说明的标签 | |
| 94 | +| `git tag -d <tagname>` | 删除标签 | |
| 95 | +| `git show <tagname>` | 查看标签详细信息 | |
| 96 | +| `git push origin <tagname>` | 推送标签到远程仓库 | |
| 97 | +| `git push origin --tags` | 推送所有标签到远程仓库 | |
| 98 | +| `git fetch --tags` | 拉取所有标签 | |
| 99 | +| `git checkout <tagname>` | 切换到指定标签 | |
| 100 | +| `git tag -d <tagname>` | 删除本地标签 | |
| 101 | +| `git push origin :refs/tags/<tagname>` | 删除远程标签 | |
| 102 | + |
| 103 | +## 查看差异 |
| 104 | + |
| 105 | +| 命令 | 说明 | |
| 106 | +|---------------------|-------------------| |
| 107 | +| `git diff` | 查看工作区和暂存区之间的差异 | |
| 108 | +| `git diff HEAD` | 查看工作区和最近一次提交之间的差异 | |
| 109 | +| `git diff <branch>` | 查看工作区和指定分支之间的差异 | |
| 110 | + |
| 111 | +## 检查和比较 |
| 112 | + |
| 113 | +| 命令 | 说明 | |
| 114 | +|----------------------------------------------|------------------| |
| 115 | +| `git show <commit>` | 查看提交详细信息 | |
| 116 | +| `git log --oneline --graph --decorate --all` | 查看提交历史和分支图 | |
| 117 | +| `git blame <file>` | 查看文件每一行最后修改的提交信息 | |
| 118 | +| `git shortlog` | 查看提交历史 | |
| 119 | + |
| 120 | +## 协作和审查 |
| 121 | + |
| 122 | +| 命令 | 说明 | |
| 123 | +|------------------------------|----------------| |
| 124 | +| `git fetch <remote>` | 拉取远程仓库的更新 | |
| 125 | +| `git pull <remote> <branch>` | 拉取远程分支的更新 | |
| 126 | +| `git push <remote> <branch>` | 推送本地分支的更新到远程仓库 | |
| 127 | +| `git cherry-pick <commit>` | 合并指定提交到当前分支 | |
| 128 | + |
| 129 | +## 清理 |
| 130 | + |
| 131 | +| 命令 | 说明 | |
| 132 | +|-----------------|-------------| |
| 133 | +| `git clean -f` | 删除未跟踪的文件 | |
| 134 | +| `git clean -fd` | 删除未跟踪的文件和目录 | |
| 135 | +| `git gc` | 清理仓库,回收空间 | |
| 136 | + |
0 commit comments