Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sync directories containing git repository with unison

Tags:

git

unison

I want to sync a directory containing a git repo of my dotfiles on two machines. Both machines will make changes to the files in the repo. Normally I use unison for syncing directories but in this case the files in the .git directory diverge even when the directory contents are ostensibly the same. Unison throws up its hands and quits when it sees this.

$ unison dotfiles
changed  <-?-> changed    .git/FETCH_HEAD
changed  <-?-> changed    .git/ORIG_HEAD
changed  <-?-> changed    .git/index
changed  <-?-> changed    .git/logs/HEAD
changed  <-?-> changed    .git/logs/refs/heads/master
changed  <-?-> changed    .git/logs/refs/remotes/origin/master

A byte-by-byte sync here doesn't work because even with the same files and git state the internal git files may be different. How can I sync these directories? After the sync, all non-git files and the git state should be identical on the two machines. By git state I mean commit histories and staging but not necessarily every byte of every internal git file (e.g. .gitignore, .git). I don't know enough about git to go into more detail on what I mean by that but if that's unclear just comment.

I don't want to make any git commits as part of syncing. I see version control and synchronization as two separate issues here.

Putting the words git and sync into google gives results describing how to clone a repo. Just to be clear, none of the uses of "sync" above are referring to git actions.

like image 304
Praxeolitic Avatar asked Jul 25 '15 16:07

Praxeolitic


People also ask

Does unison use rsync?

Both Unison and rsync use the so-called "rsync algorithm," by Andrew Tridgell and Paul Mackerras, for performing updates. This algorithm streamlines updates in small parts of large files by transferring only the parts that have changed.

Is there a git-sync command?

git-sync is a command that pull a git repository to a local directory. It can be used to source a container volume with the content of a git repo.


1 Answers

Based on VonC's advise, I'm going to relax the requirement of live syncing the entire git state and simply ignore the .git directory with unison and just use git fetch.

Relevant part of my .unison/dotfiles.prf:

root = /home/khouli/dotfiles
root = ssh://other_machine//home/khouli/dotfiles
ignore = BelowPath .git

Note that BelowPath matches exactly the path .git. So depending on the project structure ignore = Name .git may be better suited. More details on pathing in Unison here.

Relevant part of my sync script:

unison dotfiles
(cd $HOME/dotfiles && git fetch)
ssh other_machine 'cd dotfiles && git fetch'

This syncs the files themselves and the commit history. It doesn't sync staging but that's not too important and it might not even be desirable. This also requires that both machines have an appropriate network connection for a git fetch. Otherwise git bundle like VonC suggests could be used.

This is just here incase anyone comes here from google and finds it useful. I'm not marking it as accepted because it'd be interesting if an answer showed up with a way to live sync a git repo.

like image 156
Praxeolitic Avatar answered Oct 05 '22 22:10

Praxeolitic