Last Updated: February 25, 2016
·
5.202K
· juliomistral

I Don't Need No Stinkin' Git Merge Bubbles

So you just finished a new feature, committed it, and are now ready to push it out of the nest and up to origin. But then you get this:

To git@github.com:juliomistral/TestMainRepo.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:juliomistral/TestMainRepo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Big sad.

So you then follow the instructions and you get this little gem:

Merge branch 'master' of github.com:juliomistral/TestMainRepo

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

Which results in this history:

*   8f7e922 (HEAD, master) Merge branch 'master' of github.com:juliomistral/TestMainRepo - Julio A. Mistral, 4 seconds ago
|\  
| * 1dc0a3d (origin/master, origin/HEAD) Another commit that happened and was pushed up - Julio A. Mistral, 7 minutes ago
* | 6bf4e46 New feature to be pushed up - Julio A. Mistral, 8 minutes ago
|/  
* 1413896 Test checkin on local branch tracking master - julio@yola.com, 1 year, 6 months ago
* 710c1d3 Initial commit - julio@yola.com, 1 year, 6 months ago

Bigger sad (unless you like multi-color ASCII art, in which case go buck wild).

This just irks me: you end up committing a merge that has zero relevancy. There was no conflict to resolve and you weren't even merging another branch into master. You just wanted to push your change up.

So this is what you do:

# Preserve any local changes you have 
# that will get blown away with next step
git stash save

# Reset to last commit before merge bubble commit
git reset --hard HEAD~1

# Pull with rebase
git pull --rebase

# Push your change up, guilt free
git push origin HEAD

And now you have a clean, bubble free history

* 8f34b08 (HEAD, origin/master, origin/HEAD, master) New feature to be pushed up - Julio A. Mistral, 10 minutes ago
* 1dc0a3d Another commit that happened and was pushed up - Julio A. Mistral, 9 minutes ago
* 1413896 Test checkin on local branch tracking master - julio@yola.com, 1 year, 6 months ago
* 710c1d3 Initial commit - julio@yola.com, 1 year, 6 months ago

4 Responses
Add your response

Nice, thanks for sharing.

over 1 year ago ·

Thanks, but how do you stop Vim from coming up in the first place?

over 1 year ago ·

Steps are missing a final 'git stash apply' to restore the saved working directory changes.

wardsandler: Prevent them by using git pull --rebase in the first place, either explicitly or by setting it as a default preference.

over 1 year ago ·

Add this to your git config to auto stash your work when rebase.

[rebase]
autostash = true
over 1 year ago ·