持续过程改进

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时的文件内容 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值