git 在存储和合并后丢失了提交

问题描述 投票:0回答:1

我昨天遇到了一个奇怪的问题。

  • 在分支“add-trans”上提交了更改,日志消息为“add transaction Final”
  • 签出了分支“react”,但一个文件被修改了,我不得不隐藏它
  • 隐藏文件
  • 签出分支“react”
  • 合并“add-trans”分支
  • 掉落了藏品。其实不需要。

到目前为止一切都很好 但后来意识到提交“添加事务最终”丢失了!

git log --graph --oneline --decorate

*   ea34d09 (react) Merge branch 'add-trans' into react
|\  
* | 49b3148 added color categorie
* | 5aaf600 ordering categories
| * 60d0bc5 (HEAD -> add-trans) add new transaction. draft 1
* | aef34ed merge categories and groups selection
|/  

该死!损失了 4 个小时的工作。 发生什么事了?!

我知道 git 不会轻易删除东西,所以我可以根据帖子找到丢失的哈希值:如何恢复 Git 中丢失的存储?

git log --graph --oneline --decorate --all $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

*   ea34d09 (react) Merge branch 'add-trans' into react
|\  
* | 49b3148 added color categorie
* | 5aaf600 ordering categories
| | *   5c96e57 WIP on (no branch): faa2f21 add transaction final
| | |\  
| | | * 558aef4 index on (no branch): faa2f21 add transaction final
| | |/  
| | * faa2f21 add transaction final
| |/  
| * 60d0bc5 (HEAD -> add-trans) add new transaction. draft 1
* | aef34ed merge categories and groups selection
|/  

我可以取回提交“faa2f21 add transaction Final”并正确合并,但我很困惑......谁能告诉我发生了什么?

git reflog 

ea34d09 HEAD@{9}: commit (merge): Merge branch 'add-trans' into react
0a5cd5a HEAD@{10}: checkout: moving from master to react
6ddb28d (master) HEAD@{11}: checkout: moving from faa2f21c33ce8da763e8d5580c718590318ed5c1 to master
faa2f21 (add-trans) HEAD@{12}: commit: add transaction final
60d0bc5 HEAD@{13}: checkout: moving from react to 60d0bc5a707cf626b416a66cad6df75dc167a18c
49b3148 HEAD@{14}: commit: added color categorie

非常感谢。

git git-merge git-stash
1个回答
0
投票

日志输出中的

WIP on (no branch)
表示您正处于
rebase
或冲突解决过程中,甚至可能是
merge --no-commit

Git 将允许您添加/修改提交,但在解决存储库状态之前,合并

add-trans
将使用分支的最后一个已知的良好状态。

真的,我不知道你的本地存储库的状态发生了什么。

git status
是你的朋友,可以告诉你很多关于你的存储库中发生的事情。

这里有一些可以尝试的命令:

git rebase --abort
git am --abort
git merge --abort
git bisect reset

一旦你拿回它,你就可以将你投入虚空的那个奇怪的东西拉到你的分支中:

git checkout add-trans
git cherry-pick faa2f21

然后重新合并到

react

更好的 Git 提示

如果您无法每次都输入

git status
,您可以使用
git-prompt.sh
将回购状态指示器添加到提示中:

# Add these lines to ~/.bashrc:
. /usr/lib/git-core/git-sh-prompt
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

您应该将

git-sh-prompt
行调整到它在系统上的位置。我刚刚包含了 Ubuntu 23.04 中的默认路径。

© www.soinside.com 2019 - 2024. All rights reserved.