git的撤销与回滚【转载】

git的撤销与回滚

转载文章https://www.cnblogs.com/songgj/p/8965580.html

git的撤销与回滚在平时使用中还是比较多的,比如说我们想将某个修改后的文件撤销到上一个版本,或者是想撤销某次多余的提交,都要用到git的撤销和回滚操作。撤销分两种情况,一个是commit之前,一个是commit之后,下面具体看下这两种情况。

一.git commit之前

未添加到暂存区的撤销(没有git add)
添加进暂存区的撤销(git add后)

On branch test_git
Changes not staged for commit:     没有添加到暂存区
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)
    modified: new_src/app/Http/Controllers/Frontend/KyHome/KyHomeUnitTrait.php
    modified: new_src/app/Http/Controllers/Frontend/KyHome/KyHomeWordFilterTrait.php

可以通过

git checkout -- filename来撤销修改
git checkout -- new_src/app/Http/Controllers/Frontend/KyHome/KyHomeWordFilterTrait.php

再次查看文件状态发现选择的文件已经成功被撤销了。

$ git status
On branch test_git
Changes not staged for commit:  
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)
    modified: new_src/app/Http/Controllers/Frontend/KyHome/KyHomeUnitTrait.php

如果想将多个文件一次性撤销可以用

 git checkout -- .

上面是 未添加到暂存区的撤销

下面是添加到暂存区的

$ git status
On branch test_git
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   new_src/app/Http/Controllers/Frontend/KyHome/KyHomeApp.php
        modified:   new_src/app/Http/Controllers/Frontend/KyHome/KyHomeComment.php
$ git add -A
$ git status
On branch test_git
Changes to be committed:    已经添加暂存区了
  (use "git reset HEAD ..." to unstage)

        modified:   new_src/app/Http/Controllers/Frontend/KyHome/KyHomeApp.php
        modified:   new_src/app/Http/Controllers/Frontend/KyHome/KyHomeComment.php

从暂存区撤销

$ git reset HEAD new_src/app/Http/Controllers/Frontend/KyHome/KyHomeApp.php
Unstaged changes after reset:
M       new_src/app/Http/Controllers/Frontend/KyHome/KyHomeApp.php

如果想一次性将所有暂存区文件撤销回来,还是上面的命令,不过不用加文件路径。

$ git reset HEAD
Unstaged changes after reset:

M       new_src/app/Http/Controllers/Frontend/KyHome/KyHomeApp.php
M       new_src/app/Http/Controllers/Frontend/KyHome/KyHomeComment.php

二.git commit之后

如果当commit提交后想撤销的话,这就需要revert命令。git revert 命令是撤销某次操作,而在此次操作之前和之后的提交记录都会保留。

先修改了几个文件然后commit 再用git log查看提交记录。

commit 2842c8065322085c31fb7b8207b6296047c4ea3
Author: songguojun 
Date:   Sat Apr 28 11:21:30 2018 +0800

    add content

然后使用revert 后面跟上git提交的commitid

git  revert 2842c8065322085c31fb7b8207b6296047c4ea3
Revert "add content"

This reverts commit 2842c8065ffe2085c31fb7b8207b6296047c4ea3.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch test_git
# Changes to be committed:
#       modified:   new_src/app/Http/Controllers/Frontend/KyHome/KyHomeApp.php
#       modified:   new_src/app/Http/Controllers/Frontend/KyHome/KyHomeComment.php

然后在推送到远端更新远程仓库代码,修改的文件就撤销回来了。注意的是revert奇数次生效,偶数次又回到之前的修改状态。比如一个文件内容是a,那么修改为ab,revert后文件变成了a,如果在revert后文件又还原成ab了。

还有就是如果想回到之前某个版本,可以用reset命令,可以回退到某次提交,那该提交之后的提交都会回滚,不过这种覆盖是不可逆的,之前的提交记录都没有了。所以平时开发中尽量注意,避免使用reset。

用法:

git  reset --hard  commit_id
  • –hard – 强制将缓存区和工作目录都同步到你指定的提交
git  reset --hard fdeb212a5418cc8e31f32d63cf197550297468ec
HEAD is now at fdeb212 增加mysql端口配置

然后在提交到远端覆盖。

↓↓↓↓↓↓↓↓
转载文章https://www.cnblogs.com/songgj/p/8965580.html

你可能感兴趣的:(git,git,github)