在論壇里看到這樣一個帖子:https://ruby-china.org/topics/26320。
剛好幾周前在公司做了一次關(guān)于git使用的分享。所以想拿出來分享下這個內(nèi)容。
關(guān)于Undoing Changes
checkout
checkout有3個作用分別是:
- checkout files
git checkout master
切換分支 - checkout commit
git checkout <commit> <file>
可以checkout某個commit下的文件并放入暫存區(qū) - checkout branch
git checkout <commit>
可以checkout某個commit,然后可以將當前置于某個游離的commit
整體來講checkout可以提供一個功能review commit,并且是無害的。但是使用git checkout HEAD file
,(可以省略HEAD)可以清空當前工作區(qū)的內(nèi)容。
revert
通過revert commit來回滾某個歷史commit,所以是安全的。
reset
reset file,可以講暫存區(qū)的移除到工作區(qū)。
reset commit,撤銷local的某個commit,有3種模式
如果只是清除或者修改某個私有分支的未提交commit,直接reset即可。
如果已經(jīng)提交到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