首先,得到“你的分支领先于原点/master 3次提交”,然后我的应用程序已经恢复到较早的时间与较早的更改。

我怎么才能把过去11个小时所做的事情拿回来呢?


当前回答

Git reflog是你的朋友。在这个列表中找到你想要的提交,你可以重置到它(例如:git reset——hard e870e41)。

(如果您没有提交更改……你可能会遇到麻烦——早点承诺,经常承诺!)

其他回答

Git reflog是你的朋友。在这个列表中找到你想要的提交,你可以重置到它(例如:git reset——hard e870e41)。

(如果您没有提交更改……你可能会遇到麻烦——早点承诺,经常承诺!)

另一种获取已删除提交的方法是使用git fsck命令。

git fsck --lost-found

这将输出类似于最后一行的内容:

dangling commit xyz

我们可以使用reflog检查它是否与其他答案中建议的提交相同。现在我们可以进行git归并

git merge xyz

注意: 如果我们已经运行了一个git gc命令,它将删除对悬空提交的引用,那么我们就不能用fsck来取回提交。

可悲的是git太不靠谱了:( 我刚丢了2天的工作。

最好在提交之前手动备份任何东西。我刚刚执行了“git commit”,git什么也没说就销毁了我所有的更改。

我吸取了教训——下次要先备份,然后再提交。任何事都不要相信乞丐。

最安全的方法是

查找要恢复的状态的提交头号

Git reflog或Git reflog | grep your_phrase

创建新的分支

git checkout -b your-branch HEAD@{<your-number>}

在回答问题之前,让我们先了解一些背景知识,解释一下这个HEAD是什么。

首先,什么是HEAD?

HEAD只是当前分支上当前提交(最新)的引用。 在任何给定时间只能有一个HEAD(不包括git工作树)。

HEAD的内容存储在.git/HEAD中,它包含了当前提交的40字节SHA-1。


分离的头

如果你不是在最近一次提交上——这意味着HEAD指向历史上的前一次提交,它被称为分离HEAD。

在命令行中,它看起来像这样- SHA-1而不是分支名称,因为HEAD并不指向当前分支的顶端:


关于如何从分离的HEAD中恢复的一些选项:


去结帐

git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back

这将检出指向所需提交的新分支。 该命令将检出到给定的提交。 在这一点上,您可以创建一个分支并从这里开始工作。

# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>

# Create a new branch forked to the given commit
git checkout -b <branch name>

git 引用日志

你也可以使用reflog。 git reflog将显示更新HEAD的任何更改,检出所需的reflog条目将把HEAD设置回此提交。

每次修改HEAD时,reflog中都会有一个新的条目

git reflog
git checkout HEAD@{...}

这将使您回到期望的提交


Git reset——hard <commit_id> . txt

“移动”您的头回到所需的提交。

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts if you've modified things which were
# changed since the commit you reset to.

注意:(从Git 2.7开始)你也可以使用Git rebase——no-autostash。


git 还原<sha-1>

“撤销”给定的提交或提交范围。 reset命令将“撤销”在给定提交中所做的任何更改。 带有撤销补丁的新提交将被提交,而原始提交也将保留在历史记录中。

# Add a new commit with the undo of the original one.
# The <sha-1> can be any commit(s) or commit range
git revert <sha-1>

这个模式说明了哪个命令做什么。 如你所见,重置&&签出修改HEAD。