舞台文件,然后远程添加,然后拉 - 我的文件不见了 [英] Stage files, then remote add, then pull - and my files are gone

查看:77
本文介绍了舞台文件,然后远程添加,然后拉 - 我的文件不见了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在一个新的回购中失去了我的下午的工作。以下是我所做的:


  1. 在本地创建了一个新项目并完成了一些工作。 repo on github

  2. git init

  3. git add src

  4. git remote add origin git@github.com:Synesso / memx.git

  5. git pull origin master

  6. git add .gitignore

  7. git commit -m'updated ignore'

  8. git push origin master

我的本​​地仓库和github仓库只有两个提交。初始提交(由github在项目创建时创建)和第二个只包含文件 .gitignore



在步骤4中添加的文件( git add src )不存在。它们也不会出现在舞台上。



当您执行 git pull 时,您是否丢失了分段文件?我可以检索它们吗?



当前状态:

  $ git状态
#在分支大师
上没有提交(工作目录干净)

我的inital add 不在引用日志中。

  $ git reflog 
c80135d HEAD @ {0}:结帐:从999d128ea4e6969f9eacbceebb5f857f2aa5abb0转到主
999d128 HEAD @ {1}:结帐:从主人移动到HEAD〜1
c80135d HEAD @ {2}:结帐:从999d128ea4e6969f9eacbceebb5f857f2aa5abb0转到master
999d128 HEAD @ {3}:checkout:从master移动到999d128ea4e6969f9eacbceebb5f857f2aa5abb0
c80135d HEAD @ {4}:commit(修改):updated忽略
28b4f90 HEAD @ {5 }:commit:updated忽略
999d128 HEAD @ {6}:初始拉

history 显示我添加了 src 文件夹,但没有提交它:

  223 git init 
225 git add src
229 git add project / B uild.scala
234 git remote add origin git@github.com:Synesso / memx.git
250 git pull origin master

我知道git会抱怨,如果你试图用脏文件存在。但是,这样做可以消除阶段性文件的拖动效果。这似乎是错误的。






我刚刚再次测试了这个过程,是的,它销毁了分段文件。

  jem @ jem-usb:〜/ projects $ mkdir x 
jem @ jem-usb:〜/ projects $ cd x
jem @ jem-usb:〜/ projects / x $ git init
在/home/jem/projects/x/.git/
jem @ jem-usb中初始化的空的Git仓库:〜/ projects / x $ echohi> hello.world
jem @ jem-usb:〜/ projects / x $ git add hello.world $ b $ jem @ jem-usb:〜/ projects / x $ git status
#分支主人

#初始提交

#要提交的更改:
#(使用git rm --cached< file> ... )

#新文件:hello.world

jem @ jem-usb:〜/ projects / x $ ls -asl
total 24
4 drwxrwxr-x 3 jem jem 4096 Apr 28 20:56。
4 drwxr-xr-x 8 jem jem 4096 Apr 28 20:56 ..
4 drwxrwxr-x 7 jem jem 4096 Apr 28 20:56 .git
12 -rw-rw- r-- 1 jem jem 3 4月28日20:56 hello.world
jem @ jem-usb:〜/ projects / x $ git remote add origin git@github.com:Synesso / memx.git
jem @ jem-usb:〜/ projects / x $ git reflog
致命错误:错误的缺省修订版本'HEAD'
jem @ jem-usb:〜/ projects / x $ git pull origin master
远程:计数对象:7,完成。
remote:压缩对象:100%(5/5),完成。
remote:合计7(delta 0),重用3(delta 0)
开箱对象:100%(7/7),完成。
从github.com:Synesso/memx
*分支大师 - > FETCH_HEAD
jem @ jem-usb:〜/ projects / x $ ls -asl
total 36
4 drwxrwxr-x 3 jem jem 4096 Apr 28 20:53。
4 drwxr-xr-x 8 jem jem 4096 Apr 28 20:52 ..
4 drwxrwxr-x 8 jem jem 4096 Apr 28 20:53 .git
12 -rw-rw- r-- 1 jem jem 59 Apr 28 20:53 .gitignore
12 -rw-rw-r-- 1 jem jem 9 Apr 28 20:53 README.md
jem @ jem-usb:〜 / projects / x $ git reflog
c80135d HEAD @ {0}:initial pull

文件 hello.world 被删除而没有任何警告。

解决方案

在不使用github的情况下使用两台主机(在这里重命名为hostB,这是remote,hostA是local): p> hostB $ cd / tmp; mkdir回购; cd回购; git init
在/tmp/repo/.git/
hostB $中初始化的空Git仓库:>的.gitignore;回声这是一个自述> README.md
hostB $ git add。; git commit -m initial
[master(root-commit)58d43bd] initial
1个文件已更改,1个插入(+),0个删除( - )
创建模式100644 .gitignore
创建模式100644 README.md

hostA $ cd / tmp; mkdir回购; cd回购; git init
在/tmp/repo/.git/
hostA $ echo中初始化的空Git仓库hi> hello.world
hostA $ git add hello.world
hostA $ git status
#分支大师

#初始提交

#要提交的更改:
#(使用git rm --cached< file> ...停用)

#new file:hello.world

hostA $ git remote add origin ssh://hostB.dom.ain/tmp/repo
hostA $ git pull origin master
remote:计数对象:4,完成。
remote:压缩对象:100%(2/2),完成。
remote:合计4(delta 0),重用0(delta 0)
开箱对象:100%(4/4),完成。
从ssh://hostB.dom.ain/tmp/repo
* branch master - > FETCH_HEAD
hostA $ ls
README.md

重要:您可以使用 git fsck --lost-found 来恢复您的暂存文件:

  hostA $ git fsck --lost-found 
检查对象目录:100%(256/256),完成。
dangling blob 45b983be36b73c0788dc9cbcb76cbb80fc7bb057
hostA $

果然,如果您检查blob(现在已经恢复到 .git / lost-found / other ),那些将会丢失你的东西。 (任何目录结构都将被删除,你必须弄清楚哪个文件是哪个文件并将它们重新放到你想要的地方。)



有趣的是,如果你 git fetch origin 后跟 git merge origin / master ,它抓取原始的初始版本(带有.gitignore和来自hostB的README.md文件),并保留要提交的更改。支持 git fetch 后跟 git merge 的另一个原因是? : - )

I seem to have lost my afternoon's work in a new repo. Here's what I did:

  1. Created a new project locally and did some work.
  2. Created a repo on github
  3. git init
  4. git add src
  5. git remote add origin git@github.com:Synesso/memx.git
  6. git pull origin master
  7. git add .gitignore
  8. git commit -m 'updated ignore'
  9. git push origin master

Both my local repo and the github repo have only two commits. The initial commit (made by github on project creation) and a second one that includes only the file .gitignore.

The files added in step 4 (git add src) are not present. Nor do they appear to be staged.

Do you lose staged files when you do a git pull? Can I retrieve them somehow?

Current state:

$ git status
# On branch master
nothing to commit (working directory clean)

My inital add is not in the reflog.

$ git reflog
c80135d HEAD@{0}: checkout: moving from 999d128ea4e6969f9eacbceebb5f857f2aa5abb0 to master
999d128 HEAD@{1}: checkout: moving from master to HEAD~1
c80135d HEAD@{2}: checkout: moving from 999d128ea4e6969f9eacbceebb5f857f2aa5abb0 to master
999d128 HEAD@{3}: checkout: moving from master to 999d128ea4e6969f9eacbceebb5f857f2aa5abb0
c80135d HEAD@{4}: commit (amend): updated ignore
28b4f90 HEAD@{5}: commit: updated ignore
999d128 HEAD@{6}: initial pull

history shows that I added the src folder, but did not commit it:

223  git init
225  git add src
229  git add project/Build.scala
234  git remote add origin git@github.com:Synesso/memx.git
250  git pull origin master

I know git will complain if you try to pull with dirty files present. But it's OK with doing a pull that will obliterate staged files? That seems wrong.


I've just tested this process again and yes, it destroys staged files.

jem@jem-usb:~/projects$ mkdir x
jem@jem-usb:~/projects$ cd x
jem@jem-usb:~/projects/x$ git init
Initialized empty Git repository in /home/jem/projects/x/.git/
jem@jem-usb:~/projects/x$ echo "hi" > hello.world
jem@jem-usb:~/projects/x$ git add hello.world
jem@jem-usb:~/projects/x$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   hello.world
#
jem@jem-usb:~/projects/x$ ls -asl
total 24
 4 drwxrwxr-x 3 jem jem 4096 Apr 28 20:56 .
 4 drwxr-xr-x 8 jem jem 4096 Apr 28 20:56 ..
 4 drwxrwxr-x 7 jem jem 4096 Apr 28 20:56 .git
12 -rw-rw-r-- 1 jem jem    3 Apr 28 20:56 hello.world
jem@jem-usb:~/projects/x$ git remote add origin git@github.com:Synesso/memx.git
jem@jem-usb:~/projects/x$ git reflog
fatal: bad default revision 'HEAD'
jem@jem-usb:~/projects/x$ git pull origin master
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 7 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (7/7), done.
From github.com:Synesso/memx
 * branch            master     -> FETCH_HEAD
jem@jem-usb:~/projects/x$ ls -asl
total 36
 4 drwxrwxr-x 3 jem jem 4096 Apr 28 20:53 .
 4 drwxr-xr-x 8 jem jem 4096 Apr 28 20:52 ..
 4 drwxrwxr-x 8 jem jem 4096 Apr 28 20:53 .git
12 -rw-rw-r-- 1 jem jem   59 Apr 28 20:53 .gitignore
12 -rw-rw-r-- 1 jem jem    9 Apr 28 20:53 README.md
jem@jem-usb:~/projects/x$ git reflog
c80135d HEAD@{0}: initial pull

The file hello.world was deleted without warning.

解决方案

I was able to reproduce this without using github, using two hosts (renamed here as hostB, which is the "remote", and hostA, which is the "local"):

hostB$ cd /tmp; mkdir repo; cd repo; git init
Initialized empty Git repository in /tmp/repo/.git/
hostB$ : > .gitignore; echo this is a readme > README.md
hostB$ git add .; git commit -m initial
[master (root-commit) 58d43bd] initial
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 README.md

hostA$ cd /tmp; mkdir repo; cd repo; git init
Initialized empty Git repository in /tmp/repo/.git/
hostA$ echo hi > hello.world
hostA$ git add hello.world
hostA$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   hello.world
#
hostA$ git remote add origin ssh://hostB.dom.ain/tmp/repo
hostA$ git pull origin master
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From ssh://hostB.dom.ain/tmp/repo
 * branch            master     -> FETCH_HEAD
hostA$ ls
README.md

Important: you can use git fsck --lost-found to recover your staged files:

hostA$ git fsck --lost-found
Checking object directories: 100% (256/256), done.
dangling blob 45b983be36b73c0788dc9cbcb76cbb80fc7bb057
hostA$ 

and sure enough, if you examine the blob(s) (which are now restored in .git/lost-found/other), those will have your lost stuff. (Any directory structure will be gone though, and you'll have to figure out which file is which and re-put them where you want them.)

Interestingly, if you git fetch origin followed by git merge origin/master, it grabs the origin's initial revision(s) (with the .gitignore and README.md files from hostB, in this case) and retains your "changes to be committed". Another reason to favor git fetch followed by git merge? :-)

这篇关于舞台文件,然后远程添加,然后拉 - 我的文件不见了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆