Undoing Changes

2015-07-18  本文已影响27人  orcl_zhang

在论坛里看到这样一个帖子:https://ruby-china.org/topics/26320
刚好几周前在公司做了一次关于git使用的分享。所以想拿出来分享下这个内容。

关于Undoing Changes

checkout

checkout有3个作用分别是:

  1. checkout files
    git checkout master切换分支
  2. checkout commit
    git checkout <commit> <file>可以checkout某个commit下的文件并放入暂存区
  3. checkout branch
    git checkout <commit>可以checkout某个commit,然后可以将当前置于某个游离的commit

整体来讲checkout可以提供一个功能review commit,并且是无害的。但是使用git checkout HEAD file,(可以省略HEAD)可以清空当前工作区的内容。

revert

通过revert commit来回滚某个历史commit,所以是安全的。

reset

reset file,可以讲暂存区的移除到工作区。
reset commit,撤销local的某个commit,有3种模式
如果只是清除或者修改某个私有分支的未提交commit,直接reset即可。
如果已经提交到remote,需要强制push。但是因为会修改历史commit记录,所以对其他人会有影响,所以不建议在公共分支上做此操作!
如果需要回滚某个公有分支的commit,可以切出私有分支revert某个commit,然后提交pr。

clean

清除Unstaging a File

参考这里:
https://www.atlassian.com/git/tutorials/undoing-changes
https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting

另外还有两篇:
https://www.atlassian.com/git/tutorials/rewriting-history
https://www.atlassian.com/git/tutorials/merging-vs-rebasing
主要介绍merge和rebase。一般用rebase -i来整理私有分支上的commit。
在私有分支上rebase其他分支来引入其他分支的commit,并且merge以后可以很好的保证一个线性的commit历史纪录。
但是永远不要在公有分支上rebase操作,因为会修改历史commit的历史纪录

git的workflow
https://www.atlassian.com/git/tutorials/syncing
thoughtbot的:
https://github.com/thoughtbot/guides/blob/master/protocol/git/README.md

另外推荐一个工具:
https://github.com/aanand/git-up

上一篇下一篇

猜你喜欢

热点阅读