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

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


当前回答

试试这个, 这将显示一段时间内记录在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中的所有提交

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教程

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

如果你在使用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