Skip to content

Instantly share code, notes, and snippets.

@mikhail
Last active September 25, 2015 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikhail/9bdb1d274dece54b14a0 to your computer and use it in GitHub Desktop.
Save mikhail/9bdb1d274dece54b14a0 to your computer and use it in GitHub Desktop.
Working with forks, branches, and pull requests

Helper scripts for git

When working with forks & branches for features and pull requests I often find myself needing common actions, like

  1. Create a new branch, and I don't care what it's named
  2. Update the last commit, and keep the commit message
  3. List all the branches I have with some description of what that branch is

Below are the scripts to do just that.

Git is sexy

Git will find any command in your $PATH that begins with git- and allow you to use it as a native git command or alias. So for the scripts below, the invocation is very simple:

$ git ls
  * feature_27745            27c8f25 Add `sudo su` permission to dev users.
  master                     10ce941 Merge pull request #62 from mikhail/feature_54821

git checkout master

$ git fb Demonstrating this love
Branch feature_32609 set up to track local branch master by rebasing.
Switched to a new branch 'feature_32609'
[feature_32609 76094e2] Demonstrating this love
$ git ls
  * feature_32609            76094e2 Demonstrating this love
  feature_27745              27c8f25 Add `sudo su` permission to dev users.
  master                     10ce941 Merge pull request #62 from mikhail/feature_54821

git add .

$ git update
[feature_32609 dbe0d03] Demonstrating this love
 Date: Thu May 28 13:37:10 2015 -0700
 1 file changed, 1527 insertions(+)
 create mode 100644 patch

git push origin feature_32609 -f

Interactive

Lastly, combinging some bash magic -- git-lsi will provide an ls with interactive menu. Preview

#!/bin/bash
git checkout -t -b feature_$RANDOM
git commit --allow-empty -m "${*:1}"
#!/bin/bash
branch=""
branches=`git branch --list`
while read -r branch; do
# git marks current branch with "* ", remove it
clean_branch_name=${branch//\*\ /}
lastcommit=`git log -n1 --oneline $clean_branch_name`
printf " %-25s $lastcommit\n" "$branch"
done <<< "$branches"
#!/bin/bash
# Store menu options selected by the user
INPUT=/tmp/menu.sh.$$
# trap and delete temp files
trap "rm $INPUT; exit" SIGHUP SIGINT SIGTERM
branch=""
branches=`git branch --list`
menuitems=""
while read -r branch; do
# git marks current branch with "* ", remove it
clean_branch_name=${branch//\*\ /}
lastcommit=`git log -n1 --oneline $clean_branch_name`
#lastcommit=${lastcommit//\ /\\\ }
menuitems="$menuitems $clean_branch_name \"$lastcommit\" "
done <<< "$branches"
### display main menu ###
echo $menuitems | xargs dialog \
--title "git-ls" \
--menu "Choose a branch" 15 80 4 \
2>"${INPUT}"
branch=$(<"${INPUT}")
[ -f $INPUT ] && rm $INPUT
[ -z "$branch" ] && echo 'Canceled...' && exit
echo Selected branch: $branch
git checkout $branch
#!/bin/bash
git commit --amend --no-edit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment