2

Is it possible in Git to do the following, easily?

  • Have a 'main' repo located at say, GitHub
  • Keep another repository at Bitbucket, and another one on a production server (web host)

When local development occurs, I want the push to push changes into GitHub, and push those same changes to the other repositories simultaneously.

I tried doing this with --mirror, but with a team of 7 working on the same project, it seems prone to removing feature branches when the mirror is updated.

Thoughts? Is there any way to do this without manually editing the .gitconfig file to manually define an origin that points to multiple repo URLs? Could this be done with a hook?

Kevin
  • 13,153
  • 11
  • 60
  • 87
  • I'd recommend to use a server hook to _forward_ the push to another remote. I don't know exactly how would I achieve this, but that would be my strategy. – mgarciaisaia Sep 02 '13 at 19:11
  • If you didn't know this already, `git push --mirror` deletes references/branches in the remote repo if they don't exist in the local repo, that's why you're seeing features branches get deleted...that's why it's a "mirror", the ***exact state*** of the local repo is copied over to the remote repo! –  Sep 02 '13 at 19:41

2 Answers2

4

You can simply define an alias to do this:

git config alias.pusheverywhere \
"!git push github && git push bitbucket && git push production"

The && in the command is logical AND, so the command on the right side of it will only execute if the command on the left side succeeds without errors.

3

Mirroring a got repo can be tricky, especially if you don't have created your downstream repo with git clone --mirror.
And in your case, you wouldn't create your local ("downstream") repo that way, since you have three upstream mirror repos you want to maintain.

The blog article "How to properly mirror a git repository" mention that, for a local repo to mirror push, you need to:

git fetch --prune upstream_repo

(to be sure to clean remote tracking branches that are tracking deleted branches of the upstream repo)

git push --prune upstream_repo +refs/remotes/origin/*:refs/heads/* +refs/tags/*:refs/tags/*

(and that doesn't include git notes, by the way)

Pushing to three repos is one way, but I would prefer:

  • pushing the to prod repo
  • having a post receive hook which pushed to GitHub and BitBucket repos

If a problem is detected at the prod repo (like a missing comment on a commit, or some other policy not respected), you get a chance to:

  • deny the push on prod
  • not pollute the GitHub and BitBucket repos
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250