DEV Community

Cover image for Stop Rebasing Every Time: A Safer Way to Keep Your Git Branch Updated with `master`
Mohammad Rajaei Monfared
Mohammad Rajaei Monfared Subscriber

Posted on

Stop Rebasing Every Time: A Safer Way to Keep Your Git Branch Updated with `master`

If you work on long-lived feature branches, you've probably experienced this:

  • master (or main) keeps moving.
  • Your branch falls behind.
  • Pull requests become harder to review.
  • Merge conflicts get bigger every day.

Many teams solve this by rebasing their feature branches.

Others—including many enterprise teams—prefer merging the latest master into the feature branch to preserve commit history and avoid rewriting commits that may already be shared.

If your workflow uses merge instead of rebase, this article shows how to make the process much faster with a custom Git alias.


The Problem

Imagine your repository looks like this.

master
A──B──C──D

feature/login
     \
      E──F
Enter fullscreen mode Exit fullscreen mode

While you're developing, your teammates merge several pull requests.

master
A──B──C──D──G──H──I

feature/login
     \
      E──F
Enter fullscreen mode Exit fullscreen mode

Now your feature branch is missing the latest changes.

If you don't sync it:

  • merge conflicts accumulate
  • CI may fail unexpectedly
  • testing becomes less reliable
  • your eventual pull request becomes much harder to review

Keeping your branch up-to-date regularly makes integration much smoother.


Updating Your Branch Manually

Suppose you're working on:

feature/login
Enter fullscreen mode Exit fullscreen mode

and want to sync it with master.

First, fetch the latest changes:

git fetch origin
Enter fullscreen mode Exit fullscreen mode

Switch to your feature branch:

git checkout feature/login
Enter fullscreen mode Exit fullscreen mode

Reset your local branch to match the remote version:

git reset --hard origin/feature/login
Enter fullscreen mode Exit fullscreen mode

Why reset?

This ensures your local branch exactly matches the remote branch before merging. It's useful if your local branch is only a working copy of the remote branch.

Warning: Any unpushed commits will be permanently deleted.

Merge the latest master:

git merge --no-ff origin/master
Enter fullscreen mode Exit fullscreen mode

Finally, push the updated branch:

git push
Enter fullscreen mode Exit fullscreen mode

Your history now becomes:

master
A──B──C──D──G──H──I
               \
feature/login   M
      \        /
       E──────F
Enter fullscreen mode Exit fullscreen mode

where M is the merge commit.


That's a Lot of Typing...

Every time you want to synchronize a branch, you're repeating the same commands:

git fetch
git checkout feature/login
git reset --hard origin/feature/login
git merge --no-ff origin/master
git push
Enter fullscreen mode Exit fullscreen mode

After doing this dozens of times each week, it becomes repetitive.

Fortunately, Git aliases can automate the workflow.


Creating a Git Alias

Run the following command once:

git config --global alias.sync-branch '!f() {
    git fetch &&
    git checkout "$1" &&
    git diff --quiet &&
    git diff --cached --quiet &&
    git reset --hard origin/$1 &&
    git merge --no-ff origin/${2:-master};
}; f'
Enter fullscreen mode Exit fullscreen mode

This creates a new command called:

git sync-branch
Enter fullscreen mode Exit fullscreen mode

How to Use It

Sync your branch with master:

git sync-branch feature/login
Enter fullscreen mode Exit fullscreen mode

Or sync it with another branch:

git sync-branch feature/login develop
Enter fullscreen mode Exit fullscreen mode

which is equivalent to:

git fetch
git checkout feature/login
git reset --hard origin/feature/login
git merge --no-ff origin/develop
Enter fullscreen mode Exit fullscreen mode

What Does the Alias Do?

Let's break it down.

git fetch
Enter fullscreen mode Exit fullscreen mode

Downloads the latest commits from the remote.


git checkout "$1"
Enter fullscreen mode Exit fullscreen mode

Switches to the target branch.


git diff --quiet
git diff --cached --quiet
Enter fullscreen mode Exit fullscreen mode

Ensures there are no uncommitted changes.

If your working tree isn't clean, the alias stops instead of accidentally deleting work.


git reset --hard origin/$1
Enter fullscreen mode Exit fullscreen mode

Makes your local branch identical to the remote branch.

This removes:

  • local commits
  • staged changes
  • unstaged changes

that haven't been pushed.


git merge --no-ff origin/${2:-master}
Enter fullscreen mode Exit fullscreen mode

Merges the latest master (or another specified branch) into your feature branch while always creating a merge commit.


When Should You Use This?

This workflow is ideal if your team:

  • prefers merge over rebase
  • wants to preserve merge history
  • frequently syncs long-running feature branches
  • already pushes work to the remote regularly

It is not recommended if you often keep local-only commits that haven't been pushed yet.


Important Warnings

⚠️ This alias uses:

git reset --hard
Enter fullscreen mode Exit fullscreen mode

If you have local commits or changes that are not pushed, they will be permanently deleted.

Before using it:

  • commit your work
  • push your commits
  • or stash your changes

Always understand what a Git alias does before adding it to your global configuration.


Final Thoughts

One of Git's most underused features is aliases.

Small automations like this can eliminate repetitive commands and reduce mistakes, especially when you perform the same workflow dozens of times a week.

If your team follows a merge-based workflow, a sync-branch alias can save time while keeping every feature branch aligned with the latest changes from master.

Top comments (0)