gpt4 book ai didi

git - 如何取消本地 git 提交?

转载 作者:IT王子 更新时间:2023-10-29 01:09:02 30 4
gpt4 key购买 nike

我的问题是我更改了一个文件,例如:自述文件,添加了一个新行“this for my testing line”并保存了文件,然后我发出了以下命令:

git status

# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: README
#
no changes added to commit (use "git add" and/or "git commit -a")


git add README

git commit -a -m 'To add new line to readme'

我没有将代码推送到 GitHub。现在我想取消这个提交。

为此,我使用了

git reset --hard HEAD~1

但是我从 README 文件中丢失了新添加的行“this for my testing line”。这不应该发生。我需要内容在那里。 有没有办法保留内容并取消我的本地提交?

最佳答案

只需使用 git reset 而不使用 --hard 标志:

git reset HEAD~1

PS:在基于 Unix 的系统上,您可以使用 HEAD^,它等于 HEAD~1。在 Windows 上 HEAD^ 将不起作用,因为 ^ 表示行继续。所以你的命令提示符只会问你 More?

关于git - 如何取消本地 git 提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4850717/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com