git的撤销与回滚在平时使用中还是比较多的,比如说我们想将某个修改后的文件撤销到上一个版本,或者是想撤销某次多余的提交,都要用到git的撤销和回滚操作。撤销分两种情况,一个是commit之前,一个是commit之后,下面具体看下这两种情况。
撤销
撤销就是在没有commit之前,存在两种情况;
未添加到暂存区的撤销(没有git add)
$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: app/Services/WeChatService.php
modified: routes/api.php
no changes added to commit (use "git add" and/or "git commit -a")
可以通过
git checkout -- filename来撤销修改
git checkout -- app/Services/WeChatService.php
再次查看文件状态发现选择的文件已经成功被撤销了。
$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: routes/api.php
no changes added to commit (use "git add" and/or "git commit -a")
如果想将多个文件一次性撤销可以用
git checkout -- .
添加进暂存区的撤销(git add后)
$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: app/Services/WeChatService.php
modified: routes/api.php
no changes added to commit (use "git add" and/or "git commit -a")
加入暂存区
$ git add -A
$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: app/Services/WeChatService.php
modified: routes/api.php
从暂存区撤销
$ git reset HEAD app/Services/WeChatService.php
Unstaged changes after reset:
M app/Services/WeChatService.php
如果想一次性将所有暂存区文件撤销回来,还是上面的命令,不过不用加文件路径。
$ git reset HEAD
Unstaged changes after reset:
M app/Services/WeChatService.php
M routes/api.php
回滚
回滚即在git commit之后,有revert和rest两种命令;
revert命令
如果当commit提交后想撤销的话,这就需要revert命令。git revert命令是撤销某次操作,而在此次操作之前和之后的提交记录都会保留。 先修改了几个文件然后commit再用git log查看提交记录。
commit 5e30c37dbab33520313264ac5970411d50fdf52f
Author: Chenxiao_Zhao <1037089219@qq.com>
Date: Tue Oct 22 17:28:06 2019 +0800
add token
然后使用revert,后面跟上git提交的commitid
git revert 5e30c37dbab33520313264ac5970411d50fdf52f
Revert "add token"
This reverts commit 5e30c37dbab33520313264ac5970411d50fdf52f.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch develop
# Your branch is ahead of 'origin/develop' by 1 commit.
# (use "git push" to publish your local commits)
#
# Changes to be committed:
# modified: app/Services/WeChatService.php
# modified: routes/api.php
#
然后在推送到远端更新远程仓库代码,修改的文件就撤销回来了。注意的是revert奇数次生效,偶数次又回到之前的修改状态。比如一个文件内容是a,那么修改为ab,revert后文件变成了a,如果在revert后文件又还原成ab了。
### reset命令
还有就是如果想回到之前某个版本,可以用reset命令,可以回退到某次提交,那该提交之后的提交都会回滚,不过这种覆盖是不可逆的,之前的提交记录都没有了。所以平时开发中尽量注意,避免使用reset。用法:
git reset --hard commit_id
–hard – 强制将缓存区和工作目录都同步到你指定的提交
git reset --hard 28252b303ad8b933b3b1aaa7760df2791f4dfdf3
HEAD is now at 28252b3 Update HomeDataController.php
另外git reset –sort xxxx只回退commit,如果你想再次提交直接git commit即可