---
title: "Auto Syncing a Forked Git Repository"
description: "Bash script to automatically sync forked repos with parent. Practical notes and code examples on Auto Syncing a Forked Git."
url: "https://h3manth.com/scribe/auto-syncing-a-forked-git-repository/"
canonical_url: "https://h3manth.com/scribe/auto-syncing-a-forked-git-repository/"
markdown_url: "https://h3manth.com/scribe/blog/auto-syncing-a-forked-git-repository.md"
publishDate: "2013-07-31T00:00:00.000Z"
author: "Hemanth HM"
category: "opensource"
tags: ["git","bash","cli"]
word_count: 131
reading_time_minutes: 1
x_markdown_tokens: 302
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Auto Syncing a Forked Git Repository

> Bash script to automatically sync forked repos with parent. Practical notes and code examples on Auto Syncing a Forked Git.

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

```bash
#!/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!
