0

I often work on the same project on several computers. After some time, I need to make sure that the project files are synced and up to date with the latest changes on each computer. So I end up with two directories, A and B, that have the same contents, but the .git files do not match between the two directories, even if their contents are the same. I need to make sure that one directory (A) ends up with everything that B has, so that I can then delete B.

So I need a way to know that directory A/.git contains all the info that B/.git contains, even if the .git/* files do not compare. Here is what I came up with:

(git branch -av;git stash show -v)|md5sum

If I run this in both A and B, I should get the same result if both sides have the same branches and stashes. Is this sufficient to be sure that both repos have the same info? Or am I missing something else?

blakedude
  • 26
  • 2
  • 4
    Uh, syncing between different PCs, ain't that the main goal of git. – Simon Boudrias Aug 29 '13 at 17:25
  • What I am trying to avoid is overwriting one repo with another, and later realizing that I deleted a branch or something that only existed on the target PC. Obviously syncing files is easy, but I like to know first what I will be destroying in the process. – blakedude Aug 29 '13 at 17:51
  • 1
    Why overwrite though? Why not just `git fetch`? – Ajedi32 Aug 29 '13 at 18:08
  • I use git fetch all day. I end up writing code on several computers over the week and do all kinds of stuff on each one, making/deleting branches and such. At the end, I want to have one backup that contains everything. All I am asking is whether my command above will guarantee that both repos contain the same info, or is there more to it? If I have the same branches and stashes, is that all? – blakedude Aug 29 '13 at 18:12
  • To make this clearer, I have a number of branches, B1, B2, B3, B4. In order to make sure both computers are synced, I have to manually check each branch individually. If I forget to check and I overwrite one repo with another, data will be lost. I need a way to verify the whole repo, not just the working directory. – blakedude Aug 29 '13 at 18:24

1 Answers1

0

That's rather straightforward.

Suppose you have repos cloneA and cloneB originally cloned from the same shared repo.

  1. From within cloneA add the other clone as a new remote:

    $ git remote add cloneB file:///path/to/cloneB/.git

  2. Then just fetch from it:

    $ git fetch cloneB

This will copy all branches that exist only in cloneB over to cloneA with all the corresponding commits and objects, so techinally _cloneA can now be discarded. Don't remove the remote ref though since that will make it much harder to locate the copied over branches.

This does not copy any stashes though. However, if you need to copy the stashes as well you can look into this approach.

Community
  • 1
  • 1
Levi Haskell
  • 795
  • 8
  • 20
  • I did not know that fetch would copy *all* remote branches, since a push command does not push all branches. This is a good method, because it ensures that the other repo's contents do indeed get copied over. I still need to check for stashes, however. – blakedude Aug 30 '13 at 00:17
  • The "EXAMPLES" section in of my `git fetch` documentation states that: "`$ git fetch origin` command copies _all_ branches from the remote refs/heads/ namespace and stores them to the local refs/remotes/origin/ namespace, unless the branch..fetch option is used to specify a non-default refspec. This also corresponds with my experience and a little test I performed before posting." As to the stashes AFAIK is does _not_ copy them. But since stashes are actually commits too I usually just set branch pointers to the ones I need beforehand. – Levi Haskell Aug 30 '13 at 17:51
  • @user2730226, edited the answser to add a link about copying stashes – Levi Haskell Aug 30 '13 at 19:21