Created
May 11, 2020 16:04
-
-
Save goldcaddy77/8ede7c8ba40964705cea0d74d62c58e5 to your computer and use it in GitHub Desktop.
gups - Git update from remote and rebase in one command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sync your local base branch with remote (Github) and | |
# rebase from base, all in one command | |
# Can be run in both a clean state or with unstaged changes | |
function gups() { | |
current_branch=$(current_branch) | |
local base_br=$1 | |
# Allow specifying which base branch we want to update from | |
# this is almost always develop (or master in repos that don't use develop) | |
if [ "$base_br" = "" ]; then | |
base_br=$(base_branch) | |
fi | |
# Save off your current change set before pulling down remote code | |
if [ "$current_branch" != $base_br ]; then | |
wip | |
fi | |
# Sync your local copies of the base branch with remote | |
git_update $base_br | |
if [ "$current_branch" != $base_br ]; then | |
echo | |
# Now switch back to your branch | |
git checkout $current_branch | |
echo | |
# Rebase (and integrate any conflicts) | |
git_rebase $base_br | |
echo | |
# Finally "uncommit" the wip commit so that git has unstaged files | |
unwip | |
fi | |
} | |
# Returns "upstream" if there is an upstream remote, otherwise "origin" | |
function base_remote() { | |
local base_rm="" | |
upstream_count=$(git remote | grep "upstream" | wc -l | xargs) | |
if [ "$upstream_count" -eq "0" ]; then | |
base_rm="origin" | |
else | |
base_rm="upstream" | |
fi | |
echo $base_rm | |
} | |
# Returns the current branch you're working in | |
function current_branch() { | |
ref=$(git symbolic-ref HEAD 2> /dev/null) || \ | |
ref=$(git rev-parse --short HEAD 2> /dev/null) || return | |
echo ${ref#refs/heads/} | |
} | |
# Returns "develop" if there is a develop branch, otherwise "master" | |
function base_branch() { | |
local base_br="" | |
git rev-parse --verify --quiet develop &> /dev/null | |
if [ $? = 0 ]; then | |
base_br="develop" | |
else | |
base_br="master" | |
fi | |
echo $base_br | |
} | |
# Update your local base branch from remote (Github) | |
function git_update() { | |
local current_branch=$(current_branch) | |
local base_rm=$(base_remote) | |
local base_br=$1 | |
if [ "$base_br" = "" ]; then | |
base_br=$(base_branch) | |
fi | |
# Dont bother checking out if you're already on base | |
if [ "$current_branch" != "$base_br" ]; then | |
git checkout $base_br | |
fi | |
# update and prune remotes - like a super fetch | |
git remote update -p | |
# See https://stackoverflow.com/a/6406947 | |
git merge --ff-only $base_rm/$base_br | |
# Only sync up our fork if there is an upstream | |
if [ "$base_rm" == "upstream" ]; then | |
git push origin $base_br | |
fi | |
} | |
function git_rebase() { | |
local base_br=$1 | |
if [ "$base_br" = "" ]; then | |
base_br=$(base_branch) | |
fi | |
wip | |
git rebase $base_br | |
unwip | |
} | |
function wip() { | |
if ! git_is_dirty; then | |
return | |
fi | |
print_success "Your working directory is dirty, 'wip'ing the uncommitted files\n" | |
git add . | |
git commit -m "wip" --no-verify | |
} | |
function unwip() { | |
local last_msg=$(git --no-pager log -n 1 --pretty=format:%s) | |
# If our last commit is a 'wip', reset it | |
if [ $last_msg = "wip" ]; then | |
print_success "Found 'wip' commit, resetting working directory\n" | |
git reset HEAD^ | |
fi | |
} | |
# Note: can't call this git_dirty because that's used by the shell | |
function git_is_dirty() { | |
local num_dirty=$(git status --porcelain | wc -l | awk '{print $1}') | |
if [ "$num_dirty" -gt 0 ]; then | |
return 0; | |
fi | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment