Skip to content

Instantly share code, notes, and snippets.

@andreievg
Created July 18, 2023 11:32
Show Gist options
  • Save andreievg/a091b81f5cc0ac8abd061588ab206bb8 to your computer and use it in GitHub Desktop.
Save andreievg/a091b81f5cc0ac8abd061588ab206bb8 to your computer and use it in GitHub Desktop.
Bash script to create a branch with current changes and compare to current branch for github
#!/bin/bash
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# Replace every character that is not letter or number with dash
COMMIT_MESSAGE="$@"
SANITISED_COMMIT=$(echo $COMMIT_MESSAGE | sed -E 's/[^a-zA-Z0-9]/\-/g')
NEW_BRANCH="$CURRENT_BRANCH($SANITISED_COMMIT)"
git checkout -b $NEW_BRANCH
git add -A
git commit -m "$COMMIT_MESSAGE"
git push --set-upstream origin $NEW_BRANCH
git checkout $CURRENT_BRANCH
# https://github.com/{org}/{repo}.git
REPO_URL_NAME=$(git remote get-url origin)
# Remove .get
SANITISED_URL=$(echo $REPO_URL_NAME | sed -E 's/(\.git)//g')
open "$SANITISED_URL/compare/$CURRENT_BRANCH...$NEW_BRANCH"
@andreievg
Copy link
Author

During PR review (which I usually do in VS code), I quite often do code change suggestions (more then one line suggestion), commit them to a new branch and show the diff in github compare view

This script helps with automating this task:

After making the changes run

./commit_and_compare.sh Your commit name here

This will create a new branch (say current branch is the-pr, new branch will be the-pr(Your-commit-name-here)), and then open browser with github compare link (i.e. https://github.com/{yourorg}/{yourrepo}/compare/the-pr...the-pr(Your-commit-name-here))

You can save this script to /usr/local/bin under a short name like ghc (github compare) for ease of access:

ghc Your commit name here

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