代码之家  ›  专栏  ›  技术社区  ›  Greg Hewgill

如何在Git中恢复丢失的藏品?

  •  1437
  • Greg Hewgill  · 技术社区  · 17 年前

    我经常使用 git stash git stash pop 保存和恢复我的工作树中的更改。昨天我在我的工作树上做了一些改变,我把它们藏起来,然后我对我的工作树做了更多的改变。我想回去回顾一下昨天隐藏的变化,但是 流行音乐流行歌曲 显示为删除对关联提交的所有引用。

    我知道如果我使用 然后 .git/refs/stash包含 用于创建存储的提交的引用。和 .git/logs/refs/stash包含 全部藏匿。但是那些推荐信已经不见了 流行音乐流行歌曲 . 我知道提交仍在我的存储库中的某个地方,但我不知道它是什么。

    有没有一个简单的方法来恢复昨天的库存承诺参考?

    请注意,今天这对我来说并不重要,因为我有每日备份,可以返回昨天的工作树来获取更改。我问是因为一定有更简单的方法!

    19 回复  |  直到 17 年前
        1
  •  2301
  •   jpaugh JotaBe    7 年前

    git stash apply $stash_hash
    

    git branch recovered $stash_hash
    

    still have the hash value printed by git stash pop on screen

    git fsck --no-reflog | awk '/dangling commit/ {print $3}'
    

    git fsck --no-reflog | select-string 'dangling commit' | foreach { $bits = $_ -split ' '; echo $bits[2];}
    

    gitk

    gitk --all $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )
    

    the answer from emragins

    git log --graph --oneline --decorate

    git stash

        2
  •  653
  •   outis    14 年前

    git stash pop

    $ git stash pop
    [...]
    Dropped refs/stash@{0} (2ca03e22256be97f9e40f08e6d6773c7d41dbfd1)
    

    git stash drop

    git branch tmp 2cae03e

    git stash apply tmp
    git stash
    

        3
  •  246
  •   Wade Santa    17 年前

    $ git stash apply ad38abbf76e26c803b27a6079348192d32f52219
    

        4
  •  71
  •   Greg Hewgill    17 年前

    for ref in `find .git/objects | sed -e 's#.git/objects/##' | grep / | tr -d /`; do if [ `git cat-file -t $ref` = "commit" ]; then git show --summary $ref; fi; done | less
    

    for ref in `git fsck --unreachable | grep commit | cut -d' ' -f3`; do git show --summary $ref; done | less
    
        5
  •  71
  •   ANeves    8 年前

    git fsck --unreachable | grep commit | cut -d" " -f3 | xargs git log --merges --no-walk --grep=WIP
    

    -grep=WIP -grep=Tesselation

    WIP on mybranch: [previous-commit-hash] Message of the previous commit.

        6
  •  37
  •   Nathan Jones    17 年前

    git fsck --unreachable | grep commit git show <sha1>

    git cherry-pick -m 1 <sha1>

        7
  •  25
  •   ericbn    8 年前

    git fsck

    log-all

    git log --graph --decorate --pretty=oneline --abbrev-commit --all $(git fsck --no-reflogs | grep commit | cut -d' ' -f3)
    

    git update-ref refs/stash ed6721d
    

    -m

    git update-ref -m "$(git log -1 --pretty=format:'%s' ed6721d)" refs/stash ed6721d
    

    restash = !git update-ref -m $(git log -1 --pretty=format:'%s' $1) refs/stash $1
    
        8
  •  16
  •   Shaheen Ghiassy    13 年前

    git show $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' ) > ~/stash_recovery.diff
    

    git stash apply ad38abbf76e26c803b27a6079348192d32f52219
    
        9
  •  15
  •   emragins user2383049    10 年前

    gitk --all $(git fsck --no-reflog | Select-String "(dangling commit )(.*)" | %{ $_.Line.Split(' ')[2] })

        10
  •  12
  •   Can Tecim    9 年前

    $ git fsck --unreachable | grep commit | cut -c 20- | xargs git show | grep -B 6 -A 2 <name of the stash>

    $ git fsck --unreachable | grep commit | cut -c 20- | xargs git show

        11
  •  11
  •   RobbyD    9 年前

        12
  •  10
  •   Phil    13 年前

    git fsck --no-reflog | awk '/dangling commit/ {print $3}' > tmp_commits
    
    for h in `cat tmp_commits`; do git show $h | less; done
    

        13
  •  9
  •   Brad Feehan    10 年前

    git fsck --no-reflog | \
    awk '/dangling commit/ {print $3}' | \
    xargs git log --no-walk --format="%H" \
      --grep="WIP on" --min-parents=3 --max-parents=3
    

    git stash save "My newly created stash"

    git stash show

    git fsck --no-reflog | \
    awk '/dangling commit/ {print $3}' | \
    xargs git log --no-walk --format="%H" \
      --grep="WIP on" --min-parents=3 --max-parents=3 | \
    xargs -n1 -I '{}' bash -c "\
      git log -1 --format=medium --color=always '{}'; echo; \
      git stash show --color=always '{}'; echo; echo" | \
    less -R
    
        14
  •  9
  •   ΩmegaMan    9 年前

    awk grep Select-string

    • git fsck --unreachable | findstr "commit"
    • start cmd /k git show

    start cmd /k git show 8506d235f935b92df65d58e7d75e9441220537a4 start cmd /k git show 44078733e1b36962571019126243782421fcd8ae start cmd /k git show ec09069ec893db4ec1901f94eefc8dc606b1dbf1 start cmd /k git show d00aab9198e8b81d052d90720165e48b287c302e

    • git stash apply (your hash)

        15
  •  9
  •   Vivek Kumar    8 年前

    git fsck --unreachable
    

    git show hash
    

    git stash apply hash
    
        16
  •  4
  •   Ben    12 年前

    git stash

    $ git checkout somethingOld
    $ git stash pop
    ...
    nothing added to commit but untracked files present (use "git add" to track)
    Dropped refs/stash@{0} (27f6bd8ba3c4a34f134e12fe69bf69c192f71179)
    $ git checkout 27f6bd8ba3c
    $ git reset HEAD^    # Make the working tree differ from the parent.
    $ git stash # Put the stash back in the stack.
    Saved working directory and index state WIP on (no branch): c2be516 Some message.
    HEAD is now at c2be516 Some message.
    $ git checkout somethingOld # Now we are back where we were.
    

    git stash apply git stash pop bisect

    $ git reset --hard; git bisect good; git stash apply
    $ # Run tests
    $ git reset --hard; git bisect bad; git stash apply
    etc.
    
        17
  •  4
  •   miva2 Greg Bacon    10 年前

        18
  •  -1
  •   dorony    8 年前

    git reset HEAD --hard
    git checkout my_correct_branch
    git stash pop
    

        19
  •  -1
  •   Nike Kov    7 年前

    推荐文章