首先,收到了“您的分支领先于origin/master 3个提交”的消息,然后我的应用程序回滚到了早期的时间并且有早期的更改。
我该如何找回过去11个小时所做的工作?
首先,收到了“您的分支领先于origin/master 3个提交”的消息,然后我的应用程序回滚到了早期的时间并且有早期的更改。
我该如何找回过去11个小时所做的工作?
git reflog
是你的好朋友。在列表中找到你想要回到的提交记录,然后可以重置到该提交记录(例如:git reset --hard e870e41
)。
(如果你还没有提交你的更改……那可能有麻烦了——尽早提交,频繁提交!)
git log HEAD@{1}
的记录。如果它看起来像是正确的提交序列,那么可以运行 git reset HEAD@{1}
。 - Ambergit fsck --lost-found
)找回。 - LandysHEAD
。
首先,什么是 HEAD?
HEAD
简单地说是当前分支上最新的提交(commit)的引用。
在任何给定的时间只能有一个 HEAD
(不包括 git worktree
)。
HEAD
的内容存储在 .git/HEAD
中,并包含当前提交的 40 字节 SHA-1。
脱离 HEAD
如果您没有处于最新提交状态 - 意味着HEAD
指向历史记录中的先前提交,则称为脱离 HEAD
。
HEAD
没有指向当前分支的末尾:
git checkout
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
您也可以随时使用reflog
。
git reflog
将显示更新HEAD
的任何更改,并检出所需的reflog条目将HEAD
设置回此提交。
每次修改HEAD都会在reflog
中创建一个新条目
git reflog
git checkout HEAD@{...}
这将使您返回到所需的提交
git reset --hard <commit_id>
将您的HEAD退回至所需提交记录。
# 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 rebase --no-autostash
。git revert <sha-1>
撤销给定的提交或提交范围。
重置命令将“撤消”在给定提交中进行的任何更改。
一个包含撤消补丁的新提交将被提交,而原始提交也将保留在历史记录中。
# 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>
reset && checkout
修改了 HEAD
。
git reset --hard <commit_id>
中,删除 HEAD
就可以了!赞一个图形表示!! - reverie_ssgit reflog <branchname>
可以非常有用,因为你可以看到一个分支的更改。 - Markus Schreibergit cherry-pick <dangling-commit-hash>
将其放置在那里。 - Emile Vrijdags使用git fsck
命令还有一种方法可以到达已删除的提交。
git fsck --lost-found
这将在最后一行输出类似下面的内容:dangling commit xyz
我们可以使用其他答案中建议的 reflog
来检查它是否是相同的提交。现在我们可以执行 git merge
。
git merge xyz
注意:
如果我们已经运行了git gc
命令并删除了对悬空提交的引用,则无法使用fsck
获取提交。
今天我遇到了这个问题,下面是对我非常有帮助的解决方法。我的答案与 @Amber 的答案非常相似。
首先,我运行了git reflog
命令并搜索了那个特定提交的哈希值,然后只需复制该哈希值并从该分支运行git cherry-pick <hash>
命令。 这将会把丢失提交中的所有更改合并到当前分支,并恢复我的GIT操作信心。
祝愉快!
首先使用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
试试这个,这将展示在一段时间内 Git 记录的所有提交
git reflog
使用以下命令找到你想要的提交
git log HEAD@{3}
或者git log -p HEAD@{3}
然后检查一下它是否正确:
git checkout HEAD@{3}
这将为该提交创建一个分离的 HEAD。如果需要,请添加和提交任何更改。git status
git add
git commit -m "temp_work"
现在,如果您想将提交恢复到一个分支,比如主分支,您需要给它命名,切换到主分支,然后将其合并到主分支。
git branch temp
git checkout master
git merge temp
这里还有一个链接,专门介绍 Git 教程网站上的 reflog: Atlassian Git 教程
git reflog
不能找到你的提交记录,那么你可以通过右键点击项目根目录 -> 本地历史 -> 显示历史记录
来撤销你的更改。git rebase
和git push -f
时出现问题,但是这个方法真正拯救了我,因为该提交记录已经从本地和远程仓库中删除。最安全的方法是
git reflog
或 git reflog | grep your_phrase
git checkout -b your-branch HEAD@{<your-number>}
很遗憾,Git太不可靠了 :( 我刚刚丢失了两天的工作 :(
在提交之前最好手动备份任何东西。我刚刚执行了“git commit”,但是Git没有任何提示就摧毁了我的所有更改。
我吸取了教训 - 下次先备份,然后再提交。永远不要相信Git。
git stash; git stash drop
命令吗? - TheTechRobo the Nerd
git reflog
,找到你的提交,然后运行git cherry-pick <commit-hash>
。 - undefined