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

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


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

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


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

git fsck --lost-found

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

dangling commit xyz

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

git merge xyz

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


在回答问题之前,让我们先了解一些背景知识,解释一下这个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。


试试这个, 这将显示一段时间内记录在git中的所有提交

git reflog

找到你想要的承诺

git log HEAD@{3}

or

git log -p HEAD@{3}    

然后看看它是否正确:

git checkout HEAD@{3}

这将为该提交创建一个独立的头部。如果需要,添加并提交任何更改

git status 
git add
git commit -m "temp_work" 

现在,如果想要恢复提交回一个分支,比如master,你需要将这个分支交换机命名为master,然后合并为master。

git branch temp
git checkout master
git merge temp

这里还有一个Git教程网站上专门用于reflog的链接: Atlassian Git教程


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

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

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


今天我就遇到了这样的事,所以我写下了我的救命稻草。我的回答和@Amber的回答很相似。

首先,我做了一个git reflog,搜索那个特定的提交的哈希,然后复制那个哈希,并从那个分支做了一个git cherry-pick <hash>。这为我当前的分支带来了所有的变化,并恢复了我对GIT的信心。

祝你有愉快的一天!


如果你在使用IntelliJ IDE时找不到你的git reflog提交,你可以右键单击你的项目根文件夹> Local History -> Show History并从那里恢复你的更改。

我用git push -f搞砸了git rebase,这真的救了我,因为提交是从本地和远程存储库中删除的。

希望这能帮到别人

干杯


最安全的方法是

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

Git reflog或Git reflog | grep your_phrase

创建新的分支

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


首先,使用git reflog列出你所有的提交,包括丢失的提交

git reflog

然后使用git log HEAD@{your_commit_number}找到你正在寻找的提交。如

git log HEAD@{17}

在你用git Checkout HEAD@{your_commit_number}找到它后签入提交

git checkout HEAD@{17}

您可能需要添加并提交更改

git add .
git commit -m"quuck_fix"

然后,您必须创建一个临时分支来将提交恢复到您的分支

git branch temp

最后,您将签入现有的分支,然后合并临时分支。

#git checkout <your_existing_branch> e.g

git checkout main
git merge temp