V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
clrss
V2EX  ›  git

用 git 踩了个坑

  •  
  •   clrss · 2021-04-13 14:21:16 +08:00 · 2281 次点击
    这是一个创建于 1099 天前的主题,其中的信息可能已经有所发展或是发生改变。

    事件顺序

    1. 添加文件 A -- commit1
    2. 一般操作 -- commit2~9
    3. git rm --cached A 并把 A 添加到 gitignore -- commit10
    4. git rebase -i commit2. 保持 commit10 不变
    5. 回放 commit10 时, 文件 A 就丢了...
    第 1 条附言  ·  2021-04-13 16:11:27 +08:00

    复现步骤总结错了.

    实际上是

    git rm A # and ignore A -- commit10
    mv /some/other/file A
    

    这个A没有备份的话, 应该真的找不回来.

    8 条回复    2021-04-15 10:21:16 +08:00
    yiXu
        1
    yiXu  
       2021-04-13 14:27:11 +08:00
    @clrss 没怎么看懂,能给出可复现的,所有操作的命令行代码吗? gist 给个最好,我想试试看。
    momocraft
        2
    momocraft  
       2021-04-13 14:32:56 +08:00
    commit10 删掉了 A, apply 时也删掉 A 不是正常的吗?

    你的期待是 apply 后 A 留在 worktree? 可是 commit 一级不保留这个信息
    xuanbg
        3
    xuanbg  
       2021-04-13 14:42:23 +08:00
    签出 commit9 不就回来了?
    hw93
        4
    hw93  
       2021-04-13 15:57:07 +08:00
    只要 commit 了,文件就不会丢
    clrss
        5
    clrss  
    OP
       2021-04-13 16:08:41 +08:00
    @xuanbg
    @hw93

    复现步骤总结错了.

    实际上是
    git rm A
    mv /some/other/file A
    dezng
        6
    dezng  
       2021-04-13 16:21:38 +08:00
    看你上面的操作,rebase 基于 commit2,commit1 还存在,A 文件肯定还在。即使 commit1 没了,一般通过 git reflog 也能找回。
    clrss
        7
    clrss  
    OP
       2021-04-13 16:54:17 +08:00
    @dezng 看附言, 实际上是 git rm A, 然后换了个同名文件. 我要的是后来换的同名文件.
    dezng
        8
    dezng  
       2021-04-15 10:21:16 +08:00
    @clrss commit1 还在,A 一定还在 git checkout commit1 -- path/to/A 你就看到原来的 A 文件了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2012 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:17 · PVG 00:17 · LAX 09:17 · JFK 12:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.