求大神帮看下怎么恢复误删并 push 的文件(附操作记录)

2014-04-15 01:52:09 +08:00
 kchum
真是No zuo no die。
前段时间刚上手git,就用来部署线上代码。
其实这段时间还都挺好的,不用每次修改都scp。

但今天不知道怎么会删掉了一些文件。
开了ss,Gist还是打开很慢,各位将就看下我的操作记录吧

我的思路很简单,服务器运行这么些天,增加了不少图片,是没有放进远端仓库的。
先在服务器端git commit -a了这些图片,然后本地仓库git pull,本地修改代码后git commit -a,然后本地git push。不知道我思路哪里出错了。。(没错,我按照svn的思路了。。)

操作记录如下
服务器:

1.git commit -am 'update';
确实有几百张图片加入仓库了

2.git status
# On branch master
nothing to commit (working directory clean)
说明已经没有新的改动了

本地操作
1.git pull
remote: Counting objects: 254, done.
remote: Compressing objects: 100% (206/206), done.
remote: Total 206 (delta 20), reused 0 (delta 0)
Receiving objects: 100% (206/206), 12.70 MiB | 76.00 KiB/s, done.
Resolving deltas: 100% (20/20), completed with 20 local objects.
From xxx:/www/web/xxxcom/
51c91d8..fe6c6c6 master -> origin/master
Updating 51c91d8..fe6c6c6
error: Your local changes to the following files would be overwritten by merge:
public_html/data/config.inc.php
public_html/data/tasks.inc.php
Please, commit your changes or stash them before you can merge.
Aborting

因为config.inc.php,tasks.inc.php是数据库配置,我之前用git undate-index --assume-unchanged file忽略掉,所以提示Aborting

2.修改bug,然后git commit -am 'fix images not show in detail'
[master 5aaf3a2] fix images not show in detail
4 files changed, 19 insertions(+), 4 deletions(-)
没错,是改了4个文件

3.git push
To xxx:/www/web/xxxcom/.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'xxx:/www/web/xxxcom/.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
失败了,猜想大概是因为本地git pull Abort的那2个文件

4.git push -f origin master
尝试性的这样提交。。然后就悲催了
Counting objects: 21, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 1.10 KiB | 0 bytes/s, done.
Total 10 (delta 8), reused 0 (delta 0)
remote: Push to checked out branch refs/heads/master
remote: W:unstaged changes found in working copy
remote: W:stashing dirty working copy - see git-stash(1)
remote: Saved working directory and index state On (no branch): dirty working copy before update to 5aaf3a2ea32bb30cf367dcf713a2031dee00f75e
remote: HEAD is now at fe6c6c6 update
remote: trapped 9133
remote: Updating working copy
remote: M public_html/app/goods.app.php
remote: M public_html/app/shop.app.php
remote: M public_html/app/cart.app.php
remote: M public_html/data/config.inc.php
remote: D public_html/data/files/store_44/goods_1/201404101820016058.jpg
remote: D public_html/data/files/store_44/goods_10/201403301603305815.jpg
***
remote: D 100多个文件
***
remote: HEAD is now at 5aaf3a2 fix images not show in detail
To xxx:/www/web/xxxcom/.git
+ fe6c6c6...5aaf3a2 master -> master (forced update)
然后服务器上的remote: D的文件都删除了。。。
我想不明白,本地也没有删除啊,怎么就push会有呢?

5.git status
On branch master
Your branch is up-to-date with 'origin/master'.

本地仓库,远端仓库项目目录已经备份(吃大亏了,必须这么做)。

尝试解决方法:
1.我尝试性在服务器目录git log

commit 5aaf3a2ea32bb30cf367dcf713a2031dee00f75e
Author: xxxx <xxxx@xxxx.com>
Date: Mon Apr 14 17:04:37 2014 +0800

fix images not show in detail

commit 51c91d833a62709713d6ff1be6f48aa3ec14fe4a
Author: xxxx <xxxx@xxxx.com>
Date: Fri Apr 4 14:36:00 2014 +0800

fix htaccess

居然没有17:04:37前的 'update',费解。。。

2.单个文件也尝试过,没用
git checkout deleted-file
#error: pathspec 'deleted-file' did not match any file(s) known to git.

3.git ls-files –deleted
没有删除文件

弄了一晚上,没找到解决方案,烦啊。

请问下该如何恢复,或者可以尝试哪些方法恢复。麻烦写清楚点步骤。。


回复均送金币。
只能以此为报,
不能捡肥皂。

谢谢大家看完这么长操作记录。
4598 次点击
所在节点    git
4 条回复
lijinma
2014-04-15 02:38:59 +08:00
服务器上使用:

git reflog 查找 'update'

找到对应的commit 点;

服务器只要你提交过,就不用怕,git reflog 记录你每一次操作过程;
kchum
2014-04-15 03:01:20 +08:00
@lijinma Cool!已解决!
lightening
2014-04-16 01:54:16 +08:00
嗯,楼上reflog正解。但是你永远不要force push到master。
mengzhuo
2014-04-16 07:24:03 +08:00
而且建议不要reset --hard
因为这样会丢失改动后的代码
----------
p.s. 谁用tig,gitg谁知道哈哈哈~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/108584

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX