0

On committing my master branch to my Git repository, I need to update that latest code to multiple other repositories.
How can I do this?

git commit -u origin master 

eg: if I have repository 1,2 and 3.
If I push my code to master branch only in repository 1, automatically code should be updated in master branch of repository2 and 3.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Ansplz
  • 203
  • 1
  • 4
  • 9

3 Answers3

1

Don't forget you can push to multiple repos in one step

git remote add all git://original/repo.git
git remote set-url --add --push all git://another/repo.git
git remote set-url --add --push all git://original/repo.git

git push all master

A pure post-commit hook push solution would be "How to automatically push after committing in git?".
For instance, the one from i4h:

#!/usr/bin/env bash

branch_name=`git symbolic-ref --short HEAD` 
retcode=$?
non_push_suffix="_local"

# Only push if branch_name was found (my be empty if in detached head state)
if [ $retcode = 0 ] ; then
    #Only push if branch_name does not end with the non-push suffix
    if [[ $branch_name != *$non_push_suffix ]] ; then
        echo
        echo "**** Pushing current branch $branch_name to origin [i4h_mobiles post-commit hook]"
        echo
        git push origin $branch_name;
    fi
fi

It will push any branch except the one named xxx_local (in order to avoid pushing those branches when multiple intermediate commits are done before you are ready for said commits to be pushed).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you..I think am looking for git hooks..Can you suggest a good reference for this? – Ansplz Dec 13 '17 at 07:26
  • @Dev The point is: you don't need Git hooks. But if you do, then a good reference is https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks and https://stackoverflow.com/q/7925850/6309. I have edited the answer accordingly – VonC Dec 13 '17 at 08:00
  • Could you please share the a sample hook script for pushing code to another repo in post-commit...Thanks – Ansplz Dec 13 '17 at 09:46
  • i have tried adding this, ==> !/bin/sh git push production master; to pre-receive file but not working.. – Ansplz Dec 13 '17 at 12:08
  • @Dev No this is a post-commit client side hook, not a pre-receive server side hook – VonC Dec 13 '17 at 12:36
0

Follow the steps correctly:

If you want to push code in one repository:

Step1: git add -A (this will add all files to git)

Step2: git commit -m "First commit"

Step3: git remote add origin (first repo url)

Step4: git push -u origin master

If you want to push code to multiple repositories

Follow above two Steps

Then

Step 3: git remote add alt (second remote url)

Step 4: git push alt master

Mohamed Sameer
  • 2,998
  • 3
  • 22
  • 51
  • Thanks, actually the code should be automatically pushed to other repos, if i push to my master branch only...is your suggestion suits my scenario?Thanks – Ansplz Dec 13 '17 at 05:26
  • Thank you..I think am looking for git hooks..Can you suggest a good reference for this? – Ansplz Dec 13 '17 at 07:26
0

You may using git hook to do so.

First, locate your hook folder:

git config --list
#...
#core.hookspath=~/shared-git-hooks

Or set it using the following command:

git config core.hookspath ~/shared-git-hooks

Create a hook file contains scripts

touch ~/shared-git-hooks/post-commit

#post-commit file
git push ...

After set it, once you commit will run the script inside post-commit.

You may see more details for git hook here: https://git-scm.com/book/gr/v2/Customizing-Git-Git-Hooks

Hope it can help.


The script may look like:

#This script will push master to all existing remote
#git remote add repo2 https://github.com/repo2.git
#git remote add repo3 https://github.com/repo3.git
for remote in $(git remote); do git push $remote master; done;

Also please note that if you want to trigger it when push then need to use pre-push hook instead of post-commit

One more thing, if you want only doing this stuff on specific project / repository, then you may need to check the project location first.

if [ "$(pwd)" == "/your/project/path" ]; then
    for remote in $(git remote); do git push $remote master; done;
fi
Ming Chu
  • 1,315
  • 15
  • 13