• 请不要在回答技术问题时复制粘贴 AI 生成的内容
theSong
V2EX  ›  程序员

项目在本地写了很久,没有 commit 过,今天打开的时候从很早之前的一个 commit 版本打开了,咋恢复

  •  
  •   theSong · Aug 1, 2021 · 6984 views
    This topic created in 1924 days ago, the information mentioned may be changed or developed.

    今天脑抽,webstorm 打开的时候清了下启动项目那里的缓存,发现代码变成了很久之前的一个版本,git history 是很久之前的了。 能恢复吗?编辑器本身的缓存打开,而不是 git 的某一个版本打开,咋操作?

    Supplement 1  ·  Aug 2, 2021
    找到了,谢谢大家🙏
    51 replies  •  2021-08-03 08:32:47 +08:00
    Zhuzhuchenyan
    Zhuzhuchenyan
    Aug 1, 2021❤️1
    1
    看一下 webstorm 的 local history,或许还能救

    没有 stash 的话基本没办法了
    raysonx
    raysonx
    Aug 1, 2021 via iPhone
    2
    git reflog 找找之前的 commit 记录
    raysonx
    raysonx
    Aug 1, 2021 via iPhone
    3
    如果没有 commit 那就没了
    hullopanda
    hullopanda
    Aug 1, 2021
    4
    这每天不 commit 是啥好习惯啊
    尝试恢复 webstorm 的数据,从 windows 磁盘层面去恢复。
    tonytonychopper
    tonytonychopper
    Aug 1, 2021 via iPhone
    5
    每天 commit 才是好习惯
    liuzhaowei55
    liuzhaowei55
    Aug 1, 2021 via Android
    6
    应该随手 commit 就像随手 Ctrl + S 一样
    yitingbai
    yitingbai
    Aug 1, 2021
    7
    一楼说的对, webstorm 自身有文件历史记录, 可以保留一段时间, 可以找回来
    KMpAn8Obw1QhPoEP
    KMpAn8Obw1QhPoEP
    Aug 1, 2021 via Android
    8
    @liuzhaowei55 如果写了一半没写完的呢 也要 commit 吗?有点强迫症 每次都想把一个完整的写完 一切都干干净净了再 commit 觉得写了一半像草稿的 commit 之后有记录 心里总有疙瘩。。
    yitingbai
    yitingbai
    Aug 1, 2021
    9
    @enchilada2020 提交记录写清楚不就行了, 什么临时保存, **功能尚未完成, 这种强迫症要不得
    Rache1
    Rache1
    Aug 1, 2021
    10
    Local History 也不是万能的 😂

    [Local History]( https://www.jetbrains.com/help/webstorm/local-history.html)

    > Note that Local History is not a replacement for a proper version control for long-term versioning. Local History is cleared when you install a new version of WebStorm. It also has a [retention period]( https://www.jetbrains.com/help/webstorm/local-history.html#local-history-retention) and maximum size, so revisions are not guaranteed to persist.
    512357301
    512357301
    Aug 1, 2021 via Android
    11
    @enchilada2020 commit 的目的就是记录你的思路,开源嘛,让别人也能知道你的代码变动过程。
    一次性完整写完再 commit 大可不必,一次性完整写完我觉得那应该叫发版或者大版本,这里面肯定应该有无数个小版本来支撑的
    raysonx
    raysonx
    Aug 1, 2021 via iPad❤️1
    12
    @enchilada2020 完整写完后可以把之前的 squash 成一个 commit 再提交
    ClericPy
    ClericPy
    Aug 1, 2021
    13
    wip commit 了解一下...
    ClericPy
    ClericPy
    Aug 1, 2021
    14
    看错了... 已经覆盖了的话只能指望 IDE 自带的撤回了, 我撤回过...
    pengtdyd
    pengtdyd
    Aug 1, 2021
    15
    项目在本地写了很久,没有 commit 过。就这句话我觉的你还真是个人才
    WenhaoWu
    WenhaoWu
    Aug 1, 2021 via Android
    16
    commit --amend 了解一下
    mercury233
    mercury233
    Aug 1, 2021
    17
    我也不喜欢每天 commit 但我会 add
    forgottencoast
    forgottencoast
    Aug 1, 2021❤️2
    18
    @enchilada2020
    搞一个开发分支,弄好了,合并成一个 commit 也可以解决有强迫症的问题。
    正常是每次完成一个小改动就 commit 。
    中途的临时保存可以用 stash,这个不是 commit 。
    CEBBCAT
    CEBBCAT
    Aug 2, 2021
    19
    听起来有点不合理,是不是打开错项目了?不然的话我怀疑是你没有描述清楚,Intellij 这些都是有自动保存的,比如窗口失焦,更别说关闭程序了。

    清理缓存不可能把代码都清理掉的
    theSong
    theSong
    OP
    Aug 2, 2021
    20
    @Zhuzhuchenyan 找着了,还好 stash 了
    theSong
    theSong
    OP
    Aug 2, 2021
    21
    @tonytonychopper 你说的对,以后再也不敢了
    theSong
    theSong
    OP
    Aug 2, 2021
    22
    @pengtdyd 我错了。。。
    theSong
    theSong
    OP
    Aug 2, 2021
    23
    @CEBBCAT 我重新打开了另一个项目,说我这俩项目是同一个,不能同一窗口打开,然后我就同一窗口打开了。。。
    theSong
    theSong
    OP
    Aug 2, 2021
    24
    @Rache1 还好是当天的,而且只损失了 nodemodules 里,重新 install 了就好了
    techphoebe
    techphoebe
    Aug 2, 2021 via iPhone❤️1
    25
    @enchilada2020 每天提交,最后 rebase 一下把中间的提交干掉
    tonytonychopper
    tonytonychopper
    Aug 2, 2021 via iPhone
    26
    @enchilada2020 这其实就涉及到功能的拆解问题了
    limbo0
    limbo0
    Aug 2, 2021
    27
    最后的机会是开 timemachine, 惨痛的经理
    msg7086
    msg7086
    Aug 2, 2021 via Android
    28
    功能完成的时候重新整理提交不是常规操作么。
    simo
    simo
    Aug 2, 2021
    29
    有 reflog 就 git 恢复,没有只能尝试文件恢复了
    simo
    simo
    Aug 2, 2021
    30
    看到找回了。。。
    liuzhaowei55
    liuzhaowei55
    Aug 2, 2021❤️1
    31
    @enchilada2020 如果你自己的 feature 分支,并且不会有各种 lint 那就 commit,并且 push,电脑说不定什么情况就阵亡了。
    rationa1cuzz
    rationa1cuzz
    Aug 2, 2021
    32
    没有 add reflog 找不到基本上没戏,长长记性吧
    swaggeek
    swaggeek
    Aug 2, 2021
    33
    @enchilada2020 没写完提交,可以下一次提交的时候用 commit amend 的啊。
    landfill
    landfill
    Aug 2, 2021 via Android
    34
    没写完也 commit 写完了 rebase squash 就可以合并
    Macv1994
    Macv1994
    Aug 2, 2021
    35
    JB 家的 IDE 好像都有 local history 应该可以恢复 我上周也是手贱执行了 git restore app/* 结果第二天打开修改全没了 从 local history 恢复了
    RRRoger
    RRRoger
    Aug 2, 2021
    36
    local history 是个好东西
    acmore
    acmore
    Aug 2, 2021
    37
    @enchilada2020 Commit 本就不是用于搞一个完整的事的,就是 Feature 层面的 Ctrl + S 而已。真正表述一件完整事情的是 Pull-Request / Squashed Commit
    nightwitch
    nightwitch
    Aug 2, 2021
    38
    理解错了 commit 的含义。。commit 并不代表某个功能完成,可以理解成 git 层面的保存文件,完成功能的话应该打 release 包和 tag 了。
    commit 记录可以 squash 的,平时在 dev 分支开发,开发完了以后把所有的更改 squash 一下提交到 master 分支,这样 master 分支始终是干净的,Dev 分支又保留了很多历史记录。
    no1xsyzy
    no1xsyzy
    Aug 2, 2021
    39
    squash 和 WIP commit 都有一个问题:如果后来发现一个 bug 需要 bisect 的话,
    1. squash 只能发现在这一个大提交之中。
    2. WIP 你需要在一堆提交中找到恰当的可用提交。
    这都是不可接受的,所以应当采用细致的功能划分,每次只动一个部分。

    顺便还有一个命令是 git fsck --lost-found
    可能可以找到未提交被 reset 掉的内容。
    xuanbg
    xuanbg
    Aug 2, 2021
    40
    每修一个 bug 或者新加一个功能都要 commit 一次才是好习惯
    ThanksSirAlex
    ThanksSirAlex
    Aug 2, 2021❤️1
    41
    @enchilada2020 git stash,或者 commit 掉之后再 git reset
    CharAct3
    CharAct3
    Aug 2, 2021
    42
    function gfix(){ git reset --soft HEAD~${1-1} && git commit --amend -C HEAD; }

    加一个这个命令,随时 commit,要 push 前直接 gfix <number> 就可以快速 squash commit 了
    libook
    libook
    Aug 2, 2021
    43
    一个 branch 尽可能包含一个细粒度的 feature 或 fix,可以随时 commit 和 push,合并的时候可以用 rebase 把 branch 的所有 commit 合并为一个 commit 来合并到其他分支上。
    root8080
    root8080
    Aug 2, 2021
    44
    看到这个帖子 吓得我赶紧先 commit 下😅 三天没提交了
    HangoX
    HangoX
    Aug 2, 2021
    45
    试试 vscode 打开试试,jetbrains 有个 bug,会显示缓存,显示和看到不一样
    nacosboy
    nacosboy
    Aug 3, 2021 via iPhone
    46
    随时 commit,最后 push 可以 rebase -i 精简 commit
    theSong
    theSong
    OP
    Aug 3, 2021
    47
    @simo 哈哈,是的。补救措施看了这么多,还是养成及时保存的习惯最好
    theSong
    theSong
    OP
    Aug 3, 2021
    48
    @nacosboy 好的,谢谢,学到了很多没见过的指令
    theSong
    theSong
    OP
    Aug 3, 2021
    49
    @root8080 😄
    theSong
    theSong
    OP
    Aug 3, 2021
    50
    @Macv1994 是的,感谢 JB 。。
    theSong
    theSong
    OP
    Aug 3, 2021
    51
    @CharAct3 这个命令直接在终端输入吗
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   713 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 94ms · UTC 19:52 · PVG 03:52 · LAX 11:52 · JFK 14:52
    ♥ Do have faith in what you're doing.