Effortless Upstream Syncing: A Custom Alias for Git Users

My Problem

One of the challenges I've encountered while contributing to Codu and other projects has been keeping my fork's default branch in sync with their upstream branches.

Until a few days ago, I was navigating to the upstream repo on GitHub and clicking "Sync fork."

Sync Fork

This process was frustrating for several reasons, primarily because I dislike manual steps. As this is open source, I want to be as productive as possible during the limited time I can contribute.

My Solution

I already use Oh My Zsh and its built-in Git aliases. If you’re not already using it, I highly recommend checking it out here.

I knew Oh My Zsh supports aliases out of the box, but can I add my own custom ones? Thankfully, yes!

This is quite simple. First, make sure you have Oh My Zsh installed. Then, open your terminal and follow these steps:

  1. Register Your Upstream Dependency:

    git remote add upstream https://github.com/codu-code/codu.git
    

    This command is for the Codu repository but will work for any repository.

  2. Open Your Zsh Config File:

    nano ~/.zshrc
    

    This command opens your Zsh config file in the Nano editor within your terminal. Navigate to the following section of the config:

    plugins=(git)
    
    # Add your custom aliases
    
  3. Add Your Custom Alias: Under the custom aliases comment, add:

    alias gsync='git fetch upstream && \
                 git checkout develop && \
                 git rebase upstream/develop && \
                 git push origin develop'
    

Summary of Steps:

  • Fetch updates from the upstream repository.
  • Checkout the local develop branch.
  • Rebase your local develop branch with the upstream develop.
  • Automatically push the updated local develop branch to your remote repository.
  1. Reload Your Config File: After adding the alias, ensure to reload your config file:
    source ~/.zshrc
    

Now, you’re ready to use your new gsync command!

New Command

I hope this helps someone save time. If anyone has a more elegant solution, let me know, and I’ll update the article!

ProductivityGitHacktoberfest
Avatar for John Allen

Written by John Allen

Electronic engineer turned software developer. Tech lead and Frenchie dad. Very interested in oAuth and Graph database engines.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.