首先,得到“你的分支领先于原点/master 3次提交”,然后我的应用程序已经恢复到较早的时间与较早的更改。
我怎么才能把过去11个小时所做的事情拿回来呢?
首先,得到“你的分支领先于原点/master 3次提交”,然后我的应用程序已经恢复到较早的时间与较早的更改。
我怎么才能把过去11个小时所做的事情拿回来呢?
当前回答
在回答问题之前,让我们先了解一些背景知识,解释一下这个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 reflog是你的朋友。在这个列表中找到你想要的提交,你可以重置到它(例如:git reset——hard e870e41)。
(如果您没有提交更改……你可能会遇到麻烦——早点承诺,经常承诺!)
今天我就遇到了这样的事,所以我写下了我的救命稻草。我的回答和@Amber的回答很相似。
首先,我做了一个git reflog,搜索那个特定的提交的哈希,然后复制那个哈希,并从那个分支做了一个git cherry-pick <hash>。这为我当前的分支带来了所有的变化,并恢复了我对GIT的信心。
祝你有愉快的一天!
最安全的方法是
查找要恢复的状态的提交头号
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
在回答问题之前,让我们先了解一些背景知识,解释一下这个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。