Hemanth.HM

A Computer Polyglot, CLI + WEB ♥'r.

Auto Syncing a Forked Git Repository With the Parent

| Comments

Syncing my forked repositories is the most common task I do! So wanted to automate it.

The bash script below helps me with syncing my forked code from github, without me worrying about the parent git url!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env bash
# Author : Hemanth.HM 
# License : MIT
# Some JSON parsing logic from http://l-o-n-g.livejournal.com/146422.html

# Get the current repo URL
url=$(git config --get remote.origin.url)

# Get the user and repo part from the URL 
# Must be something like part=${url#*//*/}
# Sticking to github specific code as of now.

part=${url#*github.com*/}

# ZOMG parse JSON using the github api get the parent git_url.
# would suggest to use curl -s .. | jq -r '.parent.git_url'  
upstream=$(curl -s https://api.github.com/repos/${part%.git} | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | grep '"git_url":' | sed 's/:/ /1' | awk -F" " '{ print $2 }' | uniq | tail -1)

# Rest of it...
git remote add upstream $upstream
git checkout master
git pull --rebase
git push origin master

Usage :

1
2
curl https://raw.github.com/hemanth/futhark/6cdbe8f14b9f1b12498a22473d398ee197327051/syncfork.bash > /usr/bin/sf
chmod a+x /usr/bin/sf

In any github git repo you want to sync, just run sf to sync your fork with the parent.

Hope this helps you, happy hacking! :)

Comments