首先介紹下場景
首先,一定要想清楚,自己想干什么。
找redis源碼作為例子,查看所在的分支git branch
3.0 3.2 * unstable
取前5條commit看看
git log --pretty=format:"%h - %an, %ar : %s" -5
e9d861e - antirez, 27 hours ago : Clear child data when opening the pipes. e565632 - antirez, 27 hours ago : Child -> Parent pipe for COW info transferring. e1eccf9 - antirez, 31 hours ago : zmalloc: Make fp var non local to fix build. 945a2f9 - antirez, 31 hours ago : zmalloc: zmalloc_get_smap_bytes_by_field() modified to work for any PID. b13759e - antirez, 31 hours ago : redis-cli: "allocator-stats" -> "malloc-stats".
臨時切換到某個commit
有可能你并不需要回滾代碼,你只想將代碼庫暫時切換到之前的某個版本,看看那個時候的實(shí)現(xiàn),然后回來繼續(xù)寫之前沒有完成的任務(wù)。比如想看看945a2f9這個commit
,執(zhí)行操作
git checkout 945a2f9
Note: checking out '945a2f9'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b new-branch-name> HEAD is now at 945a2f9... zmalloc: zmalloc_get_smap_bytes_by_field() modified to work for any PID.
再執(zhí)行git branch
* (HEAD detached at 945a2f9) 3.0 3.2 unstable
可以看到處于一個臨時的分支上面,如果想以這個分支作為基礎(chǔ)開發(fā)新的功能,可以創(chuàng)建一個新的分支進(jìn)行開發(fā)。
git checkout -b old-bottle 945a2f9
如果想回到之前的的分支,只要checkout
回去就好了。
當(dāng)然如果在切回以前的commit
的之前已經(jīng)改了一些代碼,可能不會那么順利地切到之前的commit
,可以
使用reset
將修改了的代碼丟棄
stash->checkout->stash pop
將修改的代碼找回來
生成一個新的分支用于保存修改的代碼
刪除未發(fā)布的提交
如果確定要刪除某幾個commit
,并且那幾個commit
的代碼還沒有提交到遠(yuǎn)程版本庫里的話。
可以這樣
git reset --hard 945a2f9 HEAD is now at 945a2f9 zmalloc: zmalloc_get_smap_bytes_by_field() modified to work for any PID.
再查看git log,就已經(jīng)看不到945a2f9之前的commit
記錄了。
當(dāng)然如果想保存當(dāng)前沒有提交的代碼,可以和之前一樣使用stash
git stash git reset --hard 945a2f9 git stash pop
注意:
reset --hard 一定要慎用??!
這回讓你徹底丟失本地的代碼(除非有備份過)
刪除已發(fā)布的提交
假設(shè)代碼已經(jīng)提交到遠(yuǎn)程版本庫了,這時應(yīng)該使用revert
命令,revert
命令實(shí)際是提交一個新的commit
來回滾之前的commit
。還是以945a2f9為例,現(xiàn)在想讓代碼會滾到945a2f9的狀態(tài),那么需要revert
掉之前的commit
git revert e9d861e e565632 e1eccf9
或者可以這樣做,revert
掉HEAD的前三個commit
git revert HEAD~3..HEAD
之后就可以將現(xiàn)在的HEAD push到版本庫了。
如果發(fā)現(xiàn)之前revert
操作錯了,可以再執(zhí)行一次revert
來取消上次的revert
。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,本文只是簡單講了一些我個人的一些操作實(shí)踐。希望能對大家的學(xué)習(xí)或者工作帶來一定的幫助,如果有疑問大家可以留言交流。
標(biāo)簽:日照 巴彥淖爾 防城港 96 山南 內(nèi)蒙古 定州 宜賓
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《簡單談?wù)凣it中的回滾操作》,本文關(guān)鍵詞 簡單,談?wù)?Git,中的,回滾,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。