2

I have a git repository on a usb stick and want to copy it to my hard drive. I have tried copying it over with rsync -rXP, cp and the gui in ubuntu but on the copied version all commits seem to have vanished. When I type git log or git reflog, I get

fatal: your current branch 'master' does not have any commits yet

The file .git/logs/HEAD shows all commits but git can't see them.

How do I move the repository over without losing any data?

EDIT1+3+5: commands used:

  • rsync -rXP usb_folder/ hdd_folder
  • cp -rp usb_folder hdd_folder (hdd_folder is located in /home)
  • rsync -a usb_folder/ hdd_folder
  • change ownership with sudo chown and sudo chmod -v -R a+rwx usb_folder + copying. File permissions didn't actually change though (turns out this doesn't work on fat32 filesystems, should've known -.-')

When I copy the repository to a new folder on the same usb stick the commits are still there

EDIT2:

I don't want to use git clone for the reasons mentioned here and because I have a bunch of untracked files I need for testing

EDIT4:

Repository may have been created with git for Windows. Problem with windows line-endings or version? The commits are present on the usb stick though

EDIT5:

It seems to be a problem with the filesystem (usb stick is fat32) and/or git for windows (repository was probably created with git for windows). I can copy the repository to a windows 10 pc and git log within the git bash will show all commits correctly. If I use the ubuntu subsystem to check, all commits are gone (though some files are staged for some reason)

KJoke
  • 59
  • 6
  • What are the exact commands you tipped in? – ckruczek Feb 19 '18 at 12:23
  • `rsync -rXP usb_folder/ hdd_folder`, `cp -rp usb_folder hdd_folder`. Not using the X (rsync) and p (cp) flag gets the same result. If I copy the folder to a folder on the same usb stick the commits are still there – KJoke Feb 19 '18 at 12:32
  • 4
    `git clone` will do this. – Don Branson Feb 19 '18 at 12:42
  • I can't reproduce this. I will agree on @DonBranson comment. – ckruczek Feb 19 '18 at 12:45
  • @DonBranson I don't want to use `git clone` for the reasons listed [here](https://stackoverflow.com/questions/5851966/moving-a-git-repo-to-a-second-computer/5852283#5852283) – KJoke Feb 19 '18 at 12:45
  • Okay. Have you tried `rsync -a`? – Don Branson Feb 19 '18 at 12:49
  • 2
    You should mention in your question why you don't want to do this the normal way. – Don Branson Feb 19 '18 at 12:50
  • If `rsync -a` doesn't work, check your file permissions. You may need to `chmod` everything. – Don Branson Feb 19 '18 at 12:55
  • I cannot reproduce the issue, was the repository created on the USB on the first place or did was it copied from a HDD to the USB stick first? + I know you don't want to `clone` but if it's only for non-tracked files, what about `git remote add stick /media/path/to/stick && git pull origin && cp /media/path/to/stick/* -r ./` ? – Arount Feb 19 '18 at 13:05
  • @DonBranson `rsync -a` doesn't work. I've tried to chmod everything to 0777 but I'm not sure if that worked...I tried `chmod -v -R a+rwx usb_folder` and the output said it changed some permissions but according to `ls -la` the permissions didn't actually change to 0777. All permissions remain at either 0644 or 0755...either way, it still doesn't work – KJoke Feb 19 '18 at 13:34
  • @Arount I think it was created on the usb and it might have been with git for windows. Could it be a problem with windows line endings within the git files? – KJoke Feb 19 '18 at 13:39
  • @KJoke Maybe the newlines, but surelly the files' rights too.. I'm really not sure but maybe different versions of git could create issues. You really should clone first, then copy missing / untracked files I think. – Arount Feb 19 '18 at 13:40
  • Well, `sudo chown`, then, to make yourself owner. – Don Branson Feb 19 '18 at 13:54

1 Answers1

1

Check the versions of Git on your source vs your destination.

While the basic Git repository format hasn't changed much, some important things have. Possibly most importantly, packfiles are much more prevalent. Particularly packed refs might cause Git to fail to see master.

If they're significantly different, ensure they match, probably by upgrading the destination.

Clone it and fix it.

If that doesn't work, use git clone. Then fix the clone to your liking.

While a simple cp -a is usually sufficient to copy a git repo, there could be any number of issues causing your problem. Since you're moving from Windows to Unit it could be line endings. It could be permissions. It could be that your source and destination git versions are so different your Ubuntu version doesn't fully understand it.

git clone will take care of these sorts of internal git repository formatting issues for you. Adjusting the clone to your liking will take far less time and effort than figuring out why the copy isn't working.

You probably shouldn't be copying your repo to another machine if it's in such active development that you need things like the reflog and stash. If it's a big concern, keep the old repo around for a while.

Copying an active dev repo is pretty rare, so doing it manually isn't a big deal. If you find yourself doing it a lot, after I'd question what's going on with your workflow, you could write a script.

As for fixing the individual concerns with cloning an active repo, here's how to handle them.

It creates remote-tracking branches for each branch in the cloned repository.

git remote rm origin

Any remote branches and other refs are completely ignored.

git remote -v and recreate them with git remote add. You probably only have one.

You don't get your hooks if you had any and you might forget that you had them!

Copy .git/hooks/.

Even better, use the same procedure you'd use when cloning a fresh repository to apply your hooks. Either a project-wide procedure, or your own personal procedure. If hooks are important to you or your project, there should be a procedure to apply, update, and restore them.

This is also why I'm not a big fan of client hooks being part of the workflow. They're fine for personal tailoring, but ensuring everyone has the hooks is fraught.

You cannot get "lost" commits etc using git reflog or other means.

(You'll also lose the stash.)

You may be able to copy .git/logs/, but I'm not 100% sure.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • git clone left me with a detached head and the master branch gone. What did work however was `git bundle create repo.bundle --all` [(source)](https://stackoverflow.com/questions/28522089/how-can-i-copy-my-git-repository-from-my-windows-machine-to-a-linux-machine-via) onto the usb, copy the bundle to my hdd and then `git clone` – KJoke Feb 22 '18 at 22:50
  • Other than the `git clone` part your answer was very helpful. Would it be proper for you to incorporate this into your answer so I can close this question (fine by me)? Or would I have to create an answer myself? I'm rather new here so I'm not quite sure how to handle this. – KJoke Feb 22 '18 at 23:14
  • @KJoke Yeah. Go ahead an [edit the question](https://stackoverflow.com/posts/48873705/edit) with your experiences with `git bundle`. Probably after "If that doesn't work, use git clone." like "And if *that* doesn't work because ... , use `git bundle ...`" – Schwern Feb 22 '18 at 23:27