git代碼版本管理(1)——git版本回滾
一、問題背景
在利用github、gitlab、Gitee等代碼管理器中對代碼的管理,咱們有時會出現錯誤提交的狀況,此時咱們但願能撤銷提交操做,讓程序回到提交以前的樣子。
本文總結了兩種解決方法:回退(reset)、反作(revert)。
push前與push後。
二、git提交小知識
使用git的每次提交,Git都會自動把它們串成一條時間線,這條時間線就是一個分支。若是沒有新建分支,那麼只有一條時間線,即只有一個分支,在Git裏,這個分支叫主分支,即master分支。有一個HEAD指針指向當前分支(只有一個分支的狀況下會指向master,而master是指向最新提交)。每一個版本都會有本身的版本信息,如特有的版本號、版本名等。以下圖,假設只有一個分支:每次提交都會生成一次提交的ID號(此ID號惟一)(以下圖)
三、解決方式
3.1 尚未push推送到遠端、只執行了本地commit提交
[root@db test]# ls # 當前本地倉庫下的文件
README.md 第一次提交.txt 第二次提交.txt
[root@db test]# touch 第三次提交.txt # 新建一個文件
[root@db test]# git add .
[root@db test]# git commit -m '3' # 本地提交
[master aee4c30] 3
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 "\347\254\254\344\270\211\346\254\241\346\217\220\344\272\24 4.txt"
[root@db test]# git log # 查看全部提交信息
commit aee4c30c17b5c916249bdcc180d3f9a912222198
Author: test <test@example.com>
Date: Mon Oct 12 16:25:02 2020 +0800
3
commit fa8f987d9ac5f37b7e77cbf519825b712ee3b03a
Author: test <test@example.com>
Date: Mon Oct 12 16:13:31 2020 +0800
2
commit ccedae5cc29205f44c2164b8f541bd28dd1197ed
Author: test <test@example.com>
Date: Mon Oct 12 16:12:49 2020 +0800
1
commit 729fd8eaa8f7ec30784bd2cdafbcc262d2f9f039
Author: test <test@example.com>
Date: Mon Oct 12 16:11:33 2020 +0800
add README
[root@db test]# git reset --hard fa8f987d9ac5f37b7e77cbf519825b712ee3b03a # 回滾到指定版本(我這裏是回滾到上一個提交的commit_id)
HEAD is now at fa8f987 2
[root@db test]# ls # 再次查看
README.md 第一次提交.txt 第二次提交.txt
[root@db test]#
[root@db test]# git log # 再次查看全部提交信息發現回滾成功
commit fa8f987d9ac5f37b7e77cbf519825b712ee3b03a
Author: test <test@example.com>
Date: Mon Oct 12 16:13:31 2020 +0800
2
commit ccedae5cc29205f44c2164b8f541bd28dd1197ed
Author: test <test@example.com>
Date: Mon Oct 12 16:12:49 2020 +0800
1
commit 729fd8eaa8f7ec30784bd2cdafbcc262d2f9f039
Author: test <test@example.com>
Date: Mon Oct 12 16:11:33 2020 +0800
add README
3.2 push推送至遠端,使用 reset 回滾
3.2.1適用場景: nginx
若是想恢復到以前某個提交的版本,且那個版本以後提交的版本咱們都不要了,就能夠用這種方法。
3.2.2 reset 回滾原理:git
git reset的做用是修改HEAD的位置,即將HEAD指向的位置改變爲以前存在的某個版本,不會保存此版本後的提交,以下圖
3.2.3開始執行回滾:github
①. 查看版本號:sql
git log
②. 使用「git reset --hard 目標版本號」命令將版本回退:shell
git reset --hard ccedae5cc29205f44c2164b8f541bd28dd1197ed
③.使用「git push -f」提交更改:
bash
git push -f
④.驗證:
微信
至此使用reset回退成功!
3.3 push推送至遠端,使用 revert回滾
3.3.1 適用場景:運維
若是咱們想撤銷以前的某一版本,可是又想保留該目標版本後面的版本,記錄下這整個版本變更流程,就能夠用這種方法。
3.3.2 revert回滾原理:ide
git revert的做用是把以前提交的版本做爲一個新版本再次提交(提交位置爲當前版本的後一個位置,如想要回滾到版本二,當前爲版本三,則從新提交的爲版本四,不會像reset刪除回滾以後的版本,只會還原版本二所提交的內容),以達到撤銷該版本的修改的目的。以下圖所示:
3.2.3開始執行回滾:
gitlab
①. 查看版本號:
git log
②.使用「git revert -n 版本號」反作,並使用「git commit -m 版本名」提交:
git revert -n ccedae5cc29205f44c2164b8f541bd28dd1197edgit commit -m "revert 1 or commit 4"
③.使用「git push」提交更改:
git push
④.驗證:
一、但是用 git log查看二、能夠在圖形化頁面查看
反作成功!
四、其它值得注意的參數(本文回滾用的是hard)
git reset --hard HEAD^ 回退到上個版本git reset --hard HEAD~3 回退到前3次提交以前,以此類推,回退到n次提交以前git reset --hard commit_id 退到/進到 指定commit的sha碼git push origin HEAD --force 強推命令
git reset --mixed 此爲默認方式,不帶任何參數的git reset,即時這種方式,它回退到某個版本,只保留源碼,回退commit和index信息git reset --soft 回退到某個版本,只回退了commit的信息,不會恢復到index file一級。若是還要提交,直接commit便可git reset --hard 完全回退到某個版本,本地的源碼也會變爲上一個版本的內容
本文分享自微信公衆號 - 小偉運維開發(xiaowei-dev)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。