git各阶段回滚撤销操作

大家在使用git的时候,会遇到一些需要 “撤销” 的操作,比如:

  1. 修改了一个不需要修改的文件,需要撤销
  2. add了一个不需要add的文件,需要撤销
  3. commit了一条不需要的commit记录,需要撤销
  4. push了一次不需要push的操作,需要撤销

下面,就这几个问题,分别作出 “撤销” 的相应操作示例。

    1、修改了一个不需要修改的文件(工作区的代码撤销)

[work@daiyuanpei gitlab]$ cat hello.txt  # 查看一下文件内容
hello world!
[work@daiyuanpei gitlab]$ vi hello.txt   # 修改文件内容
[work@daiyuanpei gitlab]$ cat hello.txt  # 查看修改后的文件内容
hello world!
this is new line
[work@daiyuanpei gitlab]$ git status   # 查看git状态
On branch master
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:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

# 撤销操作
[work@daiyuanpei gitlab]$ git checkout hello.txt  # 使用 checkout 恢复单个文件
[work@daiyuanpei gitlab]$ cat hello.txt  # 查看文件内容
hello world!
[work@daiyuanpei gitlab]$ git status  # 查看git状态
On branch master
nothing to commit, working tree clean

    2、add了一个不需要add的文件(add到暂存区的代码撤销)

[work@daiyuanpei gitlab]$ cat hello.txt  # 先查看一下文件内容
hello world!
[work@daiyuanpei gitlab]$ vi hello.txt   # 修改文件内容
[work@daiyuanpei gitlab]$ cat hello.txt  # 查看修改后的文件内容
hello world!
this is new line
[work@daiyuanpei gitlab]$ git status   # 查看git状态
On branch master
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:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

[work@daiyuanpei gitlab]$ git add hello.txt   # add到暂存区
[work@daiyuanpei gitlab]$ git status   # 查看git状态
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   hello.txt

# 撤销操作
[work@daiyuanpei gitlab]$ git reset HEAD # 将暂存区的代码撤销到工作区
Unstaged changes after reset:
M	hello.txt
[work@daiyuanpei gitlab]$ git checkout hello.txt  # 根据是否需要恢复文件来决定是否执行该命令
hello world!
[work@daiyuanpei gitlab]$ git status  # 查看git状态
On branch master
nothing to commit, working tree clean

    3、commit了一条不需要的commit记录(提交到本地仓库到代码撤销)

[work@daiyuanpei gitlab]$ cat hello.txt  # 先查看一下文件内容
hello world!
[work@daiyuanpei gitlab]$ vi hello.txt   # 修改文件内容
[work@daiyuanpei gitlab]$ cat hello.txt  # 查看修改后的文件内容
hello world!
this is new line
[work@daiyuanpei gitlab]$ git status   # 查看git状态
On branch master
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:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

[work@daiyuanpei gitlab]$ git add hello.txt   # add到暂存区
[work@daiyuanpei gitlab]$ git status   # 查看git状态
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   hello.txt

[work@daiyuanpei gitlab]$ git commit -m 'add new line'   # 提交到本地仓库
[work@daiyuanpei gitlab]$ git --no-pager log --oneline
d16d80f (HEAD -> master) add new line
41b3e21 add comment
9a4158d add hello.txt

# 撤销操作
[work@daiyuanpei gitlab]$ git reset --hard HEAD^   # 向前回退一个版本
HEAD is now at 41b3e21 add comment

[work@daiyuanpei gitlab]$ git --no-pager log --oneline
41b3e21 (HEAD -> master) add comment
9a4158d add hello.txt

    1、可以使用HEAD^来描述版本,一个^表示前一个版本,两个^^表示前两个版本,以此类推。
    2、也可以使用数字来代替^,比如说前100个版本可以写作HEAD~100。
    3、也可以直接写版本号,表示跳转到某一个版本处。我们每次提交成功后,都会生成一个哈希码作为版本号,所以这里我们也可以直接填版本号,哈希码很长,但是我们不用全部输入,只需要输入前面几个字符即可,就能识别出来。
    这里使用了--hard,也可以使用–soft或者–mixed来操作,具体有什么区别,大家可以去看下文档。

    4、push了一次不需要push的操作(提交到远程仓库的代码撤销)

[work@daiyuanpei gitlab]$ cat hello.txt  # 先查看一下文件内容
hello world!
[work@daiyuanpei gitlab]$ vi hello.txt   # 修改文件内容
[work@daiyuanpei gitlab]$ cat hello.txt  # 查看修改后的文件内容
hello world!
this is new line
[work@daiyuanpei gitlab]$ git status   # 查看git状态
On branch master
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:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

[work@daiyuanpei gitlab]$ git add hello.txt   # add到暂存区
[work@daiyuanpei gitlab]$ git status   # 查看git状态
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   hello.txt

[work@daiyuanpei gitlab]$ git commit -m 'add new line'   # 提交到本地仓库
[work@daiyuanpei gitlab]$ git --no-pager log --oneline
d16d80f (HEAD -> master) add new line
41b3e21 add comment
9a4158d add hello.txt
[work@daiyuanpei gitlab]$ git push origin master   # 推到远程仓库

# 撤销操作
[work@daiyuanpei gitlab]$ git reset --hard HEAD^   # 向前回退一个版本
HEAD is now at 41b3e21 add comment

[work@daiyuanpei gitlab]$ git --no-pager log --oneline
41b3e21 (HEAD -> master) add comment
9a4158d add hello.txt

[work@daiyuanpei gitlab]$ git push origin master -f   # 强制提交到远程仓库

# -f 参数代表强制覆盖远程仓库,这里的操作一定要确保没有问题后再去push,不然很容易丢失记录(慎重)

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值