git commit撤销修改

背景

如果提交了代码,却发现有不需要提交的文件。这时候如何修改呢?可以用git reset指令。

git reset用法解释

git reset 命令用于回退版本,可以指定退回某一次提交的版本。

git reset 命令语法格式如下:

复制代码
git reset [--soft | --mixed | --hard] [HEAD]

--mixed 为默认,可以不用带该参数,用于重置暂存区的文件与上一次的提交(commit)保持一致,工作区文件内容保持不变。

复制代码
git reset  [HEAD] 

实例:

复制代码
$ git reset HEAD^            # 回退所有内容到上一个版本  
$ git reset HEAD^ hello.php  # 回退 hello.php 文件的版本到上一个版本  
$ git  reset  052e           # 回退到指定版本

--soft 参数用于回退到某个版本:

复制代码
git reset --soft HEAD

实例:

复制代码
$ git reset --soft HEAD~3   # 回退上上上一个版本 

--hard 参数撤销工作区中所有未提交的修改内容,将暂存区与工作区都回到上一次版本,并删除之前的所有信息提交:

复制代码
git reset --hard HEAD

实例:

复制代码
$ git reset --hard HEAD~3  # 回退上上上一个版本  
$ git reset --hard bae128  # 回退到某个版本回退点之前的所有信息。 
$ git reset --hard origin/master    # 将本地的状态回退到和远程的一样 

**注意:**谨慎使用 ---hard 参数,它会删除回退点之前的所有信息。

HEAD 说明:

  • HEAD 表示当前版本
  • HEAD^ 上一个版本
  • HEAD^^ 上上一个版本
  • 以此类推...

可以使用 ~数字表示

  • HEAD~0 表示当前版本
  • HEAD~1 上一个版本
  • HEAD^2 上上一个版本
  • 以此类推...

操作实例

shell 复制代码
JackChen@ABC MINGW64 /d/Code/test (master)
$ touch a.txt
[1]+  Done                    gitk --all

JackChen@ABC MINGW64 /d/Code/test (master)
$ git add a.txt 

JackChen@ABC MINGW64 /d/Code/test (master)
$ git commit -m"add text"
[cat1 990152f6] add text
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt

JackChen@ABC MINGW64 /d/Code/test (master)
$ git status
On branch cat1
Your branch is ahead of 'origin/cat1' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

JackChen@ABC MINGW64 /d/Code/test (master)
$ git log
commit 990152f6572a51ce37272bcef7879d3fe2a8f24b (HEAD -> cat1)
Author: JackChen
Date:   Fri Jun 21 11:11:58 2024 +0800

    add text
    
    Change-Id: I0896f0513dd0c580a5c649ac18e8462ff00c3331


JackChen@ABC MINGW64 /d/Code/test (master)
$ git reset --soft 5399da1f4

JackChen@ABC MINGW64 /d/Code/test (master)
$ git status
On branch cat1
Your branch is up to date with 'origin/cat1'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   a.txt

JackChen@ABC MINGW64 /d/Code/test (master)
$ git commit -m"add text"
[cat1 f00c31c8] add text
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt

JackChen@ABC MINGW64 /d/Code/test (master)
$ git reset 5399da1f4

JackChen@ABC MINGW64 /d/Code/test (master)
$ git status
On branch cat1
Your branch is up to date with 'origin/cat1'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        a.txt

nothing added to commit but untracked files present (use "git add" to track)
相关推荐
C++ 老炮儿的技术栈6 小时前
在 Scintilla 中为 Squirrel 语言设置语法解析器的方法
linux·运维·c++·git·ubuntu·github·visual studio
余很多之很多11 小时前
命令行和neovim的git操作软件-lazygit
git
猫头虎11 小时前
GitHub下载教程:2025年最新详解从GitHub上传、下载文件、子目录与完整项目【图文教程】
git·svn·gitee·开源·github·gitea·gitcode
i建模18 小时前
将远程 main 分支同步到 develop 分支的完整指南
git
即使再小的船也能远航1 天前
【Git】实用Git操作指南:从入门到高效协作
git
<但凡.2 天前
Git 完全手册:从入门到团队协作实战(4)
git·bash
SugarPPig2 天前
Git 创建一个完全没有提交历史的 master 分支
git
lb29172 天前
git的使用,推送仓库github
git·github
躲在云朵里`3 天前
Git的使用
大数据·git·elasticsearch
悟能不能悟3 天前
在 IntelliJ IDEA 中打开这个用于设置 Git 用户名(Name)和邮箱(Email)的特定弹窗
java·git·intellij-idea