How to Conveniently Sync a File Between Two Git Repositories

How to conveniently sync a file between two git repositories

The only other alternative would be a post-commit hook on repoA, which would, on each commit:

  • check if the file is part of said commit
  • copy it in repoB with the right path.

Does syncing the .git repo between computers cause an issue?

I've kept my code checkouts in Dropbox now for 5 years writing code most every day. I use this to sync these changes between my desktop and laptop. This includes doing large refactorings that change dozens of files at a time.

Very rarely I have encountered problems, though they have a couple of times been severe (a corrupted repository) so you need to push early and often. Those rare problems are well worth the incredible convenience of being to continue work from right where you left off when switching between machines.

I've also tried this with Transporter with much, much less success. The Transporter file watcher doesn't seem as good, which led to conflicts every few weeks. Additionally, the syncing is much slower—whereas Dropbox seems to get things matched within a few seconds if both machines are up on the same network, Transporter had a median time of 30 seconds, but sometimes up to a few minutes.

How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?

Copy the diff file to the root of your repository, and then do:

git apply yourcoworkers.diff

More information about the apply command is available on its man page.

By the way: A better way to exchange whole commits by file is the combination of the commands git format-patch on the sender and then git am on the receiver, because it also transfers the authorship info and the commit message.

If the patch application fails and if the commits the diff was generated from are actually in your repo, you can use the -3 option of apply that tries to merge in the changes.

It also works with Unix pipe as follows:

git diff d892531 815a3b5 | git apply


Related Topics



Leave a reply



Submit