0

我在我的gh-pages树枝上,当我

$ git add <file>
$ git commit -m "<message>"

我懂了

# On branch gh-pages
nothing to commit, working directory clean

然后我会做,看看以下

$ git stage
Nothing specified, nothing added.
Maybe you wanted to say 'git add .'?

$ git status
# On branch gh-pages
nothing to commit, working directory clean

我试图深入挖掘以找出问题所在

$ git log
commit d7122828ef232829e28654f4bc56022829d03722
Author: siddhion <siddhion@gmail.com>
Date:   Wed Jul 24 19:00:19 2013 +0200

    1st push to gh-pages

$ git reflog 
d712282 HEAD@{0}: checkout: moving from master to gh-pages
9bf529e HEAD@{1}: checkout: moving from gh-pages to master
d712282 HEAD@{2}: commit (initial): 1st push to gh-pages

我不确定这里发生了什么,但在此之前我试图摆脱我最后一次提交尝试以下命令

git reset --soft HEAD
git reset --hard HEAD
git reset HEAD
git reset .
git reset

也许我用这些让事情变得更糟。没有把握。我也试过

$ git push origin gh-pages 
Counting objects: 1156, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (1141/1141), done.
Writing objects:   6% (70/1156), 2.27 MiB | 680.00 KiB/s   

然后我立即取消,因为我不想再提交/添加了。

如何恢复/删除最后一次提交/添加并重新开始?

更新

好的,我首先关注的是 John Galt 的回答。我跑了

$ git checkout d712282

得到了这个

Note: checking out 'd712282'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at d712282... 1st push to gh-pages

在这一点上,我真的不知道该怎么办。我只想回到我的gh-pages分支,不再让那个大坝d712282提交困扰我。所以那时我

$ git checkout gh-pages

愚蠢地我认为d712282提交与分离的头一起死了,所以我像以前一样继续

git add index.html 

但是当我检查是否已经上演时,我得到了

$ git stage
Nothing specified, nothing added.
Maybe you wanted to say 'git add .'?

在这一点上,似乎与以前没有任何变化,但我坚持只是为了彻底。当我尝试运行提交时,我得到了

$ git commit -m "added index.html to gh-pages" 
# On branch gh-pages
nothing to commit, working directory clean

然后我做了

$ git status
# On branch gh-pages
nothing to commit, working directory clean

我想看看正在运行的东西$ git log给了我什么

$ git log
commit d7122828ef232829e28654f4bc56022829d03722
Author: siddhion <siddhion@gmail.com>
Date:   Wed Jul 24 19:00:19 2013 +0200

    1st push to gh-pages

有同样的旧d712282提交。我也跑了$ git reflog,得到了

$ git reflog
d712282 HEAD@{0}: checkout: moving from d7122828ef232829e28654f4bc56022829d03722
d712282 HEAD@{1}: checkout: moving from gh-pages to d712282
d712282 HEAD@{2}: checkout: moving from d7122828ef232829e28654f4bc56022829d03722
d712282 HEAD@{3}: checkout: moving from gh-pages to d712282
d712282 HEAD@{4}: checkout: moving from master to gh-pages
9bf529e HEAD@{5}: checkout: moving from gh-pages to master
d712282 HEAD@{6}: commit (initial): 1st push to gh-pages

而这一点我尝试了杰弗里的建议,然后跑了

$ git reset --hard d712282
HEAD is now at d712282 1st push to gh-pages

所以在这一点上我想做的就是摆脱 d712282 因为每次我尝试git push origin gh-pagesgit 开始推送 d712282 提交,它有 1156 个文件。太多了。我要它死。

4

2 回答 2

0

如果你想恢复一个提交,你需要重置为之前的提交 ID,而不是 HEAD

git reset --hard d712282

如果您没有修改任何内容, git add/commit 将什么也不做。

于 2013-07-24T18:23:27.757 回答
0

您需要从git log或者如果您需要更简洁的语法git log --oneline,这将向您显示您的提交历史记录,以便您可以找到您正在寻找的那个。一旦您拥有与您关心的提交相关的 SHA-1(这就是描述性提交消息很好的原因)。您可以显式签出该提交,如下所示

git checkout WHAT_EVER_THE_SHA-1_TAG_IS

这将使您的暂存索引设置为该提交,您可以继续您的工作。还要阅读日志上的注释,这样你就知道如何让自己摆脱这些情况,并且始终要非常小心reset --hard

https://www.kernel.org/pub/software/scm/git/docs/git-log.html

于 2013-07-24T18:36:58.460 回答