“git reset –hard HEAD~1” 与 “git checkout .” 之间的区别是什么?

huangapple go评论73阅读模式
英文:

What is the difference between "git reset --hard HEAD~1" and "git checkout ."?

问题

我目前正在学习Git,我有一个问题,这些命令到底是做什么的?

git reset --hard HEAD~1

git checkout .

它们两者有什么区别?据我理解,它们都会永久删除最后一次提交。我认为它们之间有深刻的区别,所以我希望能得到一个清晰的解释,因为我是初学者。

英文:

I'm currently learning git, and I have issue what exactly do these commands do?

git reset --hard HEAD~1

git checkout .

What's the difference between the two? They both permanently delete the last commit, as I understand. I think that there is a deep difference, so I hope for a clear explanation because I'm a beginner.

答案1

得分: 1

git reset --hard HEAD~1: 永久删除最后一次提交以及所有未提交的更改。

git checkout .: 放弃工作目录中的更改,将其恢复到最后一次提交的状态,不影响提交历史记录。

英文:

git reset --hard HEAD~1: Permanently removes the last commit and all uncommitted changes.

git checkout .: Discards changes in the working directory and restores it to the state of the last commit without affecting commit history.

答案2

得分: 0

git reset --hard HEAD~1 将当前分支/HEAD 移动到 前一个提交

--hard 丢弃对已跟踪文件的更改,包括已暂存的更改。

(由于此操作移动了HEAD,因此会在 git reflog 中创建一个条目)

git checkout . 丢弃当前目录树中已跟踪文件的 未暂存的 更改。

这意味着:已暂存的更改将保留在 当前提交 之上!

要丢弃已暂存的更改,您必须指定要检出的提交,例如 HEAD:git checkout HEAD . - 如果在存储库的顶级目录中运行,它将类似于 git reset --hard HEAD(没有 ~1!)。

英文:
git reset --hard HEAD~1

makes your current branch/HEAD move to the previous commit.<br>
--hard discards changes to tracked files, including staged changes.<br>
(As this operation moves HEAD, an entry in git reflog is created)

git checkout .

discards unstaged changes from tracked files in the current directory tree.<br>This means: Staged changes will be kept on top of current commit!<br>To also discard staged changes you have to specify the commit to check out, e.g. HEAD: git checkout HEAD . - which will behave similar to git reset --hard HEAD (no ~1!) - if run in top directory of the repo.

huangapple
  • 本文由 发表于 2023年7月3日 08:24:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定