GIT 3 撤销

入库撤销操作

git reset

使用gitsvn很大的一个不同,是在开发过程中先在本地进行一系列的入库(仅commitpush),待整个功能确认完成后,一次性推送到远端库。当然在本地库操作过程中,不可避免地会碰到需要撤销本地库入库的情况。

git reset撤销stagelocal repo的修改

使用git reset撤销commit,保留本地文件的修改和add的信息。

假设本地库中在删除delete a2.txt之后,保留了实现feature1.1/feature1.2/feature1.3的三次入库信息:

[smbtest@localhost a]$ git log

commit 8fd3cf3da8dcf0663150b29b4b7d844e83af097c

    feature 1.3

commit 644ae1487dfbb6b81aae19049af0a97b93baa52e

    feature 1.2

commit a37a3c5f6844aac058530f657c540221ef36807e

    feature 1.1

commit 3764a36206944735ea3ee461c254e4c194b160ce

delete a2.txt

使用git reset [3764a36206944735ea3ee461c254e4c194b160ce],可以将commit信息回退到delete a2.txt时的状态——log最后一条是“delete a.txt”,status中的a.txt的修改还没有加入到stage

[smbtest@localhost a]$ git log

commit 3764a36206944735ea3ee461c254e4c194b160ce

    delete a2.txt

 [smbtest@localhost a]$ git status

# On branch master

# Changes not staged for commit:

#   (use "git add <file>..." to update what will be committed)

#   (use "git checkout -- <file>..." to discard changes in working directory)

#

#       modified:   a.txt

#

no changes added to commit (use "git add" and/or "git commit -a")

然后按照基本入库步骤将feature1.1/feature1.2/feature1.3作为function1一次commit即可

[smbtest@localhost a]$ git add a.txt

[smbtest@localhost a]$ git commit -m "function 1"

[master 5ec3455] function 1

 1 file changed, 5 insertions(+)

[smbtest@localhost a]$ git log

commit 5ec3455dae0c02537c2d9ff1e001b5da97e71d9a

    function 1

git reset--soft撤销local repo的修改

假设入库function1以后,又有两次commitfeature2.1feature2.2

[smbtest@localhost a]$ git log -3

commit 40e8c12abff40aba66d8fa6ed4970a93890d755a

    feature 2.2

commit f0aac7525f6cb0be069662b0b3c52e8edf411a62

    feature 2.1

commit 5ec3455dae0c02537c2d9ff1e001b5da97e71d9a

    function 1

使用git reset –soft,撤销最近的两次commit操作

[smbtest@localhost a]$ git reset --soft 5ec3455dae0c02537c2d9ff1e001b5da97e71d9a

 [smbtest@localhost a]$ git log -1

commit 5ec3455dae0c02537c2d9ff1e001b5da97e71d9a

    function 1

 [smbtest@localhost a]$ git status

# On branch master

# Changes to be committed:

#   (use "git reset HEAD <file>..." to unstage)

#

#       modified:   a.txt

#

可以看到git log已经删除了最近两次commit,但是status内还保留了add a.txt的操作。之后入库只需直接commit即可。

git reset –hard撤销本地文件、statgelocal repo的修改

git reset –hard不再赘述,其特征是本地文件也将恢复成指定HEAD的状态(所做修改被丢弃),慎用!

git revert

git revert将指定commit所包含的修改回退后再commit,从而撤销该次入库,其顺利执行的前提是涉及文件与其他commit不存在冲突。

[smbtest@localhost a]$ git log

commit c4b214aff15850d17b386b10cf2cd5e855f27f33

    feature 2.3

commit 7e42cf823c6efe22dee4382a847a744914572e35

    feature 2.2

commit cd342b2acc8bb7f4ca5f18900ea4526aa307618e

    feature 2.1

[smbtest@localhost a]$ git revert 7e42cf823c6efe22dee4382a847a744914572e35

[master 04d7880] Revert "feature 2.2"

 0 files changed

 delete mode 100644 feature2.2

 [smbtest@localhost a]$ git log

commit 04d788084fcfb3bbc79caa47350d7edd2d6e50a6

    Revert "feature 2.2"

    This reverts commit 7e42cf823c6efe22dee4382a847a744914572e35.

commit c4b214aff15850d17b386b10cf2cd5e855f27f33

    feature 2.3

commit 7e42cf823c6efe22dee4382a847a744914572e35

    feature 2.2

commit cd342b2acc8bb7f4ca5f18900ea4526aa307618e

feature 2.1

小结

git resetgit revert可以达到同样的效果,git reset主要用于push到远端库之前对本地文件和commit进行调整;git revert通过增加一次新的入库,恢复指定入库的修改,相对来说比较方便,但是需要避免冲突。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值