# 12. 獲得舊版本
## 目的
> 學習如何檢出工作目錄中的先前快照。
在歷史中回憶很容易。`checkout` 命令將從倉庫復制任意快照 到工作目錄。
### 獲得先前版本的哈希
```
$ git hist
```
注意:你記得在 `.gitconfig` 文件中定義的 `hist`,對嗎?如果 不是這樣,那么回顧一下有關別名的實驗。
```
$ git hist
* 1f7ec5e 2013-04-13 | Added a comment (HEAD, master) [Jim Weirich]
* 582495a 2013-04-13 | Added a default value [Jim Weirich]
* 323e28d 2013-04-13 | Using ARGV [Jim Weirich]
* 9416416 2013-04-13 | First Commit [Jim Weirich]
```
檢查日志輸出,并找到第一個提交的哈希。它應當在 `git hist` 輸出的最后一行。在下面的命令中使用哈希碼(前 7 個字符就 夠了)。然后檢查 hello.rb 文件的內容。
```
$ git checkout <hash>
$ cat hello.rb
```
注意:這兒給的是 Unix 命令,適用于 Mac 和 Linux 系統。不幸 的是,Windows 用戶必須換成他們的原生命令。
注意:許多命令都需要倉庫中的哈希值。因為你的哈希值將與我的 不同,當你看到命令中的 `<hash>` 或 `<treehash>` 時,使用你 倉庫的正確哈希值替換。
你應該看到:
```
$ git checkout 9416416
Note: checking out '9416416'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at 9416416... First Commit
$ cat hello.rb
puts "Hello, World"
```
在 Git 中的“detached HEAD”信息意味著 HEAD (Git 跟蹤當前目錄 應當匹配的那部分)是直接指向提交而非分支。在你沒有切換到不同 的分支時,這種狀態只會記得已經提交的更改。一旦你檢出了新的 分支或標簽,分離的提交將被丟棄 (因為 HEAD 已經移走)。如果你 想要保存在分離狀態的提交,那么你需要創建分支來記住提交。
Git 的舊版本將抱怨分離的 HEAD 狀態不在本地分支。現在不用擔心 這種情況。
注意 hello.rb 文件是最原始的內容。
### 回到在 master 分支中的最新版本
```
$ git checkout master
$ cat hello.rb
```
你應該看到:
```
$ git checkout master
Previous HEAD position was 9416416... First Commit
Switched to branch 'master'
$ cat hello.rb
# Default is "World"
name = ARGV.first || "World"
puts "Hello, #{name}!"
```
`master` 是默認分支的名稱。通過名稱檢出分支,你能夠回到 該分支的最新版本。
- 關于
- 1. 設置
- 2. 再談設置
- 3. 創建項目
- 4. 檢查狀態
- 5. 做更改
- 6. 暫存更改
- 7. 暫存與提交
- 8. 提交更改
- 9. 更改而非文件
- 10. 歷史
- 11. 別名
- 12. 獲得舊版本
- 13. 給版本打標簽
- 14. 撤銷本地更改
- 15. 撤銷暫存的更改
- 16. 撤銷提交的更改
- 17. 從分支移除提交
- 18. 移除 oops 標簽
- 19. 修正提交
- 20. 移動文件
- 21. 再談結構
- 22. Git 內幕:.git 目錄
- 23. Git 內幕:直接處理 Git 對象
- 24. 創建分支
- 25. 導航分支
- 26. 在 master 中更改
- 27. 查看分叉的分支
- 28. 合并
- 29. 創建沖突
- 30. 解決沖突
- 31. 變基 VS 合并
- 32. 重置 greet 分支
- 33. 重置 master 分支
- 34. 變基
- 35. 合并回 master
- 36. 多個倉庫
- 37. 克隆倉庫
- 38. 回顧克隆的倉庫
- 39. 何為 Origin?
- 40. 遠程分支
- 41. 更改原始倉庫
- 42. 取得更改
- 43. 合并拉下的更改
- 44. 拉下更改
- 45. 添加跟蹤的分支
- 46. 裸倉庫
- 47. 添加遠程倉庫
- 48. 推送更改
- 49. 拉下共享的更改
- 50. 托管你的 Git 倉庫
- 51. 共享倉庫
- 52. 高級/將來的主題