At the end of our release cycle for the company I currently work for, our master branch gets locked down for 2-3 weeks for final testing, signoffs, and any last minute patches that needed to be committed. Meanwhile, our engineers are working on the next release, so we create a new feature branch for the next version that we target pull requests at and merge to. “Someone” gets tasked with creating this feature branch and keeping it up-to-date with the latest master for easy merging later. Our company uses rebase instead of a merge to keep merge commits from cluttering up the commit log. I don’t care either way (rebase vs merge), but that was the decision way back and that’s what we do.

So, this year I was the “someone” tasked with being the creator and maintainer of these feature branches every release, updating them daily, and it got real old, real quick copying line by line from my notes to fetch upstream, rebase the feature branch, rebase in the latest master, then force pushing the feature branch back upstream… SO what do good (lazy “efficient”) devs do? Script it.

Below are two ways you can add these commands to your git aliases if you’re unfamiliar with that. The two commands basically do the same thing, just giving you options depending on what branch you currently have checked out.

CAUTION!!

These two aliases will FORCE PUSH any branch changes upstream!


This can wipe out changes on an existing upstream branch.


Know what you are doing before running these commands!

Rebase Upstream Feature Branch – RUFB

rufb – rebases a different branch than the one you’re currently on

If you currently have master checked out, and you want to update a different feature branch:

  • git:(master)$ git rufb myFeatureBranch

What it does:

  • Saves the current branch name that you are on
  • Checks out myFeatureBranch – git checkout $1
  • Fetches upstream – git fetch upstream
  • Rebases upstream/myFeatureBranch – git rebase upstream/$1
  • Rebases upstream/master – git rebase upstream/master
  • Force push upstream myFeatureBranch – git push -f upstream $1

 

ruf – rebases the Feature Branch that you are currently on

If you currently have myFeatureBranch already checked out, you can use the following to update and push:

  • git:(myFeatureBranch)$ git ruf

What it does:

  • Saves your current branch name as $cb
  • Fetches upstream – git fetch upstream
  • Rebases upstream/myFeatureBranch – git rebase upstream/$cb
  • Rebases upstream/master – git rebase upstream/master
  • Force push upstream myFeatureBranch – git push -f upstream $cb

 

Adding Aliases to .gitconfig

You can add these two commands one of two ways. Please edit your version of these to fit your branch names and purposes. For example, if you or your org use origin as the main remote, then change upstream to origin.

 

1) Via editing .gitconfig file manually:

You can edit your .gitconfig file and add the following two commands under the [alias] block in the file.

[alias]	
	rufb = "!f() { cb=$(git rev-parse --abbrev-ref HEAD) && echo 'On Original Branch: '$cb && echo '== Rebasing upstream feature branch: '$1 && git checkout $1 && echo '-- Fetching upstream' && git fetch upstream && echo '-- Rebasing upstream '$1 && git rebase upstream/$1 && echo '-- Rebasing upstream master' && git rebase upstream/master && echo '-- Pushing -f upstream '$1 && git push -f upstream $1 && git checkout $cb; }; f"
	ruf = !cb=$(git rev-parse --abbrev-ref HEAD) && echo '== Rebasing upstream feature branch: '$cb && echo '-- Fetching upstream' && git fetch upstream && echo '-- Rebasing upstream '$cb && git rebase upstream/$cb && echo '-- Rebasing upstream master' && git rebase upstream/master && echo '-- Pushing -f upstream '$cb && git push -f upstream $cb

 

2) OR Via terminal commands:

Or, you can go to your terminal and paste the following and it’ll update your alias block for you.

$ git config --global alias.rufb '!f() { cb=$(git rev-parse --abbrev-ref HEAD) && echo "On Original Branch: "$cb && echo "== Rebasing upstream feature branch: "$1 && git checkout $1 && echo "-- Fetching upstream" && git fetch upstream && echo "-- Rebasing upstream "$1 && git rebase upstream/$1 && echo "-- Rebasing upstream master" && git rebase upstream/master && echo "-- Pushing -f upstream "$1 && git push -f upstream $1 && git checkout $cb; }; f'

$ git config --global alias.ruf '!cb=$(git rev-parse --abbrev-ref HEAD) && echo "== Rebasing upstream feature branch: "$cb && echo "-- Fetching upstream" && git fetch upstream && echo "-- Rebasing upstream "$cb && git rebase upstream/$cb && echo "-- Rebasing upstream master" && git rebase upstream/master && echo "-- Pushing -f upstream "$cb && git push -f upstream $cb'

Once I created these aliases, it saves me 5’s of minutes of invaluable time each day. Hopefully these help you and your organization out as well!

Categories: git

0 Comments

Leave a Reply

Avatar placeholder