美文网首页
git撤销修改

git撤销修改

作者: wangcanfeng | 来源:发表于2018-07-27 19:05 被阅读0次

在实际开发过程中,作为一名程序猿,可能会遇到这样一个问题,有时候提交的代码太多了,希望将代码回滚,有时候要将本地的修改废弃掉。这些操作都会在日常开发中遇到,今天希望把提交中一个文件回滚,但是不小心把所有文件全部会滚了,一脸懵逼。所以,这篇文章就来好好总结以下git的撤销修改操作。

撤销修改:

情形1:文件修改后还没有被放到暂存区

当对git工作区中的文件进行修改之后,通过git status命名会出现以下信息:

On branch master
Your branch is up to date with 'origin/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:   README.md

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

情形2:文件被添加到了暂存区

如果将文件README.md通过git add放入stage(暂存区),通过git status会出现以下信息:

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   README.md

根据提示,只需要通过git reset就可以将修改从暂存区中清除,此时实际上是回到了情形1中:

git reset HEAD README.md 

Unstaged changes after reset:
M   README.md

git status 

On branch master
Your branch is up to date with 'origin/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:   README.md

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

情形3:文件被提交到了本地分支

如果通过git commit将修改提交到了本地分支,这次修改又该怎么撤销?

1.通过git log查看本地分支的历史提交记录:

commit d24b19e5cee389f1e1f2bb906fc5765d321da8cd (HEAD -> master)
Author: xxxxxxxxxx
Date:   Wed Jul 25 22:44:48 2018 +0800

    修改README.md

commit 2f4c6e119c38a8f0b71dd20244903f57079fbc3c (origin/master)
Author: xxxxxxxxxx
Date:   Wed Jul 25 20:53:13 2018 +0800

    udpate

2.通过git reset --hard {commitId}或者git reset --hard HEAD~1

HEAD is now at 2f4c6e1 udpate

可以看到HEAD现在指向的是2f4c6e1这次提交,最新的提交也已经没了。

3.但是如果在一次提交中包含了多个文件,撤销修改只希望撤销其中的一个文件,上述这个命令显然是不能够满足需求的。如果是这样的话,可以通过git reset --soft HEAD~1来实现:

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   README.md

命令 git reset --soft {commitId} 的作用就是将修改从本地分支回滚,但是会保留在暂存区中。

所以这里就有必要讲讲git reset这个命令:

git reset [<mode>] [<commit>]
           This form resets the current branch head to <commit> and possibly
           updates the index (resetting it to the tree of <commit>) and the
           working tree depending on <mode>. If <mode> is omitted, defaults to
           "--mixed". The <mode> must be one of the following:

           --soft
               Does not touch the index file or the working tree at all (but
               resets the head to <commit>, just like all modes do). This
               leaves all your changed files "Changes to be committed", as git
               status would put it.

           --mixed
               Resets the index but not the working tree (i.e., the changed
               files are preserved but not marked for commit) and reports what
               has not been updated. This is the default action.

               If -N is specified, removed paths are marked as intent-to-add
               (see git-add(1)).

           --hard
               Resets the index and working tree. Any changes to tracked files
               in the working tree since <commit> are discarded.

           --merge
               Resets the index and updates the files in the working tree that
               are different between <commit> and HEAD, but keeps those which
               are different between the index and working tree (i.e. which
               have changes which have not been added). If a file that is
               different between <commit> and the index has unstaged changes,
               reset is aborted.

               In other words, --merge does something like a git read-tree -u
               -m <commit>, but carries forward unmerged index entries.

           --keep
               Resets index entries and updates files in the working tree that
               are different between <commit> and HEAD. If a file that is
               different between <commit> and HEAD has local changes, reset is
               aborted.
 

这里面值得注意的就是:
1.如果使用--hard模式的话,commit之后的所有修改都会被废弃,无法找回。之前不慎用了--hard,欲哭无泪。
2.默认使用的是--mixed,重置了暂存区(index),但不会重置工作区(working tree)。所有的修改都还在。

其实上述三种模式总结以下就是:

reset模式 工作区 暂存区 本地分支
--hard
--mixed 保留
--soft 保留 保留

相关文章

  • 1223

    git 撤销和删除 撤销工作区的修改:撤销某个文件的工作区修改:git checkout [--] filepat...

  • 撤销修改

    撤销修改, 是便于出现文件修改错误后,撤销当前修改。使用git checkout 撤销工作区的修改;...

  • Git版本回退

    git 撤销commit 撤销修改http://www.trinea.cn/dev-tools/git-skill/

  • git各场景下的撤销

    1、撤销工作区的修改(未执行 git add .) git checkout 文件或目录 2、撤销暂存区的修改(已...

  • git 相关

    目录 git SSH配置 git 分支名修改 git 放弃本地修改 强制拉取更新 git 撤销commit git...

  • Git操作速查

    舍弃本地修改 git 撤销,放弃本地修改 未add时git checkout . add后,commit前git ...

  • 常用的git命令

    常用Git命令 切换分支:git checkout name 撤销修改:git checkout -- file ...

  • git常用命令

    1.撤销本地修改 修改了本地某个或某些文件,没有git add 也没有git commit,可以用这个命令撤销,尤...

  • Github常用的命令:

    切换分支:git checkout name 撤销修改:git checkout -- file 删除文件:git...

  • Github常用的命令

    下载 git clone + 地址 切换分支:git checkout name 撤销修改:git checkou...

网友评论

      本文标题:git撤销修改

      本文链接:https://www.haomeiwen.com/subject/iuggmftx.html