|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Git速查手册 |
| 4 | +category: tools |
| 5 | +tags: Git |
| 6 | +--- |
| 7 | + |
| 8 | +* content |
| 9 | +{:toc} |
| 10 | + |
| 11 | +# git 速查手册 |
| 12 | + |
| 13 | +## ssh key |
| 14 | + |
| 15 | +> Git 服务器授权使用 SSH 公钥 |
| 16 | +
|
| 17 | +## Git仓库 |
| 18 | + |
| 19 | +### 工作目录初始化仓库 |
| 20 | + |
| 21 | +``` |
| 22 | +$ git init |
| 23 | +``` |
| 24 | +### 从现有仓库克隆clone |
| 25 | + |
| 26 | +``` |
| 27 | +$ git clone git@github.com:codingted/codingted.github.io.git |
| 28 | +``` |
| 29 | +## Git配置 |
| 30 | + |
| 31 | +``` |
| 32 | +$ git config user.name "xx" |
| 33 | +$ git config user.email "xx@gmail.com" |
| 34 | +# 修改配置 |
| 35 | +$ git config -e |
| 36 | +或者 |
| 37 | +$ vi .git/config |
| 38 | +``` |
| 39 | + |
| 40 | +## Git 关联远程仓库 |
| 41 | + |
| 42 | +``` |
| 43 | +$ git remote add [shortname] [url] |
| 44 | +
|
| 45 | +# 显示所有远程仓库 |
| 46 | +$ git remote -v |
| 47 | +
|
| 48 | +# 显示某一个远程仓库的信息(远程分支,本地分支和远程分支的关联) |
| 49 | +$ git remote show [shortname] |
| 50 | +
|
| 51 | +# 删除远程仓库的关联信息 |
| 52 | +$ git remote rm [shortname] |
| 53 | +``` |
| 54 | +> 本地仓库关联多个远程仓库,在提交时需要指定远程仓库 |
| 55 | +> 另外当本地有多个git仓库需要管理多个ssk-key,见本文: |
| 56 | +> [多Github账户SSH-Key配置](http://www.codingted.com/2017/06/21/Multi_ssh-key_Git/) |
| 57 | +
|
| 58 | +## git本地仓库图示 |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +## 增加/删除 |
| 63 | + |
| 64 | +``` |
| 65 | +# 添加指定文件到暂存区 |
| 66 | +$ git add [file1] [file2] ... |
| 67 | +
|
| 68 | +# 添加指定目录到暂存区,包括子目录 |
| 69 | +$ git add [dir] |
| 70 | +
|
| 71 | +# 添加当前目录的所有文件到暂存区 |
| 72 | +$ git add . |
| 73 | +
|
| 74 | +# 添加每个变化前,都会要求确认 |
| 75 | +# 对于同一个文件的多处变化,可以实现分次提交 |
| 76 | +$ git add -p |
| 77 | +
|
| 78 | +# 删除工作区文件,并且将这次删除放入暂存区 |
| 79 | +$ git rm [file1] [file2] ... |
| 80 | +
|
| 81 | +# 停止追踪指定文件,但该文件会保留在工作区 |
| 82 | +$ git rm --cached [file] |
| 83 | +
|
| 84 | +# 改名文件,并且将这个改名放入暂存区 |
| 85 | +$ git mv [file-original] [file-renamed] |
| 86 | +``` |
| 87 | +## 代码提交 |
| 88 | + |
| 89 | +``` |
| 90 | +# 提交暂存区到仓库区 |
| 91 | +$ git commit -m [message] |
| 92 | +
|
| 93 | +# 提交暂存区的指定文件到仓库区 |
| 94 | +$ git commit [file1] [file2] ... -m [message] |
| 95 | +
|
| 96 | +# 提交工作区自上次commit之后的变化,直接到仓库区 |
| 97 | +$ git commit -a |
| 98 | +
|
| 99 | +# 提交时显示所有diff信息 |
| 100 | +$ git commit -v |
| 101 | +
|
| 102 | +# 使用一次新的commit,替代上一次提交 |
| 103 | +# 如果代码没有任何新变化,则用来改写上一次commit的提交信息 |
| 104 | +$ git commit --amend -m [message] |
| 105 | +
|
| 106 | +# 重做上一次commit,并包括指定文件的新变化 |
| 107 | +$ git commit --amend [file1] [file2] ... |
| 108 | +``` |
| 109 | + |
| 110 | +## 查看提交历史 |
| 111 | + |
| 112 | +``` |
| 113 | +$ git log |
| 114 | +
|
| 115 | +# 展开每次的内容差异并指定最近的x次提交的历史 |
| 116 | +$ git log -p -2 |
| 117 | +
|
| 118 | +# 只显示修改的文件及修改的行数 |
| 119 | +$ git log --stat |
| 120 | +
|
| 121 | +# 显示所有提交过的用户,按提交次数排序 |
| 122 | +$ git shortlog -sn |
| 123 | +
|
| 124 | +# 显示指定文件是什么人在什么时间修改过 |
| 125 | +$ git blame [file] |
| 126 | +
|
| 127 | +# 显示暂存区和工作区的差异 |
| 128 | +$ git diff |
| 129 | +
|
| 130 | +# 显示暂存区和上一个commit的差异 |
| 131 | +$ git diff --cached [file] |
| 132 | +
|
| 133 | +# 显示工作区与当前分支最新commit之间的差异 |
| 134 | +$ git diff HEAD |
| 135 | +
|
| 136 | +# 显示两次提交之间的差异 |
| 137 | +$ git diff [first-branch]...[second-branch] |
| 138 | +
|
| 139 | +# 显示今天你写了多少行代码 |
| 140 | +$ git diff --shortstat "@{0 day ago}" |
| 141 | +
|
| 142 | +# 显示某次提交的元数据和内容变化 |
| 143 | +$ git show [commit] |
| 144 | +
|
| 145 | +# 显示某次提交发生变化的文件 |
| 146 | +$ git show --name-only [commit] |
| 147 | +
|
| 148 | +# 显示某次提交时,某个文件的内容 |
| 149 | +$ git show [commit]:[filename] |
| 150 | +
|
| 151 | +# 显示当前分支的最近几次提交 |
| 152 | +$ git reflog |
| 153 | +``` |
| 154 | + |
| 155 | +### git --pretty 参数 |
| 156 | + |
| 157 | +| 选项 | 说明 | |
| 158 | +| :------------- | :------------- | |
| 159 | +|%H | 提交对象(commit)的完整哈希字串 | |
| 160 | +|%h | 提交对象的简短哈希字串 | |
| 161 | +|%T | 树对象(tree)的完整哈希字串 | |
| 162 | +|%t | 树对象的简短哈希字串 | |
| 163 | +|%P | 父对象(parent)的完整哈希字串 | |
| 164 | +|%p | 父对象的简短哈希字串 | |
| 165 | +|%an | 作者(author)的名字 | |
| 166 | +|%ae | 作者的电子邮件地址 | |
| 167 | +|%ad | 作者修订日期(可以用 -date= 选项定制格式) | |
| 168 | +|%ar | 作者修订日期,按多久以前的方式显示 | |
| 169 | +|%cn | 提交者(committer)的名字 | |
| 170 | +|%ce | 提交者的电子邮件地址 | |
| 171 | +|%cd | 提交日期 | |
| 172 | +|%cr | 提交日期,按多久以前的方式显示 | |
| 173 | +|%s | 提交说明 | |
| 174 | + |
| 175 | +``` |
| 176 | +# 单行显示 |
| 177 | +$ git log --pretty=oneline |
| 178 | +
|
| 179 | +# 更详细的日期信息 |
| 180 | +$ git log --pretty=format:"%h - %an, %ar : %s" |
| 181 | +
|
| 182 | +# 添加 --graph 显示分支分支衍合 |
| 183 | +$ git log --pretty=format:"%h %s" --graph |
| 184 | +``` |
| 185 | +## 分支 |
| 186 | + |
| 187 | +``` |
| 188 | +# 列出所有本地分支 |
| 189 | +$ git branch |
| 190 | +
|
| 191 | +# 列出所有远程分支 |
| 192 | +$ git branch -r |
| 193 | +
|
| 194 | +# 列出所有本地分支和远程分支 |
| 195 | +$ git branch -a |
| 196 | +
|
| 197 | +# 新建一个分支,但依然停留在当前分支 |
| 198 | +$ git branch [branch-name] |
| 199 | +
|
| 200 | +# 新建一个分支,并切换到该分支 |
| 201 | +$ git checkout -b [branch] |
| 202 | +
|
| 203 | +# 新建一个分支,指向指定commit |
| 204 | +$ git branch [branch] [commit] |
| 205 | +
|
| 206 | +# 新建一个分支,与指定的远程分支建立追踪关系 |
| 207 | +$ git branch --track [branch] [remote-branch] |
| 208 | +
|
| 209 | +# 切换到指定分支,并更新工作区 |
| 210 | +$ git checkout [branch-name] |
| 211 | +
|
| 212 | +# 切换到上一个分支 |
| 213 | +$ git checkout - |
| 214 | +
|
| 215 | +# 建立追踪关系,在现有分支与指定的远程分支之间 |
| 216 | +$ git branch --set-upstream [branch] [remote-branch] |
| 217 | +
|
| 218 | +# 合并指定分支到当前分支 |
| 219 | +$ git merge [branch] |
| 220 | +
|
| 221 | +# 选择一个commit,合并进当前分支 |
| 222 | +$ git cherry-pick [commit] |
| 223 | +
|
| 224 | +# 删除分支 |
| 225 | +$ git branch -d [branch-name] |
| 226 | +
|
| 227 | +# 删除远程分支 |
| 228 | +$ git push origin --delete [branch-name] |
| 229 | +$ git branch -dr [remote/branch] |
| 230 | +``` |
| 231 | + |
| 232 | +## 标签 |
| 233 | + |
| 234 | +``` |
| 235 | +# 列出所有tag |
| 236 | +$ git tag |
| 237 | +
|
| 238 | +# 新建一个tag在当前commit |
| 239 | +$ git tag [tag] |
| 240 | +
|
| 241 | +# 新建一个tag在指定commit |
| 242 | +$ git tag [tag] [commit] |
| 243 | +
|
| 244 | +# 删除本地tag |
| 245 | +$ git tag -d [tag] |
| 246 | +
|
| 247 | +# 删除远程tag |
| 248 | +$ git push origin :refs/tags/[tagName] |
| 249 | +
|
| 250 | +# 查看tag信息 |
| 251 | +$ git show [tag] |
| 252 | +
|
| 253 | +# 提交指定tag |
| 254 | +$ git push [remote] [tag] |
| 255 | +
|
| 256 | +# 提交所有tag |
| 257 | +$ git push [remote] --tags |
| 258 | +
|
| 259 | +# 新建一个分支,指向某个tag |
| 260 | +$ git checkout -b [branch] [tag] |
| 261 | +``` |
| 262 | + |
| 263 | +## 远程拉取提交 |
| 264 | + |
| 265 | +``` |
| 266 | +# 取回远程仓库的变化,并与本地分支合并 |
| 267 | +$ git pull [remote] [branch] |
| 268 | +
|
| 269 | +# 上传本地指定分支到远程仓库 |
| 270 | +$ git push [remote] [branch] |
| 271 | +
|
| 272 | +# 强行推送当前分支到远程仓库,即使有冲突 |
| 273 | +$ git push [remote] --force |
| 274 | +
|
| 275 | +# 推送所有分支到远程仓库 |
| 276 | +$ git push [remote] --all |
| 277 | +``` |
| 278 | +## 撤销/回滚 |
| 279 | + |
| 280 | +``` |
| 281 | +# 恢复暂存区的指定文件到工作区 |
| 282 | +$ git checkout [file] |
| 283 | +
|
| 284 | +# 恢复某个commit的指定文件到暂存区和工作区 |
| 285 | +$ git checkout [commit] [file] |
| 286 | +
|
| 287 | +# 恢复暂存区的所有文件到工作区 |
| 288 | +$ git checkout . |
| 289 | +
|
| 290 | +# 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变 |
| 291 | +$ git reset [file] |
| 292 | +
|
| 293 | +# 重置暂存区与工作区,与上一次commit保持一致 |
| 294 | +$ git reset --hard |
| 295 | +
|
| 296 | +# 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变 |
| 297 | +$ git reset [commit] |
| 298 | +
|
| 299 | +# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致 |
| 300 | +$ git reset --hard [commit] |
| 301 | +
|
| 302 | +# 重置当前HEAD为指定commit,但保持暂存区和工作区不变 |
| 303 | +$ git reset --keep [commit] |
| 304 | +
|
| 305 | +# 新建一个commit,用来撤销指定commit |
| 306 | +# 后者的所有变化都将被前者抵消,并且应用到当前分支 |
| 307 | +$ git revert [commit] |
| 308 | +
|
| 309 | +# 暂时将未提交的变化移除,稍后再移入 |
| 310 | +$ git stash |
| 311 | +$ git stash pop |
| 312 | +``` |
| 313 | + |
| 314 | +# 参考链接 |
| 315 | + |
| 316 | +[阮一峰的博客/常用Git命令](http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html) |
| 317 | +[码云](http://git.oschina.net/progit/index.html) |
| 318 | +[图解Git](http://marklodato.github.io/visual-git-guide/index-zh-cn.html) |
0 commit comments