top | item 32472649

(no title)

firesloth | 3 years ago

I have a bash function I use to checkout a git branch based on a search string:

  function git-checkout-branch-by-search-string() {
    local maybe_branch_name
    maybe_branch_name=$(git branch --sort=-committerdate | grep $1 | head -n 1)
    if [ -n "$maybe_branch_name" ]; then
      git checkout "${maybe_branch_name:2}"
    else
      echo "Could not find branch matching $1"
    fi
  }
  alias gcos="git-checkout-branch-by-search-string"
Branches often include things like ticket numbers and project keys, so you can do

  $ gcos 1234
and save some typing.

I have a pair of fixup commit functions, which make it faster to target fixup commits prior to rebasing:

  function git-commit-fixup() {
    git commit --fixup ":/$*"
  }
  function git-add-all-then-git-commit-fixup() {
    git add .
    git commit --fixup ":/$*"
  }
Long function names that are then assigned to an alias can make it easier to find them later if you forget rarely used ones. That is you can do:

$ alias | grep fixup

to see the list of relevant aliases and the functions they call.

I also have two functions I use like a linear git bisect:

  function git-checkout-parent-commit() {
    local prev
    prev=$(git rev-parse HEAD~1)
    git checkout "$prev"
  }
  function git-checkout-child-commit() {
    local forward
    forward=$(git-children-of HEAD | tail -1)
    git checkout "$forward"
  }
  function git-children-of() {
    for arg in "$@"; do
      for commit in $(git rev-parse $arg^0); do
        for child in $(git log --format='%H %P' --all | grep -F " $commit" | cut -f1 -d' '); do
          echo $child
        done
      done
    done
  }

discuss

order

No comments yet.