国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

簡單談談Git中的回滾操作

2024-07-21 02:02:48
字體:
供稿:網(wǎng)友

首先介紹下場景

首先,一定要想清楚,自己想干什么。

找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

有可能你并不需要回滾代碼,你只想將代碼庫暫時切換到之前的某個版本,看看那個時候的實現(xiàn),然后回來繼續(xù)寫之前沒有完成的任務。比如想看看945a2f9這個commit,執(zhí)行操作

git checkout 945a2f9
Note: checking out '945a2f9'.You are in 'detached HEAD' state. You can look around, make experimentalchanges and commit them, and you can discard any commits you make in thisstate without impacting any branches by performing another checkout.If you want to create a new branch to retain commits you create, you maydo 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)建一個新的分支進行開發(fā)。

git checkout -b old-bottle 945a2f9

如果想回到之前的的分支,只要checkout回去就好了。

當然如果在切回以前的commit的之前已經(jīng)改了一些代碼,可能不會那么順利地切到之前的commit,可以

     使用reset將修改了的代碼丟棄

     stash->checkout->stash pop將修改的代碼找回來

     生成一個新的分支用于保存修改的代碼

刪除未發(fā)布的提交

如果確定要刪除某幾個commit,并且那幾個commit的代碼還沒有提交到遠程版本庫里的話。

可以這樣

git reset --hard 945a2f9HEAD is now at 945a2f9 zmalloc: zmalloc_get_smap_bytes_by_field() modified to work for any PID.

再查看git log,就已經(jīng)看不到945a2f9之前的commit記錄了。

當然如果想保存當前沒有提交的代碼,可以和之前一樣使用stash

git stashgit reset --hard 945a2f9git stash pop

注意:

reset --hard 一定要慎用??!

這回讓你徹底丟失本地的代碼(除非有備份過)

刪除已發(fā)布的提交

假設代碼已經(jīng)提交到遠程版本庫了,這時應該使用revert命令,revert命令實際是提交一個新的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)容了,本文只是簡單講了一些我個人的一些操作實踐。希望能對大家的學習或者工作帶來一定的幫助,如果有疑問大家可以留言交流。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 平安县| 常德市| 襄汾县| 张掖市| 盱眙县| 武邑县| 汝州市| 交口县| 越西县| 盘山县| 佛山市| 正镶白旗| 多伦县| 嘉禾县| 美姑县| 怀仁县| 奎屯市| 黔西县| 青川县| 焦作市| 巍山| 远安县| 武穴市| 年辖:市辖区| 文化| 宜春市| 六盘水市| 南城县| 电白县| 元阳县| 盐津县| 荆门市| 临汾市| 大安市| 文山县| 福海县| 静海县| 绍兴县| 东源县| 渝北区| 枣阳市|