# 4.1.2.commit

## 三個主要角色: Working Tree, Index, Repository

* Working Tree(工作目錄): &#x20;
  * Git管理的實體資料夾, 也是實際操作的資料夾(檔案)
* Index (系統索引): &#x20;
  * 存放一堆需要被commit的(異動)文件內容集合, 把檔案加入索引稱 Stage 或 Cache
* Repository
  * 是Git存放檔案的位置, 許多commit結點(版本)紀錄於此

    ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-07-04%20%E4%B8%8B%E5%8D%888.51.19.png)

## 修改commit (git reset)

* git reset就是重置HEAD
  * \--soft:&#x20;
    * working tree, Index未改變, 就只是改變repository (commit)
    * 所有的東西仍在準備狀態, 因此可以有另一次送交的機會

      ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-07-04%20%E4%B8%8B%E5%8D%888.52.55.png)

      * 1.取消最新一次的commit
        * git reset --soft HEAD^
      * 2.重新遞交commit
        * git commit -a -c ORIG\_HEAD
  * mixed: working tree未改變, 改變Index, repository(commit)

    &#x20;  ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-07-04%20%E4%B8%8B%E5%8D%888.55.15.png)
  * \--hard
    * 會更改working copy的檔案改回原樣, working tree會被改變, 並重置HEAD
    * 不關心內容

      ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-07-04%20%E4%B8%8B%E5%8D%888.56.23.png)

      * 1.取消最新一次的commit
        * git reset --hard HEAD^
      * 2.將取消掉的commit還原回來
        * git reset --hard ORIG\_HEAD

## 手動瀏覽/查找舊的commit

* 順序
  * 從commit紀錄中找到發生bug及無bug的commit -> 找出後比較兩個commit間的差異
    * 列出所有的commit

      ```
            git log               // 等義於git log HEAD
            git log master             //指定branch
            git log --pretty=short master~6..master~4   // 列出master中第4～5次的commit
            git log --all --grep="issue123"    //找出issue123的commit
      ```

      ![](https://4137043065-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4M0G8A3AbgfgoPuWWW%2F-M4M0INTQnUS90ldtPAg%2F-M4M0MYgifRSPPevj2JC%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-06-14%20%E4%B8%8B%E5%8D%887.02.28.png?generation=1586302928758438\&alt=media)
    * 顯示某個commit的內容

      ```
             git show 8244cb97d0c764bbe14c0105f298faf06826d945
      ```

      ![](https://4137043065-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4M0G8A3AbgfgoPuWWW%2F-M4M0INTQnUS90ldtPAg%2F-M4M0MYiVx8qpmrOjJlw%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-06-14%20%E4%B8%8B%E5%8D%887.05.45.png?generation=1586302928619245\&alt=media)
    * 比較兩次commit的差異

      ```
             git diff f906952b0db01941657067ab67ed1efe06530f8b..8244cb97d0c764bbe14c0105f298faf06826d945
      ```

      ![](https://4137043065-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4M0G8A3AbgfgoPuWWW%2F-M4M0INTQnUS90ldtPAg%2F-M4M0MYkRpRYlFJ_DI2J%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-06-14%20%E4%B8%8B%E5%8D%887.10.44.png?generation=1586302929074029\&alt=media)

## 修改最新的commit

* 順序
  * 1.列出原有的commit
    * git log
  * 2.將修改後的檔案加入修改
    * git add sample.txt
  * 3.將最新的commit修改為這次的commit
    * git commit --ammend

## 取消commit

* 順序
  * 1.列出原有的commit
    * git log

      ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-07-04%20%E4%B8%8B%E5%8D%886.59.39.png)
  * 2.取消特定的commit
    * git revert HEAD ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-07-04%20%E4%B8%8B%E5%8D%887.00.25.png)
    * git revert (commit) ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-07-04%20%E4%B8%8B%E5%8D%887.47.54.png)
