最新消息:欢迎加入小松的QQ群一起讨论一起学习,本站启用小松自己写的redis缓存,文章发布修改自动删除缓存,现在速度杠杠的

git各种情况的撤销命令

版本控制 460浏览 0评论

今天在提交文件的时候不小心按个

git add -a

然后把所有文件都提交了
之后想撤销一下,麻痹的之前没有撤销过,不知道命令是什么,于是网上百度
然后总结了一下撤销的命令

①本地修改没有执行过git add,怎么撤销文件

发生的情况场景
代码写了半天发现代码的文件写错了,要还原成原来的文件
执行命令

git reset --hard origin/master

marster为分支

②执行过git add之后,怎么撤销

/**创建一个demo.php**/
carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ touch demo.php
/**查看一下git的状态**/
carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git status
On branch phpsong
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        demo.php

nothing added to commit but untracked files present (use "git add" to track)
/**添加到缓存区**/
carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git add demo.php
/**查看一下git的状态,这时候的状态跟创建这个文件之后的状态不一样**/
carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git status
On branch phpsong
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   demo.php

/**测试add 之后的版本**/
carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git reset HEAD demo.php
/**查看一下git的状态,现在的状态跟创建这个文件之后的状态一样说明已经撤销了**/
carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git status
On branch phpsong
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        demo.php

nothing added to commit but untracked files present (use "git add" to track)

上面撤销文件的方法只针对单个文件,有同学会问如果有多个文件,怎么处理,如果文件多了一个个文件处理那不累死,针对这个问题,其实很简单

git reset HEAD

把后面的文件名去除,下面是我的操作

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ touch demo1.php

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git status
On branch phpsong
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        demo.php
        demo1.php

nothing added to commit but untracked files present (use "git add" to track)

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git add *.php

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git status
On branch phpsong
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   demo.php
        new file:   demo1.php


carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git reset HEAD

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git status
On branch phpsong
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        demo.php
        demo1.php

nothing added to commit but untracked files present (use "git add" to track)



②执行过git commit之后,怎么撤销提交
carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ touch demo.php

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git status
On branch phpsong
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        demo.php

nothing added to commit but untracked files present (use "git add" to track)

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git add demo.php

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git commit -m "添加demo.php"
[phpsong ac0a071] 添加demo.php
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 demo.php

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git status
On branch phpsong
nothing to commit, working directory clean

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git reset HEAD^

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git status
On branch phpsong
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        demo.php

nothing added to commit but untracked files present (use "git add" to track)

④已经执行了git push 提交到了远程,怎么撤销

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git status
On branch phpsong
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        demo.php

nothing added to commit but untracked files present (use "git add" to track)

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git add demo.php

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git commit -m "添加demo.php"
[phpsong 333e66c] 添加demo.php
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 demo.php

carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git push origin phpsong
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 313 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:QiuCarson/test.git
   e39b0a8..333e66c  phpsong -> phpsong

github git 提交文件

/**撤销本地到上一个版本**/
carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git reset HEAD^
/**强制更新远程**/
carson@carson-PC MINGW64 /d/wordpress插件/test (phpsong)
$ git push origin phpsong --force
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:QiuCarson/test.git
 + 333e66c...e39b0a8 phpsong -> phpsong (forced update)

git强制推送到github

QQ交流群:136351212

如无特别说明,本站文章皆为原创,若要转载,务必请注明以下原文信息:
转载保留版权:小松博客» git各种情况的撤销命令
本文链接地址:https://www.phpsong.com/1843.html

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
木有头像就木有JJ!点这里按步骤申请Gravatar头像吧!

网友最新评论 (2)

加载中,请稍候...
  1. :cool: :cool: 来瞅瞅
    • 兄弟我看你留了很多言,有空搞一下头像
      小松12个月前 (12-12)回复