git回滚线上代码(命令行)

目录

  • 一.如果没有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.

posted @ 2020-06-28 10:27  未月廿三  阅读(3755)  评论(0编辑  收藏  举报