Created
July 18, 2023 11:32
-
-
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
This file contains hidden or 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
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
This will create a new branch (say current branch is
the-pr
, new branch will bethe-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 likeghc
(g
ith
ubc
ompare) for ease of access: