I have a git repository with many people contributing to it. So every user having their own repository checked out locally in their workspace. I would like to maintain one more local repo as a centralized place to look for the latest code. So I would like to have an exact replica of the remote repository. By which, I mean my centralized repo should be in sync with the remote repository all the time (24/7). So I am looking for something like if someone pushes a new commit to the remote repo, my centralized repo should checkout(git pull) latest code to have the updated code all the time. Please tell me how could I achieve this
Asked
Active
Viewed 260 times
0
-
You have a number of options. All require work on your part; all have flaws. There's a fundamental problem and you must decide what to do about it: what happens if the network is partitioned, or some machine goes down, so that your local-but-centralized repo cannot talk with the remote repo for, say, one hour, or one day, or one week, or one year? Decide that; the rest follows from there. – torek Dec 06 '17 at 15:56
-
Assuming the network is intact all the time, can we achieve this? – newbie Dec 07 '17 at 09:51
-
Sure: write software that, upon receiving an update at your master remote, signals to the slave-local that the slave-local should update itself. That can be as simple as running `git push` from the master to the slave-local (this assumes the slave-local repository is bare or otherwise allows pushes from the master). The software you write can be as simple as a one-line post-receive hook that runs `git push --mirror
`. – torek Dec 07 '17 at 15:46
1 Answers
0
For the local git repo (even you treat it as a centralized place), it can not sync with remote git repo automatically.
There are two ways to sync local repo if there has new changes in remote repo:
Option 1: schedule to run a script to execute git pull for time to time
The script can be:
#!/bin/sh
git fetch -p
for bran in $(git branch -r)
do
{
if [[ $bran == *"origin/HEAD -> origin/"* ]]; then
echo "this is not a branch"
else
{
localb=$(echo $bran | cut -d'/' -f 2)
echo "This is $localb branch"
git checkout $localb
git pull origin $localb
}
fi
}
done
Schedule to run the script, you can refer:
schedule running a bash shell script in windows
Option 2: use post-receive hook to update the local repo
Use post-receive hook so that when changes are pushed to remote repo, then execute the script automatically.

Community
- 1
- 1

Marina Liu
- 36,876
- 5
- 61
- 74
-
I could write a cron job as you suggested. But I don't want to run the script unnecessarily even when there is no commit (which is of no use). I am looking for something like interrupt based mechanism so that only when there is a new commit/change merged to remote, then we will run this script (something like git hooks). But I couldn't find any such. – newbie Dec 07 '17 at 09:47
-
It works in shell script perfectly, please make sure the directory is a git repo where the scripts executed. And unless the remote repo server-side hooks are available, you can use post-receive hook update the local repo after changes pushed to remote repo. – Marina Liu Dec 07 '17 at 09:55
-
So you mean to say remote repo should be having some hooks to notify the local-centralized repo about a new merge/commit? – newbie Dec 07 '17 at 09:59
-
Yes, but if your remote repo hosted in third part, such as github, the server-side hooks are not available. – Marina Liu Dec 07 '17 at 10:02
-
Yes that's the problem. So, What if i try to have my remote repo hosted in some machine which i have access, then can we do this? Any such example hooks available? (I am trying to reduce the load on my machine by avoiding running the script every 5 minutes) – newbie Dec 07 '17 at 13:18
-
If the server-side hooks can be available for your, it's not necessary to schedule to run hook (such as every 5 mins), because (such as post-receive) hook will be automatically triggered after changes pushed to remote repo. – Marina Liu Dec 08 '17 at 05:44
-
Have you sync local repo with remote repo by local scripts or post-receive hook successful now? – Marina Liu Dec 12 '17 at 09:07
-
I didn't check that yet. I need to check the remote repo(access permissions) and post-receive hook for implementation. – newbie Dec 13 '17 at 10:38
-
Ok, feel free to let me know if you have problem setting the post-receive hook. – Marina Liu Dec 14 '17 at 07:36
-