Git——分支——其他

介紹reset,revert,cherry-pick三個指令。

1、reset

  git reset adjusts the HEAD ref to a given commit and, by default, updates the index to match that commit, If desired, git reset can also modify your working directory to mirror the revision of your project represented by the given commit

reset命令可以改變HEAD引用,INDEX,工作目錄。

  它有四種指令格式:

git reset [-q] [<tree-ish>] [--] <pathspec>…
git reset [-q] [--pathspec-from-file=<file> [--pathspec-file-nul]] [<tree-ish>]
git reset (--patch | -p) [<tree-ish>] [--] [<pathspec>…]
git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]

  前三種格式,指定還原特定文件,而不是整個工作區。當有—hard選項時,它類似於checkout時,還原整個工作區。

  選項:

      q:全稱爲quiet,無意義。

   pathsec, pathspc-from-file:指定還原哪些文件,參數形式,或者從文件中讀取這些參數

   merge:嘗試合併HEAD引用的commit和reset參數中的commit。

   keep:嘗試將文件或工作區回退到reset參數中的commit,若文件中存在變化(指針對Head引用的commit,有修改),reset失敗。 

   hard,soft,mixed, 三者的功能如下:

Option

HEAD

INDEX

工作目錄

--soft

--mixed

--hard

  示例:

         git reset --hard HEAD。

  注意:當reset之後,HEAD引用會關聯到某個commit對象,再次提交時會覆蓋後續commit, 後續commit對象變爲不可達,被gc回收。

2、cherry-pick

  The command git cherry-pick commit applies the changes introduced by the named commit on the current branch

複製其他分支上的單個提交對象,並應用到當前分支。

格式:

git cherry-pick [--edit] [-n] [-m parent-number] [-s] [-x] [--ff] [-S[<keyid>]] <commit>…
git cherry-pick (--continue | --skip | --abort | --quit)

  選項:

    edit選項允許重新編輯提交日誌信息,而不是複用之前的提交日誌。

    n全稱爲no-commit,默認情況下,執行cherry-pick,會在當前分支創建一個完全相同的提交對象,指定no-commit對象之後,只是把變更應用到最近的提交對象。

    m全稱爲merge, 合併通常會有多個父提交,後面跟parent-number指定複用的父提交。

    x,給其他分支,被cherry-pick的提交添加一條備註,可以追溯它被哪個分支複用(cherry-pick)

    ff,全稱爲fast forward,當前的HEAD在被複制的commit的提交歷史記錄中,則快進。

    s:全稱爲signature,簽名。

    commit:被複制的commit對象。

  示例:

         git cherry-pick commit標識(引用, tag, id)。

  注意:複製時通常需要保證文件的正確性,例如A提交中有a, b文件。B提交中再次修改b文件,此時複製B提交時,若b文件中引用a,則會發生錯誤。

3、revert

  it applies the inverse of the given commit

創建新提交,完全抵消某個提交的變更集。通常很少使用revert命令,因爲通常都抵消最近的一次的提交,reset命令更適合。若想抵消提交歷史上某個提交,使用revert。

  格式:

git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[<keyid>]] <commit>…
git revert (--continue | --skip | --abort | --quit)

  選項:

      與cherry-pick的選項含義完全相同,略。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章