Amb*_*ber 496
git reflog是你的朋友.在该列表中找到要包含的提交,然后可以重置为该提交(例如:) git reset --hard e870e41.
(如果您没有提交更改......您可能遇到麻烦 - 提前提交,并经常提交!)
Cod*_*ard 96
HEAD什么?HEAD它只是对当前分支中当前提交(最新)的引用.在任何给定时间
只能有1个HEAD.
如果你没有进行最新的提交 - 这意味着它HEAD指向历史上的先前提交,它被称为分离的HEAD.
几个选项:
git checkoutgit checkout <commit_id>
Run Code Online (Sandbox Code Playgroud)
git reflog您可以随时使用reflog,以及
git reflog
git checkout HEAD@{...}
Run Code Online (Sandbox Code Playgroud)
这将使您回到所需的提交
git reset HEAD --hard <commit_id>"移动"你的头回到所需的提交.
# 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.
Run Code Online (Sandbox Code Playgroud)
git rebase --no-autostash.git checkoutgit checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits to go back
Run Code Online (Sandbox Code Playgroud)
这将检查指向所需提交的新分支
这是可以做什么的一般模式.
Atr*_*tri 31
获取已删除提交的另一种方法是使用该git fsck命令.
git fsck --lost-found
Run Code Online (Sandbox Code Playgroud)
这将输出类似于最后一行的内容:
dangling commit xyz
Run Code Online (Sandbox Code Playgroud)
我们可以使用reflog其他答案中建议的方式检查它是否是相同的提交.现在我们可以做一个git merge
git merge xyz
Run Code Online (Sandbox Code Playgroud)
注意:如果我们已经运行了一个将删除对悬空提交的引用的命令,
我们就无法获得提交.fsckgit gc
小智 17
首先使用 git reflog 列出所有提交,甚至丢失的提交
git reflog
Run Code Online (Sandbox Code Playgroud)
然后使用 git log HEAD@{your_commit_number} 查找您要查找的提交。例如
git log HEAD@{17}
Run Code Online (Sandbox Code Playgroud)
使用 git checkout HEAD@{your_commit_number} 例如找到提交后签入提交
git checkout HEAD@{17}
Run Code Online (Sandbox Code Playgroud)
您可能需要添加并提交更改
git add .
git commit -m"quuck_fix"
Run Code Online (Sandbox Code Playgroud)
然后,您必须创建一个临时分支以将提交恢复到您的分支
git branch temp
Run Code Online (Sandbox Code Playgroud)
最后,您将签出到现有分支,然后合并临时分支。
#git checkout <your_existing_branch> e.g
git checkout main
git merge temp
Run Code Online (Sandbox Code Playgroud)
小智 10
试试这个,这将显示一段时间内记录在 git 中的所有提交
git reflog
Run Code Online (Sandbox Code Playgroud)
找到你想要的提交
git log HEAD@{3}
Run Code Online (Sandbox Code Playgroud)
或者
git log -p HEAD@{3}
Run Code Online (Sandbox Code Playgroud)
然后检查它是否正确:
git checkout HEAD@{3}
Run Code Online (Sandbox Code Playgroud)
这将为该提交创建一个分离的头。如果需要,添加并提交任何更改
git status
git add
git commit -m "temp_work"
Run Code Online (Sandbox Code Playgroud)
现在,如果想要将提交恢复到一个分支,让我们说 master,您需要将此分支 switch 命名为 master,然后合并到 master。
git branch temp
git checkout master
git merge temp
Run Code Online (Sandbox Code Playgroud)
这里还有一个专门用于 Git 教程站点上的 reflog 的链接: Atlassian Git Tutorial
如果您找不到您的提交,git reflog并且碰巧您正在使用IntelliJ IDE,您可以right click on your project root folder -> Local History -> Show History从那里恢复您的更改。
我搞砸了git rebase,git push -f这确实救了我,因为提交已从本地和远程存储库中删除。
希望能拯救某人的一天
干杯
这件事就在今天发生在我身上,所以我正在写一些对我来说是救命稻草的东西。我的回答与@Amber 的回答非常相似。
首先,我做了一个git reflog并搜索了那个特定提交的哈希,然后只是复制了那个哈希并git cherry-pick <hash>从那个分支做了一个。这将所有更改从丢失的提交带到了我当前的分支,并恢复了我对 GIT 的信心。
祝你今天过得愉快!
| 归档时间: |
|
| 查看次数: |
95087 次 |
| 最近记录: |