Skip to content

Instantly share code, notes, and snippets.

@daniel-bytes
Created July 16, 2022 11:30
Show Gist options
  • Save daniel-bytes/b2d9db6d8711a21c8a83f95046a1b9d7 to your computer and use it in GitHub Desktop.
Save daniel-bytes/b2d9db6d8711a21c8a83f95046a1b9d7 to your computer and use it in GitHub Desktop.
Git Helpers
function git-reset-commits(){
branch=$(git branch | grep \* | cut -d ' ' -f2)
remote=upstream
basebranch=$branch
if [ "$#" = 1 ]; then
basebranch=$1
elif [ "$#" -gt 1 ]; then
remote=$1
basebranch=$2
fi
git fetch $remote $basebranch
commit=$(git merge-base $remote/$basebranch $branch)
echo "resetting current branch $branch to base branch $remote/$basebranch via commit $commit"
git reset $commit
}
function git-rebase(){
remote=upstream
branch=$(git branch | grep \* | cut -d ' ' -f2)
if [ "$#" = 1 ]; then
branch=$1
elif [ "$#" -gt 1 ]; then
remote=$1
branch=$2
fi
echo "rebasing branch $branch from remote $remote"
git fetch $remote $branch
git rebase $remote/$branch
}
function git-since-master(){
local default_branch="main"
if [ -f "$(git rev-parse --show-toplevel)/.git/refs/heads/master" ]
then
default_branch="master"
fi
local branch="$1"
if [ "$1" = "" ]
then
branch=`git branch --show-current`
fi
git diff `git merge-base --fork-point ${default_branch} ${branch}` $branch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment