usoo4w
Last Updated: February 25, 2016
·
2.717K
· dcunit3d

Gync/Gush/Gull - Manage git branches as lists

As usual

I don't feel like describing how it works, but i will describe the value-add.

You know those times when you're working on 5-10 different branches? Here's a great way to automate the process of rebasing upstream changes in. Done manually, this can eat up a ton of time everyday. Now you can do the same thing in 30 seconds.

Notable Functions:

  • Gitar: easily pass a list of branches to xargs. bash is functional!
  • Gitlist: iterate through a list of branches and run a command on each one
  • Gwip: stash changes create a W.I.P branch.
  • Gull: pull changes from a single upstream branch into multiple local branches
    • recommend against using this one
  • Gync: rebase changes from a single local branch into multiple other branches
  • Gush: push changes from multiple branches up to your remote.
    • recommend against using this, especially if you're using a CI service.

Nota Bene

  • some of these commands can be a little dangerous
    • Mostly to your local git tree, but that's no fun if you bork
  • and it's automated, so you're working with several branches.
    • so tread carefully.
    • if you're unsure, run it on a test branch

Here's the code:

gitar/gitlist - work with lists of branches, functionally

gitar() { echo $* | xargs -n1 echo }

gitlist() { xargs -I -=-= sh -c "git checkout -=-=; $@"; }

gls() {
  # swap $1 & $2?
  git branch $2 |
    grep "$1" |
    sed "s/^.*\\($1\\/.*\\)$/\\1/"; }

glstatus() { gls | gitlist "git status"; }

gull/gync/gush/gushforce

  • here's the meat
  • gushforce lol
gull() { gitar $* | gitlist "git pull --rebase origin -=-=; if [ \"\$?\" -ne 0 ]; then echo [[COULD NOT PULL]] -=-=; git rebase --abort; fi;" }

gync() { gitar $* | gitlist "git rebase master; if [ \"\$?\" -ne 0 ]; then echo [[COULD NOT REBASE]] -=-=; git rebase --abort; fi;" }

gush() { gitar $* | gitlist "git push origin -=-=; if [ \"\$?\" -ne 0 ]; then echo [[COULD NOT PUSH]] -=-=; fi; "}

gushforce() { gitar $* | gitlist "git push --force origin -=-=; if [ \"\$?\" -ne 0 ]; then echo [[COULD NOT FORCE PUSH]] -=-=; fi; "}

gwip & grewip - stash in a temp. local WIP branch

  • make sure you start on a branch that's not master!
    • if your script uses gwip/grewip (like grbsync)
    • removing the wip commit from every rebased branch is a pain
gwip() {
  gwipname=${1:-wip}-`date +%s`
  git add -A;
  git ls-files --deleted -z |
    xargs -0 -I {} git rm {};
  git commit -m $gwipname;
  echo $gwipname; }

grewip() {
  git log -n 1 |
    grep -q -c $1 &&
      git reset HEAD~1; }

Bash Arrays are easy!

These are the branches you're working with. Define these in a script that runs when you open a project's root directory.

## most often used branches
 g=dc/feature/cool-feature-bro
 gg=dc/feature/user-is-rad
 ggg=dc/feature/user-friends-friends-friends

# hotfixes
 gh=dc/hotfix/admin-user-no-friends

# test branches
 gt=dc/test/some-cool-test-refactors
 gtt=dc/test/integration-test

 gr=dc/refactor/db-deez
 grr=dc/refactor/something-totally-awesome

 #these arrays can all be used with `gitar $rg | gitlist "git pull origin -=-="`
 # r for array
 rg=($g $gg $ggg)
 rt=($gt $gtt)
 rh=($gh)
 rr=($gr $grr)
 ra=(${rg[@]} ${rt[@]} ${rh[@]} ${rr[@]})

# also, you can use
   `gls [user] | gitlist "git pull origin -=-="`