If you work on long-lived feature branches, you've probably experienced this:
-
master(ormain) 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
While you're developing, your teammates merge several pull requests.
master
A──B──C──D──G──H──I
feature/login
\
E──F
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
and want to sync it with master.
First, fetch the latest changes:
git fetch origin
Switch to your feature branch:
git checkout feature/login
Reset your local branch to match the remote version:
git reset --hard origin/feature/login
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
Finally, push the updated branch:
git push
Your history now becomes:
master
A──B──C──D──G──H──I
\
feature/login M
\ /
E──────F
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
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'
This creates a new command called:
git sync-branch
How to Use It
Sync your branch with master:
git sync-branch feature/login
Or sync it with another branch:
git sync-branch feature/login develop
which is equivalent to:
git fetch
git checkout feature/login
git reset --hard origin/feature/login
git merge --no-ff origin/develop
What Does the Alias Do?
Let's break it down.
git fetch
Downloads the latest commits from the remote.
git checkout "$1"
Switches to the target branch.
git diff --quiet
git diff --cached --quiet
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
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}
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
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)