是否可以撤消由以下命令引起的更改?如果是,怎么办?
git reset --hard HEAD~1
是否可以撤消由以下命令引起的更改?如果是,怎么办?
git reset --hard HEAD~1
当前回答
制作了一个很小的脚本,以便稍微容易地找到要查找的提交:
git fsck--lost found | grep commit | cut-d“”-f 3 | xargs-i git show \{\}| egrp“^commit |日期:”
是的,它可以用锥子或类似的东西做得更漂亮,但它很简单,我只是需要它。可能会为其他人节省30秒。
其他回答
如果你真的很幸运,就像我一样,你可以回到你的文本编辑器,点击“撤销”。
我知道这确实不是一个正确的答案,但它节省了我半天的工作,所以希望它能为其他人做同样的事情!
数字刷新
在列表中找到提交sha,然后将其复制并粘贴到以下命令中:
git cherry pick<the sha>
帕特·诺茨是正确的。你可以在几天内收回承诺。git只会在大约一个月后收集垃圾,除非您明确告诉它删除更新的blob。
$ git init
Initialized empty Git repository in .git/
$ echo "testing reset" > file1
$ git add file1
$ git commit -m 'added file1'
Created initial commit 1a75c1d: added file1
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 file1
$ echo "added new file" > file2
$ git add file2
$ git commit -m 'added file2'
Created commit f6e5064: added file2
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 file2
$ git reset --hard HEAD^
HEAD is now at 1a75c1d... added file1
$ cat file2
cat: file2: No such file or directory
$ git reflog
1a75c1d... HEAD@{0}: reset --hard HEAD^: updating HEAD
f6e5064... HEAD@{1}: commit: added file2
$ git reset --hard f6e5064
HEAD is now at f6e5064... added file2
$ cat file2
added new file
您可以在示例中看到,由于硬重置,file2被删除,但当我通过reflog重置时,它被放回原位。
据我所知,--hard将丢弃未提交的更改。因为git不跟踪这些。但您可以撤消放弃的提交。
$ git reflog
将列出:
b0d059c HEAD@{0}: reset: moving to HEAD~1
4bac331 HEAD@{1}: commit: added level introduction....
....
其中4bac331是丢弃的提交。
现在只需将头部移至该提交:
$ git reset --hard 4bac331
git-relog并返回到最后一个HEAD6a56624(HEAD->master)HEAD@{0}:重置:移动到HEAD~31a9bf73 HEAD@{1}:提交:在模型中添加更改生成二进制