git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
clrss
V2EX  ›  git

用 git 踩了个坑

  •  
  •   clrss · Apr 13, 2021 · 3280 views
    This topic created in 1855 days ago, the information mentioned may be changed or developed.

    事件顺序

    1. 添加文件 A -- commit1
    2. 一般操作 -- commit2~9
    3. git rm --cached A 并把 A 添加到 gitignore -- commit10
    4. git rebase -i commit2. 保持 commit10 不变
    5. 回放 commit10 时, 文件 A 就丢了...
    Supplement 1  ·  Apr 13, 2021

    复现步骤总结错了.

    实际上是

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

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

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

    你的期待是 apply 后 A 留在 worktree? 可是 commit 一级不保留这个信息
    xuanbg
        3
    xuanbg  
       Apr 13, 2021
    签出 commit9 不就回来了?
    hw93
        4
    hw93  
       Apr 13, 2021
    只要 commit 了,文件就不会丢
    clrss
        5
    clrss  
    OP
       Apr 13, 2021
    @xuanbg
    @hw93

    复现步骤总结错了.

    实际上是
    git rm A
    mv /some/other/file A
    dezng
        6
    dezng  
       Apr 13, 2021
    看你上面的操作,rebase 基于 commit2,commit1 还存在,A 文件肯定还在。即使 commit1 没了,一般通过 git reflog 也能找回。
    clrss
        7
    clrss  
    OP
       Apr 13, 2021
    @dezng 看附言, 实际上是 git rm A, 然后换了个同名文件. 我要的是后来换的同名文件.
    dezng
        8
    dezng  
       Apr 15, 2021
    @clrss commit1 还在,A 一定还在 git checkout commit1 -- path/to/A 你就看到原来的 A 文件了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   934 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 60ms · UTC 20:50 · PVG 04:50 · LAX 13:50 · JFK 16:50
    ♥ Do have faith in what you're doing.