Git Undoing Changes

rubyu2 · 2015年07月10日 · 最后由 hulajesus 回复于 2015年09月09日 · 8069 次阅读

在论坛里看到这样一个帖子: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-historyhttps://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

试了下

git checkout <commit> <file> 

只放入工作区,不会自动放入暂存区

git checkout HEAD file

同时也会清空暂存区

#1 楼 @hulajesus

git checkout <commit> <file> 

是暂存区吧?你用git diff看下就知道了。只有用git diff --cached能看到暂存区的内容。

git checkout HEAD file 同时也会清空暂存区

是的。

git checkout

@rubyu2 试验方法错了,确实可以

需要 登录 后方可回复, 如果你还没有账号请 注册新账号