持續過程改進

git 中如何操作回退呢?


情景1     回退 git add ;

1
2
3
4
5
$ edit                                              <1>
$ git add frotz.c filfre.c
$ mailx                                             <2>
$ git reset                                         <3>
$ git pull git://info.example.com/ nitfol           <4>

       

如果你把frotz.c 和filfre.c 添加到緩存區,此時需要執行pull的合併,但這兩個文件不是需要合併的,你可以執行第四步回退git add;


情景2  回退git commit,並重新提交;

1
2
3
4
$ git commit ...
$ git reset --soft HEAD^      <1>
$ edit                        <2>
$ git commit -a -c ORIG_HEAD  <3>

如果提交不完整或者想修改提交日誌,那麼執行第二步,就可以將工作目錄恢復到提交前,再你重新修改後,執行第三步,  執行reset後,上次提交被轉移到 ORIG_HEAD, 如果你不想修改提交日誌,選擇 -c 即可。


情景3 創建分支,剔除在主幹的提交


1
2
3
$ git branch topic/wip     <1>
$ git reset --hard HEAD~3  <2>
$ git checkout topic/wip   <3>

在主幹修改,發現對主幹有影響,創建分支 topic/wip, 在主幹剔除過去的3次提交,回到分支;


情景4  刪除過去的git commit

1
2
$ git commit ...
$ git reset --hard HEAD~3   <1>

把倒數第三次提交給刪除了(HEAD, HEAD^, and HEAD~2),執行第1步操作。


情景5  回退一次合併或者 pull操作

1
2
3
4
5
6
7
8
9
$ git pull                         <1>
Auto-merging nitfol
CONFLICT (content): Merge conflict in nitfol
Automatic merge failed; fix conflicts and then commit the result.
$ git reset --hard                 <2>
$ git pull . topic/branch          <3>
Updating from 41223... to 13134...
Fast-forward
$ git reset --hard ORIG_HEAD       <4>

情景6 Undo a merge or pull inside a dirty working tree

1
2
3
4
5
6
$ git pull                         <1>
Auto-merging nitfol
Merge made by recursive.
 nitfol                |   20 +++++----
 ...
$ git reset --merge ORIG_HEAD      <2>


Interrupted workflow

Suppose you are interrupted by an urgent fix request while you are in the middle of a large change.  The files in your working tree are not in any shape to be committed yet, but you need to get to the other branch for a quick bugfix.

1
2
3
4
5
6
7
8
9
$ git checkout feature ;# you were working in "feature" branch and
$ work work work       ;# got interrupted
$ git commit -a -m "snapshot WIP"                 <1>
$ git checkout master
$ fix fix fix
$ git commit ;# commit with real log
$ git checkout feature
$ git reset --soft HEAD^ ;# go back to WIP state  <2>
$ git reset                                       <3>

This commit will get blown away so a throw-away log message is OK.

This removes the WIPcommit from the commit history, and sets your working tree to the state just before you made that snapshot.

At this point the index file still has all the WIP changes you committed as snapshot WIP.  This updates the index to show your WIP files as uncommitted.

See also git-stash


Reset a single file in the index

Suppose you have added a file to your index, but later decide you do not want to add it to your commit. You can remove the file from the index while keeping your changes with git reset.


1
2
3
$ git reset -- frotz.c                      <1>
$ git commit -m "Commit files in index"     <2>
$ git add frotz.c                           <3>


This removes the file from the index while keeping it in the working directory.

This commits all other changes in the index.

Adds the file to the index again.


Keep changes in working tree while discarding some previous commits

Suppose you are working on something and you commit it, and then you continue working a bit more, but now you think that what you have in your working tree should be in another branch that has nothing to do with what you committed previously. You can start a new branch and reset it while keeping the changes in your working tree.

1
2
3
4
5
6
7
$ git tag start
$ git checkout -b branch1
$ edit
$ git commit ...                            <1>
$ edit
$ git checkout -b branch2                   <2>
$ git reset --keep start                    <3>

This commits your first edits in branch1.

In the ideal world, you could have realized that the earlier commit did not belong to the new topic when you created and switched to branch2 (i.e. "git checkout -b branch2 start"), but nobody is perfect.

But you can use "reset --keep" to remove the unwanted commit after you switched to "branch2".






情景1:本地修改後,回退還沒有緩存的文件(沒有執行$git add a.txt)

 

$vim a.txt

$git checkout  a.txt

 

 

情景2  本地修改後,回退已經提交的文件 (已經執行 $git  commit -am "message" )

$vim a.txt

$git commit -am "message" a.txt

$git reset --hard HEAD~1


這是,a.txt 的這次提交已經被取消了, 這是的狀態是a.txt 剛被修改的狀態,可以用

$git checkout a.txt   回到a.txt 被修改前的狀態。

 

情景3 將a.txt 文件回退到某個版本

$git log a.txt

選擇回退的SHA1值,

$git checkout SHA1 a.txt

此時a.txt 是 SHA1時的文件內容 

發佈了7 篇原創文章 · 獲贊 7 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章