Hemanth's Scribes

opensource

Auto Syncing a Forked Git Repository

Author Photo

Hemanth HM

Thumbnail

Syncing forked repositories is a common task. This script automates it.

#!/usr/bin/env bash
# Author: Hemanth.HM

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

# Get the user and repo part
part=${url#*github.com*/}

# Parse JSON using GitHub API to get 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)

# Sync with upstream
git remote add upstream $upstream
git checkout master
git pull --rebase
git push origin master

## Usage
bash
curl https://raw.github.com/hemanth/futhark/.../syncfork.bash > /usr/bin/sf
chmod a+x /usr/bin/sf

Then just run sf in any forked repo to sync with parent!

#git#bash#cli
Author Photo

About Hemanth HM

Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.