mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
382 words
2 minutes
Git in Practice: Three Methods to Keep Your Fork Repo Synced with Upstream
2023-12-06

“We loved with a love that was more than love.”

Method 1: GitHub Web One-Click Sync (Easiest)#

Since 2021, GitHub has natively supported a web-based sync feature, suitable for the vast majority of regular users.

  1. Open your Fork repository page.
  2. Click the Fetch upstream dropdown menu above the code list.
  3. Click the Fetch and merge button.

GitHub Sync Button

Pros: No commands needed, extremely simple operation. Cons: Requires manual clicking, and weak conflict resolution capabilities.


Method 2: Git Command Line Sync (Most Professional)#

If you are a developer, it is recommended to master the command line (CLI) method, as it gives you complete control over the merge process, making it easier to handle code conflicts.

1. Configure the Upstream Repository#

First, we need to tell local git where the “upstream” is.

# View current remote repositories
git remote -v
# Output usually only shows origin (your repo)
# Add upstream repository (replace the URL with the original author's repo address)
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git
# Check again, you should see upstream
git remote -v

2. Fetch and Merge#

# 1. Switch to the main branch (main or master)
git checkout main
# 2. Fetch the latest code from upstream to the local cache
git fetch upstream
# 3. Merge the upstream code into your local branch
git merge upstream/main
# 4. Push to your GitHub repository
git push origin main

Pros: Complete control, can handle complex conflicts. Cons: More steps, requires basic Git knowledge.


Method 3: GitHub Actions Automated Sync (Most Worry-Free)#

If you forked a repo to act as a mirror, or simply want to keep it updated without bothering to manually click, you can use GitHub Actions to set up a scheduled task.

1. Create a Workflow File#

In your repository, click Actions -> New workflow, and create a file named .github/workflows/sync.yml.

If the repo you forked already has other workflows, after clicking Actions, you might need to click New workflow first, as shown below: New Workflow

2. Fill in Configuration Code#

Copy the following YAML content, and be sure to modify the upstream repository address in step two.

name: Upstream Sync
permissions:
contents: write
on:
schedule:
- cron: "0 0 * * *" # Runs daily at 0:00 UTC (8:00 Beijing Time)
workflow_dispatch: # Allows manual triggering via button
jobs:
sync_latest_from_upstream:
name: Sync latest commits from upstream repo
runs-on: ubuntu-latest
if: ${{ github.event.repository.fork }}
steps:
# Step 1: Checkout code
- name: Checkout target repo
uses: actions/checkout@v3
# Step 2: Execute sync action
- name: Sync upstream changes
id: sync
uses: aormsby/Fork-Sync-With-Upstream-action@v3.4
with:
# ⚠️⚠️ Please modify these two lines! Fill in the original author's repo address and branch ⚠️⚠️
upstream_sync_repo: original-author/original-repo
upstream_sync_branch: main
# Your branch name
target_sync_branch: main
target_repo_token: ${{ secrets.GITHUB_TOKEN }}
test_mode: false
# Step 3: Check for failure (usually because upstream changed the workflow file; for security, GitHub pauses automatic updates)
- name: Sync check
if: failure()
run: |
echo "[Error] Auto sync failed. It is possible the upstream repository updated its .github/workflows file."
echo "Please manually go to the GitHub interface and click Fetch Upstream once to restore automation."
exit 1

Please refer to the following image for modification instructions: Modification Instructions

Here is a full code example image after configuration (Note: some secrets do not need manual entry): Full Code

3. Precautions#

  • First Run: After creating the file, it is recommended to switch to the Actions page and manually trigger it once (Run workflow) to verify the configuration is correct.
  • Protection Mechanism: If the upstream repository modifies workflow files, GitHub will pause your automated task for security reasons. At this time, you need to manually Sync once to reactivate it.

Summary#

  • Occasional sync: Directly use Method 1 (click on web).
  • Developing code: Must use Method 2 (command line), because you need to handle Merge conflicts.
  • Pure backup: Use Method 3 (Actions). Once set up, you can be completely hands-off.

Disclaimer#

  • The automated script provided in this article is only for compliant open-source project synchronization.
  • Do not use it for malicious cloning or actions violating GitHub's Terms of Service.
  • Automated sync might overwrite your custom modifications to the repository, please use with caution.
Share

If this article helped you, please share it with others!

Git in Practice: Three Methods to Keep Your Fork Repo Synced with Upstream
https://blog.levifree.com/posts/git-sync-fork-upstream-guide/
Author
LeviFREE
Published at
2023-12-06
License
CC BY-NC-SA 4.0

Some information may be outdated

Table of Contents