Skip to content

Instantly share code, notes, and snippets.

@todb-r7
Created November 26, 2014 17:26
Show Gist options
  • Save todb-r7/3fbee1a9e7b36d82ca55 to your computer and use it in GitHub Desktop.
Save todb-r7/3fbee1a9e7b36d82ca55 to your computer and use it in GitHub Desktop.
Safely publish local changes to upstream master
[alias]
# A pretty and short commit log which notes signed commits
nicelog = log --pretty=format:'%Cred%h%Creset -%Creset %s %Cgreen(%cr) %C(bold blue)<%aE>%Creset [%G?]'
# Get the current tracking branch, eg, upstream/master
tracking = !"git branch -vv | grep \\* | sed 's#.*\\[\\(.*\\)[]].*#\\1#' | cut -f 1 -d :"
# Get the current tracking branch remote, eg, upstream
tracking-remote = !"git tracking | cut -f 1 -d /"
# Fetch and rebase from the current tracking remote, preserving and re-signing local merges.
fetch-preserve-merges = !"git fetch $(git tracking-remote) && \
git rebase --preserve-merges && \
git nicelog -5 && \
git commit --amend -S; #"
# Publish changes in local upstream-master to upstream/master,
# preserving local merges and resigning them along the way.
publish = !"git fetch-preserve-merges && git push upstream upstream-master:master"
@todb-r7
Copy link
Author

todb-r7 commented Nov 26, 2014

The problem

I'm merging a pull request, working on, it, testing it, etc. Once I'm done, I want to push it, but sometimes, someone else beats me to pushing to upstream/master. If I git pull -r, I'll rebase and wipe out my local merge commit, and that's sad.

The solution

This set of aliases will get the current state of upstream/master, rebase the local branch with the upstream changes, preserve my merge commit, and resign it. Finally, it'll push to upstream (without forcing).

I like having the last 5 commits displayed at me, too, just so I can see what's happened recently.

This is largely based on the advice by Glen Madden. Thanks!

Example usage:

This was a case where I landed #4264 while joev-r7 was landing #4265. Seems to have worked like a charm.

todb@mazikeen:~/git/rapid7/metasploit-framework$ git publish
Successfully rebased and updated refs/heads/upstream-master.
2b042c9 - Land #4264, reload_all should complain (66 seconds ago) <todb-r7@github> [G]
cc33566 - Land #4265, @shuckins-r7 fix for RPORT error on UDP sweep. (56 minutes ago) <joev-r7@github> [G]
2aeedb1 - Merge pull request #1 from jhart-r7/landing-4265-jhart (2 hours ago) <shuckins-r7@github> [N]
79b2b5e - RPORT is required by UDPScanner; deregister instead (2 hours ago) <jon_hart@rapid7.com> [N]
3a99f7f - Land #4267, CheckCode fix for vmware_mount (10 hours ago) <wvu-r7@github> [G]

You need a passphrase to unlock the secret key for
user: "Tod Beardsley <tod_beardsley@rapid7.com>"
4096-bit RSA key, ID EA19CAAC, created 2014-11-06

[upstream-master c0be32e] Land #4264, reload_all should complain
Counting objects: 59, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 2.36 KiB | 0 bytes/s, done.
Total 10 (delta 8), reused 1 (delta 0)
To github-r7:rapid7/metasploit-framework
   cc33566..c0be32e  upstream-master -> master
[ruby-2.1.5@metasploit-framework](upstream-master) 
todb@mazikeen:~/git/rapid7/metasploit-framework$ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment