28

This question probably is based on my lack of understanding of the role of .gits and git repositories in general but:

Can I rsync a dir with content that I created with git init between machines ?

I have a repository on my laptop, and the only way to get it away from there is scp/rsync to a remote host, from which I can download it again. Could I rsync the complete directory structure between these hosts?

svrist
  • 7,042
  • 7
  • 44
  • 67

6 Answers6

33

There are a few possibilities:

  • You can just rsync the .git repository (or even whole repository together with working directory), provided that you don't have any activity in repository during rsyncing (same disclaimer as for using rsync:// protocol).

  • You can clone or fetch using deprecated rsync protocol (where repository URL / location looks like this: "rsync://host.xz/path/to/repo.git/"). Note that this protocol is deprecated, because if there is any activity in repository, you can get corrupted clone (or fetch).

    Also, as I have heard, it didn't work correctly in the presence of packed refs since 2007, and nobody noticed till recently. It will (it did) disappear in Git 2.8.

  • Or you can create git bundle, rsync it or scp it on other machine, and then clone or fetch from bundle.

Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
  • 2
    As I note [in my answer below](http://stackoverflow.com/a/1398041/6309), git `rsync://` protocol will *finally* disappear in git 2.8 only (March 2016, 10+ after starting being declared as "deprecated"!) – VonC Feb 18 '16 at 10:19
  • This no longer works - gives the error `git-over-rsync is no longer supported` – M.M Jun 25 '21 at 09:35
12

Yes, that is possible. IIRC, git uses a relative approach. So it's safe to sync it with another computer.

Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179
Ikke
  • 99,403
  • 23
  • 97
  • 120
  • 1
    It is possible (with the caveat that filesystem-based remotes and alternates might not work) if you don't do any work on the repository during rsync. – Jakub Narębski Sep 09 '09 at 16:28
6

Yes you can, but you should do so with a bare repository, as illustrated in this 2012 article:

$ git clone --bare ./projet projet.git
$ rsync -a --stats --delete ./projet.git/ votre.serveur:~/projets/projet.git/

Note: 6+ years later, git 2.8 (March 2016) will officially deprecate and fully remove the git rsync protocol!

See commit 0d0bac6 (30 Jan 2016) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 9f03176, 17 Feb 2016)

It turns out "git clone" over rsync transport has been broken when the source repository has packed references for a long time, and nobody noticed nor complained about it.

Commit 0d0bac6 from Jeff King (peff) has all the details.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    Bare repository is only needed when you want to push to that repo. Bit if you just copy the repo each time, you can just do it with a full repo. – Ikke Sep 09 '09 at 07:22
  • 3
    You can use rsync for full repository as well. There is nothing special about bare repository if you use rsync. – Milan Babuškov Sep 09 '09 at 07:23
  • @Ikke: true, I usually do that kind of operation with the intent of pushing to it. – VonC Sep 09 '09 at 07:25
6

You can rsync without any problems, but if you have some remotes declared with hostnames which are local to the machine (i.e. stored in /etc/hosts only) then those obviously won't work.

However, is there some reason why you don't use git itself to sync the content?

Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179
  • I was figuring that as my remote server doesnt have git I couldnt git push to it – svrist Sep 09 '09 at 07:27
  • 1
    True, if you want to use push via SSH, then you have to have git on server too (but you can have it installed locally, and use `--receive-pack` option to `git-push`, and similar option for fetch/pull/clone). If you want to push via HTTP, you need to have only web server with WebDAV, configured to allow push access for you (no git on server required). – Jakub Narębski Sep 09 '09 at 16:43
  • git clone is hanging for me and I can't find a solution on the web so rsync would help my cause but I too don't understand if some contents of `.git` are host-specific. – Sridhar Sarnobat Jun 04 '18 at 02:33
1

Yes, rsyncing the .git dir is possible and will result in a complete clone of the repository.

Bombe
  • 81,643
  • 20
  • 123
  • 127
0

Using rsync to backup .git is possible, but with one catch ( at least to myself) It can not be updated. The next time you try to rsync it again, you got massive object rename.. and you can not delete those.. Been not familiar with rsync or linux system, I purge the .git backup before I rsync it.

Which is:

> chmod -R 777 /path/of/your/backup/.git
> rm -fr /path/of/your/backup/.git
> rsync /path/of/your/origin/.git /path/of/your/backup/.git

I know, this is very taxing and stupid.. Please let me know is there a better way to do this..

Cyrus Ivan
  • 19
  • 3
  • ya if your trees in `.git` are the slightest bit different it will add a lot of data. – salotz Jun 08 '18 at 21:20
  • 2
    From `man rsync`: `--delete delete extraneous files from dest dirs`. That will delete Git's old object files (and anything else that has been deleted from the source) from the backup. You may also want `--recursive`. – Mark Haferkamp Jul 15 '20 at 02:38