Skip to content

Instantly share code, notes, and snippets.

@tomchapin
Last active January 31, 2020 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomchapin/e860bcc663e262d1abeadd62ec1bd11e to your computer and use it in GitHub Desktop.
Save tomchapin/e860bcc663e262d1abeadd62ec1bd11e to your computer and use it in GitHub Desktop.
#!/bin/bash
# --------------------------------------------------------------------------
# Reverse Merge and Push (by Tom Chapin - https://gist.github.com/tomchapin/e860bcc663e262d1abeadd62ec1bd11e)
# --------------------------------------------------------------------------
# This command is basically the opposite of "git merge". It uses git-up to
# fetch and pull all of the branches you are currently tracking, switches
# to your destination branch, merges in your original source branch, pushes
# the changes, and then switches back to the original source branch.
# If any step fails (for example, due to a merge conflict), the whole
# process will halt.
# --------------------------------------------------------------------------
# Requirements: This script requires you to have git-up gem installed
# --------------------------------------------------------------------------
# Installation:
# Copy this script as a file named "git-m2p" somewhere in
# your path (For example: /usr/local/bin/git-m2p), and then
# run "chmod +x git-m2p" to give it execution permissions.
# --------------------------------------------------------------------------
# Usage:
# Assuming you are on a branch named "foo", run "git m2p bar"
# to merge the foo branch into the bar branch.
# --------------------------------------------------------------------------
set -euo pipefail
((!$#)) && echo No branch name given, command ignored! && exit 1
source_branch=$(git rev-parse --abbrev-ref HEAD)
merge_to_branch=$1
echo "Merging ${source_branch} to ${merge_to_branch}, pushing ${merge_to_branch}, and then returning to the ${source_branch} git branch..."
echo "Fetching changes for all tracked branches (using git up)..." && git up && git checkout $merge_to_branch && echo "Merging ${source_branch} to ${merge_to_branch}..." && git merge --no-edit $source_branch && echo "Pushing ${merge_to_branch}..." && git push && git checkout $source_branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment