| 1 | --- |
| 2 | layout: zh_reference |
| 3 | --- |
| 4 | |
| 5 | <div class="box"> |
| 6 | <h2> |
| 7 | <span class="docs"> |
| 8 | <a target="new" href="http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository">书</a> |
| 9 | </span> |
| 10 | 基本快照 |
| 11 | </h2> |
| 12 | <div class="block"> |
| 13 | <p> |
| 14 | Git 的工作就是创建和保存你的项目的快照及与之后的快照进行对比。本章将对有关创建与提交你的项目的快照的命令作介绍。 |
| 15 | </p> |
| 16 | |
| 17 | <p> |
| 18 | 这里有个重要的概念,Git 有一个叫做“索引”的东东,有点像是你的快照的缓存区。这就使你能够从更改的文件中创建出一系列组织良好的快照,而不是一次提交所有的更改。 |
| 19 | </p> |
| 20 | |
| 21 | <p class="nutshell"> |
| 22 | <strong>简而言之</strong>,使用 <code>git add</code> 添加需要追踪的新文件和待提交的更改, |
| 23 | 然后使用 <code>git status</code> 和 <code>git diff</code> 查看有何改动, |
| 24 | 最后用 <code>git commit</code> 将你的快照记录。这就是你要用的基本流程,绝大部分时候都是这样的。 |
| 25 | </p> |
| 26 | |
| 27 | </div> |
| 28 | </div> |
| 29 | |
| 30 | <div class="box"> |
| 31 | <h2> |
| 32 | <span class="docs"> |
| 33 | <a target="new" href="http://git-scm.com/docs/git-add">文档</a> |
| 34 | <a target="new" href="http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository#Tracking-New-Files">书</a> |
| 35 | </span> |
| 36 | <a name="add">git add</a> |
| 37 | <span class="desc">添加文件到缓存</span> |
| 38 | </h2> |
| 39 | |
| 40 | <div class="block"> |
| 41 | <p> |
| 42 | 在 Git 中,在提交你修改的文件之前,你需要把它们添加到缓存。如果该文件是新创建的,你可以执行 |
| 43 | <code>git add</code> 将该文件添加到缓存,但是,即使该文件已经被追踪了 —— 也就是说,曾经提交过了 —— |
| 44 | 你仍然需要执行 <cpde>git add</code> 将新更改的文件添加到缓存去。让我们看几个例子: |
| 45 | </p> |
| 46 | |
| 47 | <p>回到我们的 Hello World 示例,初始化该项目之后,我们就要用 <code>git add</code> 将我们的文件添加进去了。 |
| 48 | 我们可以用 <code>git status</code> 看看我们的项目的当前状态。 |
| 49 | </p> |
| 50 | |
| 51 | <pre> |
| 52 | <b>$ git status -s</b> |
| 53 | <span class="red">??</span> README |
| 54 | <span class="red">??</span> hello.rb |
| 55 | </pre> |
| 56 | |
| 57 | <p>我们有俩尚未被追踪的文件,得添加一下。</p> |
| 58 | |
| 59 | <pre> |
| 60 | <b>$ git add README hello.rb</b> |
| 61 | </pre> |
| 62 | |
| 63 | <p>现在我们再执行 <code>git status</code>,就可以看到这俩文件已经加上去了。</p> |
| 64 | |
| 65 | <pre> |
| 66 | <b>$ git status -s</b> |
| 67 | <span class="green">A</span> README |
| 68 | <span class="green">A</span> hello.rb |
| 69 | </pre> |
| 70 | |
| 71 | <p class="aside"> |
| 72 | 新项目中,添加所有文件很普遍,可以在当前工作目录执行命令:<code>git add .</code>。 |
| 73 | 因为 Git 会递归地将你执行命令时所在的目录中的所有文件添加上去,所以如果你将当前的工作目录作为参数, |
| 74 | 它就会追踪那儿的所有文件了。如此,<code>git add .</code> 就和 <code>git add README hello.rb</code> |
| 75 | 有一样的效果。 |
| 76 | 此外,效果一致的还有 <code>git add *</code>,不过那只是因为我们这还木有子目录,不需要递归地添加新文件。 |
| 77 | </p> |
| 78 | |
| 79 | <p>好了,现在我们改个文件,再跑一下 <code>git status</code>,有点古怪。</p> |
| 80 | <pre> |
| 81 | <b>$ vim README</b> |
| 82 | <b>$ git status -s</b> |
| 83 | <span class="green">A</span><span class="red">M</span> README |
| 84 | <span class="green">A</span> hello.rb |
| 85 | </pre> |
| 86 | |
| 87 | <p>“AM” 状态的意思是,这个文件在我们将它添加到缓存之后又有改动。这意味着如果我们现在提交快照, |
| 88 | 我们记录的将是上次跑 <code>git add</code> 的时候的文件版本,而不是现在在磁盘中的这个。 |
| 89 | Git 并不认为磁盘中的文件与你想快照的文件必须是一致的 —— (如果你需要它们一致,)得用 <code>git add</code> 命令告诉它。 |
| 90 | </p> |
| 91 | |
| 92 | <p class="nutshell"> |
| 93 | <strong>一言以蔽之</strong>, |
| 94 | 当你要将你的修改包含在即将提交的快照里的时候,执行 <code>git add</code>。 |
| 95 | 任何你没有添加的改动都不会被包含在内 —— 这意味着你可以比绝大多数其他源代码版本控制系统更精确地归置你的快照。 |
| 96 | </p> |
| 97 | |
| 98 | <p>请查看《Pro Git》中 <code>git add</code> 的 “-p” 参数,以了解更多关于提交文件的灵活性的例子。 |
| 99 | </p> |
| 100 | |
| 101 | </div> |
| 102 | |
| 103 | </div> |
| 104 | |
| 105 | <div class="box"> |
| 106 | <h2> |
| 107 | <span class="docs"> |
| 108 | <a target="new" href="http://git-scm.com/docs/git-status">文档</a> |
| 109 | <a target="new" href="http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository#Checking-the-Status-of-Your-Files">书</a> |
| 110 | </span> |
| 111 | <a name="status">git status</a> |
| 112 | <span class="desc">查看你的文件在工作目录与缓存的状态</span> |
| 113 | </h2> |
| 114 | |
| 115 | <div class="block"> |
| 116 | <p>正如你在 <code>git add</code> 小节中所看到的,你可以执行 <code>git status</code> |
| 117 | 命令查看你的代码在缓存与当前工作目录的状态。我演示该命令的时候加了 <code>-s</code> 参数,以获得简短的结果输出。 |
| 118 | 若没有这个标记,命令 <code>git status</code> 将告诉你更多的提示与上下文欣喜。 |
| 119 | 以下便是同样状态下,有跟没有 <code>-s</code> 参数的输出对比。简短的输出如下: |
| 120 | </p> |
| 121 | |
| 122 | <pre> |
| 123 | <b>$ git status -s</b> |
| 124 | <span class="green">A</span><span class="red">M</span> README |
| 125 | <span class="green">A</span> hello.rb |
| 126 | </pre> |
| 127 | |
| 128 | <p>而同样的状态,详细的输出看起来是这样的:</p> |
| 129 | |
| 130 | <pre> |
| 131 | <b>$ git status</b> |
| 132 | # On branch master |
| 133 | # |
| 134 | # Initial commit |
| 135 | # |
| 136 | # Changes to be committed: |
| 137 | # (use "git rm --cached <file>..." to unstage) |
| 138 | # |
| 139 | # <span class="green">new file: README</span> |
| 140 | # <span class="green">new file: hello.rb</span> |
| 141 | # |
| 142 | # Changed but not updated: |
| 143 | # (use "git add <file>..." to update what will be committed) |
| 144 | # (use "git checkout -- <file>..." to discard changes in working directory) |
| 145 | # |
| 146 | # <span class="red">modified: README</span> |
| 147 | # |
| 148 | </pre> |
| 149 | |
| 150 | <p>你很容易发现简短的输出看起来很紧凑。而详细输出则很有帮助,提示你可以用何种命令完成你接下来可能要做的事情。 |
| 151 | </p> |
| 152 | |
| 153 | <p>Git 还会告诉你在你上次提交之后,有哪些文件被删除、修改或者存入缓存了。</p> |
| 154 | |
| 155 | <pre> |
| 156 | <b>$ git status -s</b> |
| 157 | <span class="green">M</span> README |
| 158 | <span class="red">D</span> hello.rb |
| 159 | </pre> |
| 160 | |
| 161 | <p>你可以看到,在简短输出中,有两栏。第一栏是缓存的,第二栏则是工作目录的。 |
| 162 | 所以假设你临时提交了 README 文件,然后又改了些,并且没有执行 <code>git add</code>,你会看到这个:</p> |
| 163 | |
| 164 | <pre> |
| 165 | <b>$ git status -s</b> |
| 166 | <span class="green">M</span><span class="red">M</span> README |
| 167 | <span class="red">D</span> hello.rb |
| 168 | </pre> |
| 169 | |
| 170 | <p class="nutshell"> |
| 171 | <strong>一言以蔽之</strong>,执行 <code>git status</code> 以查看在你上次提交之后有啥被修改或者临时提交了, |
| 172 | 从而决定自己是否需要提交一次快照,同时也能知道有什么改变被记录进去了。 |
| 173 | </p> |
| 174 | |
| 175 | </div> |
| 176 | </div> |
| 177 | |
| 178 | <div class="box"> |
| 179 | <h2> |
| 180 | <span class="docs"> |
| 181 | <a target="new" href="http://git-scm.com/docs/git-diff">文档</a> |
| 182 | <a target="new" href="http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository#Viewing-Your-Staged-and-Unstaged-Changes">书</a> |
| 183 | </span> |
| 184 | <a name="diff">git diff</a> |
| 185 | <span class="desc">显示已写入缓存与已修改但尚未写入缓存的改动的区别</span> |
| 186 | </h2> |
| 187 | |
| 188 | <div class="block"> |
| 189 | <p><code>git diff</code> 有两个主要的应用场景。我们将在此介绍其一, |
| 190 | 在 <a href="/inspect">检阅与对照</a> 一章中,我们将介绍其二。 |
| 191 | 我们这里介绍的方式是用此命令描述已临时提交的或者已修改但尚未提交的改动。 |
| 192 | </p> |
| 193 | |
| 194 | <h4> |
| 195 | git diff |
| 196 | <small>#尚未缓存的改动</small> |
| 197 | </h4> |
| 198 | |
| 199 | <p>如果没有其他参数,<code>git diff</code> 会以规范化的 diff 格式(一个补丁)显示自从你上次提交快照之后尚未缓存的所有更改。 |
| 200 | </p> |
| 201 | |
| 202 | <pre> |
| 203 | <b>$ vim hello.rb</b> |
| 204 | <b>$ git status -s</b> |
| 205 | <span class="red">M</span> hello.rb |
| 206 | <b>$ git diff</b> |
| 207 | <span class="umber">diff --git a/hello.rb b/hello.rb |
| 208 | index d62ac43..8d15d50 100644 |
| 209 | --- a/hello.rb |
| 210 | +++ b/hello.rb</span> |
| 211 | <span class="lblue">@@ -1,7 +1,7 @@</span> |
| 212 | class HelloWorld |
| 213 | |
| 214 | def self.hello |
| 215 | <span class="red">- puts "hello world"</span> |
| 216 | <span class="green">+ puts "hola mundo"</span> |
| 217 | end |
| 218 | |
| 219 | end |
| 220 | </pre> |
| 221 | |
| 222 | <p>所以,<code>git status</code>显示你上次提交更新至后所更改或者写入缓存的改动, |
| 223 | 而 <code>git diff</code> 一行一行地显示这些改动具体是啥。 |
| 224 | 通常执行完 <code>git status</code> 之后接着跑一下 <code>git diff</code> 是个好习惯。 |
| 225 | </p> |
| 226 | |
| 227 | <h4> |
| 228 | git diff --cached |
| 229 | <small>#查看已缓存的改动</small> |
| 230 | </h4> |
| 231 | |
| 232 | <p><code>git diff --cached</code> 命令会告诉你有哪些内容已经写入缓存了。 |
| 233 | 也就是说,此命令显示的是接下来要写入快照的内容。所以,如果你将上述示例中的 <code>hello.rb</code> |
| 234 | 写入缓存,因为 <code>git diff</code> 显示的是尚未缓存的改动,所以在此执行它不会显示任何信息。 |
| 235 | </p> |
| 236 | |
| 237 | <pre> |
| 238 | <b>$ git status -s</b> |
| 239 | <span class="red">M</span> hello.rb |
| 240 | <b>$ git add hello.rb </b> |
| 241 | <b>$ git status -s</b> |
| 242 | <span class="green">M</span> hello.rb |
| 243 | <b>$ git diff</b> |
| 244 | <b>$ </b> |
| 245 | </pre> |
| 246 | |
| 247 | <p>如果你想看看已缓存的改动,你需要执行的是 |
| 248 | <code>git diff --cached</code>。</p> |
| 249 | |
| 250 | <pre> |
| 251 | <b>$ git status -s</b> |
| 252 | <span class="green">M</span> hello.rb |
| 253 | <b>$ git diff</b> |
| 254 | <b>$ </b> |
| 255 | <b>$ git diff --cached</b> |
| 256 | <span class="umber">diff --git a/hello.rb b/hello.rb |
| 257 | index d62ac43..8d15d50 100644 |
| 258 | --- a/hello.rb |
| 259 | +++ b/hello.rb</span> |
| 260 | <span class="lblue">@@ -1,7 +1,7 @@</span> |
| 261 | class HelloWorld |
| 262 | |
| 263 | def self.hello |
| 264 | <span class="red">- puts "hello world"</span> |
| 265 | <span class="green">+ puts "hola mundo"</span> |
| 266 | end |
| 267 | |
| 268 | end |
| 269 | </pre> |
| 270 | |
| 271 | <h4> |
| 272 | git diff HEAD |
| 273 | <small>查看已缓存的与未缓存的所有改动</small> |
| 274 | </h4> |
| 275 | |
| 276 | <p>如果你想一并查看已缓存的与未缓存的改动,可以执行 <code>git diff HEAD</code> —— |
| 277 | 也就是说你要看到的是工作目录与上一次提交的更新的区别,无视缓存。 |
| 278 | 假设我们又改了些 <code>ruby.rb</code> 的内容,那缓存的与未缓存的改动我们就都有了。 |
| 279 | 以上三个 <code>diff</code> 命令的结果如下: |
| 280 | </p> |
| 281 | <pre> |
| 282 | <b>$ vim hello.rb </b> |
| 283 | <b>$ git diff</b> |
| 284 | <span class="umber">diff --git a/hello.rb b/hello.rb |
| 285 | index 4f40006..2ae9ba4 100644 |
| 286 | --- a/hello.rb |
| 287 | +++ b/hello.rb</span> |
| 288 | <span class="lblue">@@ -1,7 +1,7 @@</span> |
| 289 | class HelloWorld |
| 290 | |
| 291 | <span class="green">+ # says hello</span> |
| 292 | def self.hello |
| 293 | puts "hola mundo" |
| 294 | end |
| 295 | |
| 296 | end |
| 297 | <b>$ git diff --cached</b> |
| 298 | <span class="umber">diff --git a/hello.rb b/hello.rb |
| 299 | index 2aabb6e..4f40006 100644 |
| 300 | --- a/hello.rb |
| 301 | +++ b/hello.rb</span> |
| 302 | <span class="lblue">@@ -1,7 +1,7 @@</span> |
| 303 | class HelloWorld |
| 304 | |
| 305 | def self.hello |
| 306 | <span class="red">- puts "hello world"</span> |
| 307 | <span class="green">+ puts "hola mundo"</span> |
| 308 | end |
| 309 | |
| 310 | end |
| 311 | <b>$ git diff HEAD</b> |
| 312 | <span class="umber">diff --git a/hello.rb b/hello.rb |
| 313 | index 2aabb6e..2ae9ba4 100644 |
| 314 | --- a/hello.rb |
| 315 | +++ b/hello.rb</span> |
| 316 | <span class="lblue">@@ -1,7 +1,8 @@</span> |
| 317 | class HelloWorld |
| 318 | |
| 319 | <span class="green">+ # says hello</span> |
| 320 | def self.hello |
| 321 | <span class="red">- puts "hello world"</span> |
| 322 | <span class="green">+ puts "hola mundo"</span> |
| 323 | end |
| 324 | |
| 325 | end |
| 326 | </pre> |
| 327 | |
| 328 | <h4> |
| 329 | git diff --stat |
| 330 | <small>显示摘要而非整个 diff</small> |
| 331 | </h4> |
| 332 | |
| 333 | <p>如果我们不想要看整个 diff 输出,但是又想比 <code>git status</code> 详细点, |
| 334 | 就可以用 <code>--stat</code> 选项。该选项使它显示摘要而非全文。上文示例在使用 <code>--stat</code> 选项时,输出如下: |
| 335 | </p> |
| 336 | |
| 337 | <pre> |
| 338 | <b>$ git status -s</b> |
| 339 | <span class="green">M</span><span class="red">M</span> hello.rb |
| 340 | <b>$ git diff --stat</b> |
| 341 | hello.rb | 1 <span class="green">+</span> |
| 342 | 1 files changed, 1 insertions(+), 0 deletions(-) |
| 343 | <b>$ git diff --cached --stat</b> |
| 344 | hello.rb | 2 <span class="green">+</span><span class="red">-</span> |
| 345 | 1 files changed, 1 insertions(+), 1 deletions(-) |
| 346 | <b>$ git diff HEAD --stat</b> |
| 347 | hello.rb | 3 <span class="green">++</span><span class="red">-</span> |
| 348 | 1 files changed, 2 insertions(+), 1 deletions(-) |
| 349 | </pre> |
| 350 | |
| 351 | <p>你还可以在上述命令后面制定一个目录,从而只查看特定文件或子目录的 <code>diff</code> 输出。 |
| 352 | </p> |
| 353 | |
| 354 | |
| 355 | <p class="nutshell"> |
| 356 | <strong>简而言之</strong>, |
| 357 | 执行 <code>git diff</code> 来查看执行 <code>git status</code> 的结果的详细信息 —— |
| 358 | 一行一行地显示这些文件是如何被修改或写入缓存的。 |
| 359 | </p> |
| 360 | |
| 361 | </div> |
| 362 | </div> |
| 363 | |
| 364 | <div class="box"> |
| 365 | <h2> |
| 366 | <span class="docs"> |
| 367 | <a target="new" href="http://git-scm.com/docs/git-commit">文档</a> |
| 368 | <a target="new" href="http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository#Committing-Your-Changes">书</a> |
| 369 | </span> |
| 370 | <a name="commit">git commit</a> |
| 371 | <span class="desc">记录缓存内容的快照</span> |
| 372 | </h2> |
| 373 | |
| 374 | <div class="block"> |
| 375 | |
| 376 | <p>现在你使用 <code>git add</code> 命令将想要快照的内容写入了缓存, |
| 377 | 执行 <code> git commit</code> 就将它实际存储快照了。 |
| 378 | Git 为你的每一个提交都记录你的名字与电子邮箱地址,所以第一步是告诉 Git 这些都是啥。 |
| 379 | </p> |
| 380 | |
| 381 | <pre> |
| 382 | <b>$ git config --global user.name 'Your Name'</b> |
| 383 | <b>$ git config --global user.email you@somedomain.com</b> |
| 384 | </pre> |
| 385 | |
| 386 | <p>让我们写入缓存,并提交对 <code>hello.rb</code> 的所有改动。在首个例子中,我们使用 <code>-m</code> 选项以在命令行中提供提交注释。 |
| 387 | </p> |
| 388 | |
| 389 | <pre> |
| 390 | <b>$ git add hello.rb </b> |
| 391 | <b>$ git status -s</b> |
| 392 | <span class="green">M</span> hello.rb |
| 393 | <b>$ git commit -m 'my hola mundo changes'</b> |
| 394 | [master 68aa034] my hola mundo changes |
| 395 | 1 files changed, 2 insertions(+), 1 deletions(-) |
| 396 | </pre> |
| 397 | |
| 398 | <p>现在我们已经记录了快照。如果我们再执行 <code>git status</code>,会看到我们有一个“干净的工作目录”。 |
| 399 | 这意味着我们在最近一次提交之后,没有做任何改动 —— 在我们的项目中没有未快照的工作。 |
| 400 | </p> |
| 401 | |
| 402 | <pre> |
| 403 | <b>$ git status</b> |
| 404 | # On branch master |
| 405 | nothing to commit (working directory clean) |
| 406 | </pre> |
| 407 | |
| 408 | <p>如果你漏掉了 <code>-m</code> 选项,Git 会尝试为你打开一个编辑器以填写提交信息。 |
| 409 | 如果 Git 在你对它的配置中找不到相关信息,默认会打开 <code>vim</code>。屏幕会像这样: |
| 410 | </p> |
| 411 | |
| 412 | <pre> |
| 413 | |
| 414 | # Please enter the commit message for your changes. Lines starting |
| 415 | # with '#' will be ignored, and an empty message aborts the commit. |
| 416 | # On branch master |
| 417 | # Changes to be committed: |
| 418 | # (use "git reset HEAD <file>..." to unstage) |
| 419 | # |
| 420 | # modified: hello.rb |
| 421 | # |
| 422 | ~ |
| 423 | ~ |
| 424 | ".git/COMMIT_EDITMSG" 9L, 257C |
| 425 | </pre> |
| 426 | |
| 427 | <p>在此,你在文件头部添加实际的提交信息。以“#”开头的行都会被无视 ——Git 将 <code> git status</code> 的输出结果放在那儿以提示你都改了、缓存了啥。 |
| 428 | </p> |
| 429 | |
| 430 | <p>通常,撰写良好的提交信息是很重要的。以开放源代码项目为例,多多少少以以下格式写你的提示消息是个不成文的规定: |
| 431 | </p> |
| 432 | |
| 433 | <pre> |
| 434 | 简短的关于改动的总结(25个字或者更少) |
| 435 | |
| 436 | 如果有必要,更详细的解释文字。约 36 字时换行。在某些情况下, |
| 437 | 第一行会被作为电子邮件的开头,而剩余的则会作为邮件内容。 |
| 438 | 将小结从内容隔开的空行是至关重要的(除非你没有内容); |
| 439 | 如果这两个待在一起,有些 git 工具会犯迷糊。 |
| 440 | |
| 441 | 空行之后是更多的段落。 |
| 442 | |
| 443 | - 列表也可以 |
| 444 | |
| 445 | - 通常使用连字符(-)或者星号(*)来标记列表,前面有个空格, |
| 446 | 在列表项之间有空行,不过这些约定也会有些变化。 |
| 447 | |
| 448 | # Please enter the commit message for your changes. Lines starting |
| 449 | # with '#' will be ignored, and an empty message aborts the commit. |
| 450 | # On branch master |
| 451 | # Changes to be committed: |
| 452 | # (use "git reset HEAD <file>..." to unstage) |
| 453 | # |
| 454 | # modified: hello.rb |
| 455 | # |
| 456 | ~ |
| 457 | ~ |
| 458 | ~ |
| 459 | ".git/COMMIT_EDITMSG" 25L, 884C written |
| 460 | </pre> |
| 461 | |
| 462 | <p class="aside"> |
| 463 | 提交注解是很重要的。因为 Git 很大一部分能耐就是它在组织本地提交和与他人分享的弹性, |
| 464 | 它很给力地能够让你为逻辑独立的改变写三到四条提交注解,以便你的工作被同仁审阅。因为提交与推送改动是有区别的, |
| 465 | 请务必花时间将各个逻辑独立的改动放到另外一个提交,并附上一份良好的提交注解, |
| 466 | 以使与你合作的人能够方便地了解你所做的,以及你为何要这么做。 |
| 467 | </p> |
| 468 | |
| 469 | <h4> |
| 470 | git commit -a |
| 471 | <small>自动将在提交前将已记录、修改的文件放入缓存区</small> |
| 472 | </h4> |
| 473 | |
| 474 | <p>如果你觉得 <code>git add</code> 提交缓存的流程太过繁琐,Git 也允许你用 <code>-a</code> 选项跳过这一步。 |
| 475 | 基本上这句话的意思就是,为任何已有记录的文件执行 <code>git add</code> —— |
| 476 | 也就是说,任何在你最近的提交中已经存在,并且之后被修改的文件。 |
| 477 | 这让你能够用更 Subversion 方式的流程,修改些文件,然后想要快照所有所做的改动的时候执行 <code>git commit -a</code>。 |
| 478 | 不过你仍然需要执行 <code>git add</code> 来添加新文件,就像 Subversion 一样。 |
| 479 | </p> |
| 480 | |
| 481 | <pre> |
| 482 | <b>$ vim hello.rb</b> |
| 483 | <b>$ git status -s</b> |
| 484 | <span class="red">M</span> hello.rb |
| 485 | <b>$ git commit -m 'changes to hello file'</b> |
| 486 | # On branch master |
| 487 | # Changed but not updated: |
| 488 | # (use "git add <file>..." to update what will be committed) |
| 489 | # (use "git checkout -- <file>..." to discard changes in working directory) |
| 490 | # |
| 491 | # modified: hello.rb |
| 492 | # |
| 493 | <span class="hl">no changes added to commit (use "git add" and/or "git commit -a")</span> |
| 494 | <b>$ git commit -am 'changes to hello file'</b> |
| 495 | [master 78b2670] changes to hello file |
| 496 | 1 files changed, 2 insertions(+), 1 deletions(-) |
| 497 | </pre> |
| 498 | |
| 499 | <p>注意,如果你不缓存改动,直接执行 <code>git commit</code>,Git 会直接给出 <code>git status</code> |
| 500 | 命令的输出,提醒你啥也没缓存。我已将该消息中的重要部分高亮,它说没有添加需要提交的缓存。 |
| 501 | 如果你使用 <code>-a</code>,它会缓存并提交每个改动(不含新文件)。 |
| 502 | </p> |
| 503 | |
| 504 | <p> |
| 505 | 现在你就完成了整个快照的流程 ——改些文件,然后用 <code>git add</code> 将要提交的改动提交到缓存, |
| 506 | 用 <code>git status</code> 和 <code>git diff</code> 看看你都改了啥,最后 <code>git commit</code> 永久地保存快照。 |
| 507 | </p> |
| 508 | |
| 509 | <p class="nutshell"> |
| 510 | <strong>简而言之</strong>,执行 <code>git commit</code> 记录缓存区的快照。如果需要的话,这个快照可以用来做比较、共享以及恢复。 |
| 511 | </p> |
| 512 | |
| 513 | </div> |
| 514 | </div> |
| 515 | |
| 516 | <div class="box"> |
| 517 | <h2> |
| 518 | <span class="docs"> |
| 519 | <a target="new" href="http://git-scm.com/docs/git-reset">文档</a> |
| 520 | <a target="new" href="http://git-scm.com/book/en/Git-Basics-Undoing-Things#Unstaging-a-Staged-File">书</a> |
| 521 | </span> |
| 522 | <a name="reset">git reset HEAD</a> |
| 523 | <span class="desc">取消缓存已缓存的内容</span> |
| 524 | </h2> |
| 525 | |
| 526 | <div class="block"> |
| 527 | <p><code>git reset</code> 可能是人类写的最费解的命令了。 |
| 528 | 我用 Git 有些年头了,甚至还写了本书,但有的时候还是会搞不清它会做什么。 |
| 529 | 所以,我只说三个明确的,通常有用的调用。请你跟我一样尽管用它 —— 因为它可以很有用。 |
| 530 | </p> |
| 531 | |
| 532 | <p>在此例中,我们可以用它来将不小心缓存的东东取消缓存。假设你修改了两个文件,想要将它们记录到两个不同的提交中去。 |
| 533 | 你应该缓存并提交一个,再缓存并提交另外一个。如果你不小心两个都缓存了,那要如何才能<i>取消</i>缓存呢? |
| 534 | 你可以用 <code>git reset HEAD -- file</code>。 |
| 535 | 技术上说,在这里你不需要使用 <code>--</code> —— 它用来告诉 Git 这时你已经不再列选项,剩下的是文件路径了。 |
| 536 | 不过养成使用它分隔选项与路径的习惯很重要,即使在你可能并不需要的时候。 |
| 537 | </p> |
| 538 | |
| 539 | <p>好,让我们看看取消缓存是什么样子的。这里我们有两个最近提交之后又有所改动的文件。我们将两个都缓存,并取消缓存其中一个。</p> |
| 540 | |
| 541 | <pre> |
| 542 | <b>$ git status -s</b> |
| 543 | <span class="red">M</span> README |
| 544 | <span class="red">M</span> hello.rb |
| 545 | <b>$ git add .</b> |
| 546 | <b>$ git status -s</b> |
| 547 | <span class="green">M</span> README |
| 548 | <span class="green">M</span> hello.rb |
| 549 | <b>$ git reset HEAD -- hello.rb </b> |
| 550 | Unstaged changes after reset: |
| 551 | M hello.rb |
| 552 | <b>$ git status -s</b> |
| 553 | <span class="green">M</span> README |
| 554 | <span class="red">M</span> hello.rb |
| 555 | </pre> |
| 556 | |
| 557 | <p> |
| 558 | 现在你执行 <code>git commit</code> 将只记录 <code>README</code> 文件的改动,并不含现在并不在缓存中的 <code>hello.rb</code>。 |
| 559 | </p> |
| 560 | |
| 561 | <p class="aside"> |
| 562 | 如果你好奇,它实际的操作是将该文件在“索引”中的校验和重置为最近一次提交中的值。 |
| 563 | <code>git add</code> 会计算一个文件的校验和,将它添加到“索引”中, |
| 564 | 而 <code>git reset HEAD</code> 将它改写回原先的,从而取消缓存操作。 |
| 565 | </p> |
| 566 | |
| 567 | <p class="tip"> |
| 568 | 如果你想直接执行 <code>git unstage</code>,你可以在 Git 中配置个别名。 |
| 569 | 执行 <code>git config --global alias.unstage "reset HEAD"</code> 即可。 |
| 570 | 一旦执行完它,你就可以直接用 <code>git unstage [file]</code> 作为代替了。 |
| 571 | </p> |
| 572 | |
| 573 | <p>如果你忘了取消缓存的命令,Git 的常规 <code>git status</code> 输出的提示会很有帮助。 |
| 574 | 例如,在你有已缓存的文件时,如果你不带 <code>-s</code> 执行 <code>git status</code>,它将告诉你怎样取消缓存: |
| 575 | </p> |
| 576 | |
| 577 | <pre> |
| 578 | <b>$ git status</b> |
| 579 | # On branch master |
| 580 | # Changes to be committed: |
| 581 | # <span class="hl">(use "git reset HEAD <file>..." to unstage)</span> |
| 582 | # |
| 583 | # <span class="green">modified: README</span> |
| 584 | # <span class="green">modified: hello.rb</span> |
| 585 | # |
| 586 | </pre> |
| 587 | |
| 588 | <p class="nutshell"> |
| 589 | <strong>简而言之</strong>,执行 <code>git reset HEAD</code> 以取消之前 <code>git add</code> 添加,但不希望包含在下一提交快照中的缓存。 |
| 590 | </p> |
| 591 | |
| 592 | </div> |
| 593 | </div> |
| 594 | |
| 595 | <div class="box"> |
| 596 | <h2> |
| 597 | <span class="docs"> |
| 598 | <a href="http://git-scm.com/docs/git-rm">文档</a> |
| 599 | <a href="http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository#Removing-Files">书</a> |
| 600 | </span> |
| 601 | <a name="rm-mv">git rm</a> |
| 602 | <span class="desc">将文件从缓存区移除</span> |
| 603 | </h2> |
| 604 | |
| 605 | <div class="block"> |
| 606 | |
| 607 | <p> |
| 608 | <code>git rm</code> 会将条目从缓存区中移除。这与 <code>git reset HEAD</code> 将条目取消缓存是有区别的。 |
| 609 | “取消缓存”的意思就是将缓存区恢复为我们做出修改之前的样子。 |
| 610 | 在另一方面,<code>git rm</code> 则将该文件彻底从缓存区踢出,因此它不再下一个提交快照之内,进而有效地删除它。 |
| 611 | </p> |
| 612 | |
| 613 | <p> |
| 614 | 默认情况下,<code>git rm file</code> 会将文件从缓存区和你的硬盘中(工作目录)删除。 |
| 615 | 如果要在工作目录中留着该文件,可以使用 <code>git rm --cached</code> |
| 616 | </p> |
| 617 | |
| 618 | <h4> |
| 619 | git mv |
| 620 | <small>git rm --cached orig; mv orig new; git add new</small> |
| 621 | </h4> |
| 622 | |
| 623 | <p> |
| 624 | 不像绝大多数其他版本控制系统,Git 并不记录记录文件重命名。它反而只记录快照,并对比快照以找到有啥文件可能被重命名了。 |
| 625 | 如果一个文件从更新中删除了,而在下次快照中新添加的另一个文件的内容与它很相似,Git 就知道这极有可能是个重命名。 |
| 626 | 因此,虽然有 <code>git mv</code> 命令,但它有点多余 —— 它做得所有事情就是 <code>git rm --cached</code>, |
| 627 | 重命名磁盘上的文件,然后再执行 <code>git add</code> 把新文件添加到缓存区。 |
| 628 | 你并不需要用它,不过如果觉得这样容易些,尽管用吧。 |
| 629 | </p> |
| 630 | |
| 631 | <p class="aside"> |
| 632 | 我自己并不使用此命令的普通形式 —— 删除文件。通常直接从硬盘删除文件,然后执行 <code>git commit -a</code> 会简单些。 |
| 633 | 它会自动将删除的文件从索引中移除。 |
| 634 | </p> |
| 635 | |
| 636 | <p class="nutshell"> |
| 637 | <strong>简而言之</strong>, |
| 638 | 执行 <code>git rm</code> 来删除 Git 追踪的文件。它还会删除你的工作目录中的相应文件。 |
| 639 | </p> |
| 640 | |
| 641 | </div> |
| 642 | </div> |
| 643 | |
| 644 | <p><a href="{{ site.baseurl }}/zh/branching">到 分支与合并 »</a></p> |