Skip to content

Instantly share code, notes, and snippets.

@telmotrooper
Last active August 30, 2024 14:51
Show Gist options
  • Save telmotrooper/2facfb5ab33fb501ce256f3ba3552f62 to your computer and use it in GitHub Desktop.
Save telmotrooper/2facfb5ab33fb501ce256f3ba3552f62 to your computer and use it in GitHub Desktop.
POSIX-compliant commands to automate common Git tasks
# Display commits from HEAD while counting them (useful for rebase).
alias gloc="git log --oneline --decorate --color | cat -n | less"
# Create a temporary branch squashing the changes of the current branch into another one.
function gcotb {
CURRENT_BRANCH=$(git branch --show-current)
DEFAULT_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD --short | sed 's@^origin/@@')
if [ -n "$1" ] ;then
DEFAULT_BRANCH=$1
fi
echo -n "Would you like to create a temporary branch squashing \"$CURRENT_BRANCH\" into \"$DEFAULT_BRANCH\"? (y/N) "
read answer
if [ "$answer" != "${answer#[Yy]}" ] ;then
TEMP_BRANCH=temp/squash_$(date -u +%Y%m%d%H%M%S)
git checkout $DEFAULT_BRANCH && git pull
git checkout -b $TEMP_BRANCH && git push -u origin $TEMP_BRANCH
git squash $CURRENT_BRANCH && git add -A
git commit -m "chore: squashed commits from \"$CURRENT_BRANCH\""
git push
else
echo "Operation aborted."
fi
}
# Create a new branch based on the current one and push it to the remote.
function gcob {
echo "You're on branch \"$(git branch --show-current)\"."
git pull
echo "Creating branch with name \"$1\"."
git checkout -b $1
echo "Pushing branch to remote."
git push --set-upstream origin $1
}
# Delete a branch both on local and remote.
function gdel {
echo "Deleting branch \"$1\" both locally and remotely is a destructive operation."
echo -n "Do you want to proceed anyway? (y/N) "
read answer
if [ "$answer" != "${answer#[Yy]}" ] ;then
git branch -d $1 & git push origin --delete $1
echo "Branch \"$1\" deleted."
else
echo "Operation aborted."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment