在論壇里看到這樣一個(gè)帖子:https://ruby-china.org/topics/26320忧勿。
剛好幾周前在公司做了一次關(guān)于git使用的分享。所以想拿出來分享下這個(gè)內(nèi)容孕暇。
關(guān)于Undoing Changes
checkout
checkout有3個(gè)作用分別是:
- checkout files
git checkout master
切換分支 - checkout commit
git checkout <commit> <file>
可以checkout某個(gè)commit下的文件并放入暫存區(qū) - checkout branch
git checkout <commit>
可以checkout某個(gè)commit蜓氨,然后可以將當(dāng)前置于某個(gè)游離的commit
整體來講checkout可以提供一個(gè)功能review commit,并且是無害的突倍。但是使用git checkout HEAD file
腔稀,(可以省略HEAD)可以清空當(dāng)前工作區(qū)的內(nèi)容。
revert
通過revert commit來回滾某個(gè)歷史commit羽历,所以是安全的焊虏。
reset
reset file,可以講暫存區(qū)的移除到工作區(qū)秕磷。
reset commit诵闭,撤銷local的某個(gè)commit,有3種模式
如果只是清除或者修改某個(gè)私有分支的未提交commit澎嚣,直接reset即可疏尿。
如果已經(jīng)提交到remote,需要強(qiáng)制push易桃。但是因?yàn)闀?huì)修改歷史commit記錄褥琐,所以對(duì)其他人會(huì)有影響,所以不建議在公共分支上做此操作晤郑!
如果需要回滾某個(gè)公有分支的commit敌呈,可以切出私有分支revert某個(gè)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以后可以很好的保證一個(gè)線性的commit歷史紀(jì)錄线脚。
但是永遠(yuǎn)不要在公有分支上rebase操作,因?yàn)闀?huì)修改歷史commit的歷史紀(jì)錄叫榕。
git的workflow
https://www.atlassian.com/git/tutorials/syncing
thoughtbot的:
https://github.com/thoughtbot/guides/blob/master/protocol/git/README.md
另外推薦一個(gè)工具:
https://github.com/aanand/git-up