目錄
- 一.如果沒有push到遠程倉庫
- 二.如果push到遠程倉庫
一.如果沒有push上去,可以用git reset 本地回滾到之前的代碼。
1.git reset 版本號和git reset --mixed 版本號(作用一致)
①查看版本,需要回滾到385ad19aa255fb977c118cb79d2752d6d6cd4fb9版本
TheEternitydeiMac:git-test admin$ git log
commit 88d6f6d056c5e51755727bc82acaaef12585e47e (HEAD -> master)
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 09:38:50 2020 +0800
測試本地commit,但是沒有push
commit 385ad19aa255fb977c118cb79d2752d6d6cd4fb9 (origin/master, origin/HEAD)
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 09:36:17 2020 +0800
排除非空文件
commit 2c9b92bd5d4c08b7c2aae9adb53412e3038fb6d8
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 09:32:22 2020 +0800
Revert "測試線上提交回滾"
"回滾測試666"
This reverts commit 5728b800ff68aed664a03a35f3c411c52e0b624f.
②回滾版本
##### 回滾操作
TheEternitydeiMac:git-test admin$ git reset 385ad19aa255fb977c118cb79d2752d6d6cd4fb9
Unstaged changes after reset:
M test.txt
③回滾后查看
##### 本地得commit提交已經回滾
TheEternitydeiMac:git-test admin$ git log
commit 385ad19aa255fb977c118cb79d2752d6d6cd4fb9 (HEAD -> master, origin/master, origin/HEAD)
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 09:36:17 2020 +0800
排除非空文件
commit 2c9b92bd5d4c08b7c2aae9adb53412e3038fb6d8
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 09:32:22 2020 +0800
Revert "測試線上提交回滾"
"回滾測試666"
This reverts commit 5728b800ff68aed664a03a35f3c411c52e0b624f.
④回滾結束之后,顯示test.txt處於編輯狀態,且沒有被提交到暫存區,test.txt編輯得內容還存在
+6TheEternitydeiMac:git-test admin$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
TheEternitydeiMac:git-test admin$ cat test.txt
666
888
測試本地commit,但是沒有push
暫存區(add/index區)回退處於沒有add的狀態, 提交區(commit區)會回退到某個版本,工作區代碼不改變。
2.git reset --soft 版本號
①查看版本,需要回滾到385ad19aa255fb977c118cb79d2752d6d6cd4fb9版本
TheEternitydeiMac:git-test admin$ git log
commit c2730d200275d0bf3ae7d1cbbc2376470858819f (HEAD -> master)
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 10:01:27 2020 +0800
測試本地commit,但是沒有push
commit 385ad19aa255fb977c118cb79d2752d6d6cd4fb9 (origin/master, origin/HEAD)
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 09:36:17 2020 +0800
排除非空文件
commit 2c9b92bd5d4c08b7c2aae9adb53412e3038fb6d8
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 09:32:22 2020 +0800
Revert "測試線上提交回滾"
"回滾測試666"
This reverts commit 5728b800ff68aed664a03a35f3c411c52e0b624f.
②回滾版本
TheEternitydeiMac:git-test admin$ git reset --soft 385ad19aa255fb977c118cb79d2752d6d6cd4fb9
Unstaged changes after reset:
M test.txt
③回滾后查看
TheEternitydeiMac:git-test admin$ git log
commit 385ad19aa255fb977c118cb79d2752d6d6cd4fb9 (HEAD -> master, origin/master, origin/HEAD)
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 09:36:17 2020 +0800
排除非空文件
commit 2c9b92bd5d4c08b7c2aae9adb53412e3038fb6d8
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 09:32:22 2020 +0800
Revert "測試線上提交回滾"
"回滾測試666"
This reverts commit 5728b800ff68aed664a03a35f3c411c52e0b624f.
④回滾結束之后,顯示test.txt被提交到暫存區,test.txt編輯得內容還存在
TheEternitydeiMac:git-test admin$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.txt
TheEternitydeiMac:git-test admin$ cat test.txt
666
888
測試本地commit,但是沒有push
暫存區(add/index區)沒有回退還是add狀態, 提交區(commit區)會回退到某個版本,工作區代碼不改變。
3.git reset --hard 版本號
①查看版本,需要回滾到385ad19aa255fb977c118cb79d2752d6d6cd4fb9版本
TheEternitydeiMac:git-test admin$ git log
commit fa1e2d18892e6554564f8c8830c85b04b09a1e4f (HEAD -> master)
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 10:15:03 2020 +0800
測試本地commit,但是沒有push
commit 385ad19aa255fb977c118cb79d2752d6d6cd4fb9 (origin/master, origin/HEAD)
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 09:36:17 2020 +0800
排除非空文件
commit 2c9b92bd5d4c08b7c2aae9adb53412e3038fb6d8
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 09:32:22 2020 +0800
Revert "測試線上提交回滾"
"回滾測試666"
This reverts commit 5728b800ff68aed664a03a35f3c411c52e0b624f.
②回滾版本
TheEternitydeiMac:git-test admin$ git reset --hard 385ad19aa255fb977c118cb79d2752d6d6cd4fb9
HEAD is now at 385ad19 排除非空文件
③回滾后查看
TheEternitydeiMac:git-test admin$ git log
commit 385ad19aa255fb977c118cb79d2752d6d6cd4fb9 (HEAD -> master, origin/master, origin/HEAD)
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 09:36:17 2020 +0800
排除非空文件
commit 2c9b92bd5d4c08b7c2aae9adb53412e3038fb6d8
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 09:32:22 2020 +0800
Revert "測試線上提交回滾"
"回滾測試666"
This reverts commit 5728b800ff68aed664a03a35f3c411c52e0b624f.
④回滾結束之后,暫存區被清空,提交區(commit)回退了,工作區代碼也沒了
TheEternitydeiMac:git-test admin$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
TheEternitydeiMac:git-test admin$ cat test.txt
666
888
暫存區(add/index區)回退, 提交區(commit區)會回退到某個版本,工作區代碼沒有了,恢復到回退得版本。
4.匯總
①.git reset 版本號和git reset --mixed 版本號
暫存區(add/index區)回退處於沒有add的狀態, 提交區(commit區)會回退到某個版本,工作區代碼不改變。
②.git reset --soft 版本號
暫存區(add/index區)沒有回退還是add狀態, 提交區(commit區)會回退到某個版本,工作區代碼不改變。
③.git reset --hard 版本號
暫存區(add/index區)回退, 提交區(commit區)會回退到某個版本,工作區代碼沒有了,恢復到回退得版本。
二.如果push上去了,已經覆蓋了線上的代碼
1.git revert
①查看log,把add second這次提交回滾
要回滾哪次提交,就revert哪次提交. 我要回滾add second這次提交,就是revert 'add second'這個的版本號
TheEternitydeiMac:git-test admin$ git log
commit 133da5f8fb3809f6cf5d0697433f951d321460a3 (HEAD -> master, origin/master, origin/HEAD)
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 10:49:52 2020 +0800
add second
commit bc4d97a2b30b2a22dc8dc216ed308f08034d62a8
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 10:49:30 2020 +0800
add first
commit 82018f13c7ea90d2596cb75069bc2dd7650e0c4a
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 10:49:03 2020 +0800
test
TheEternitydeiMac:git-test admin$ cat z.txt
test1
test2
test3
test4
test5
add first
add second
②回滾
TheEternitydeiMac:git-test admin$ git revert 133da5f8fb3809f6cf5d0697433f951d321460a3
[master 0b3ee43] Revert "add second" --- revert to 'add first',delete 'add second' modifiy --- This reverts commit 133da5f8fb3809f6cf5d0697433f951d321460a3.
1 file changed, 1 insertion(+), 3 deletions(-)
③查看狀態,已經回滾到add first提交時得狀態
TheEternitydeiMac:git-test admin$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
TheEternitydeiMac:git-test admin$ cat z.txt
test1
test2
test3
test4
test5
add first
④推送,完成回滾
TheEternitydeiMac:git-test admin$ git push -u origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 344 bytes | 344.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/eternityz/git-test.git
133da5f..0b3ee43 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
2.git reset --hard 和git push
①查看log
TheEternitydeiMac:git-test admin$ git log
commit 84315891067943aa7fb1d0b858dc7b95dbfcdc55 (HEAD -> master, origin/master, origin/HEAD)
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 11:15:46 2020 +0800
git reset hard test second
commit 45f5c01d0dd4cb83f25bc1ea7b82b0348600a041
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 11:15:23 2020 +0800
git reset hard test first
commit 0b3ee438ae44192bb3d38887be6fb25c2169aea6
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 11:04:01 2020 +0800
Revert "add second"
---
revert to 'add first',delete 'add second' modifiy
---
This reverts commit 133da5f8fb3809f6cf5d0697433f951d321460a3.
②reset --hard 到 'git reset hard test first' 版本
TheEternitydeiMac:git-test admin$ git reset --hard 45f5c01d0dd4cb83f25bc1ea7b82b0348600a041
HEAD is now at 45f5c01 git reset hard test first
TheEternitydeiMac:git-test admin$ cat a.txt
git reset hard test first
③push到遠程
正常推送會被拒絕[git push -u origin master]
TheEternitydeiMac:git-test admin$ git push -u origin master
To https://gitee.com/eternityz/git-test.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/eternityz/git-test.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
需要強制推送[git push -u -f origin master]
TheEternitydeiMac:git-test admin$ git push -u -f origin master
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/eternityz/git-test.git
+ 8431589...45f5c01 master -> master (forced update)
Branch 'master' set up to track remote branch 'master' from 'origin'.
④查看推送的結果,完成了遠端的回滾
TheEternitydeiMac:git-test admin$ git log
commit 45f5c01d0dd4cb83f25bc1ea7b82b0348600a041 (HEAD -> master, origin/master, origin/HEAD)
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 11:15:23 2020 +0800
git reset hard test first
commit 0b3ee438ae44192bb3d38887be6fb25c2169aea6
Author: eternity <zhangh0725@gmail.com>
Date: Tue Jun 23 11:04:01 2020 +0800
Revert "add second"
---
revert to 'add first',delete 'add second' modifiy
---
This reverts commit 133da5f8fb3809f6cf5d0697433f951d321460a3.