Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I use a custom git alias `git recent` almost every day. It shows you the the most recent branches you have worked on. This is useful for when you are are trying to find a branch you have worked on recently, but forgot its name.

To use this alias, make an executable file called `git-recent` with the following contents and ensure it is in your `$PATH`

    git for-each-ref \
      --sort=-committerdate refs/heads/ \
      --format='%(HEAD) %(color:red)%(objectname:short)%(color:reset) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'

Here's a redacted example of the output looks like

    * fc924a7e68 team/name/feature-2 - save - My Name (4 days ago)
      2fed1acfac team/name/feature-1 - add test - My Name (3 weeks ago)
      4db4d4ac77 main - Remove changes (#22397) - My Name (6 weeks ago)


I have a similar one with slightly different output, however mine appends the top N stashes as well for additional context.

  # Branches
  2022-08-11 13:26:34 -0700 4 days ago    branch-1
  2022-07-13 07:49:36 -0700 5 weeks ago   branch-10
  2022-06-28 23:40:53 +0000 7 weeks ago   branch-8
  2022-06-28 22:47:31 +0000 7 weeks ago   main
  2022-06-28 19:24:10 +0000 7 weeks ago   branch-7
  # Stashes
  stash@{0}:Thu Aug 11 13:17:11 2022 -0700 WIP on branch-3: afa19e7444a Some changes based on morning sync
  stash@{1}:Tue Jul 26 13:25:37 2022 -0700 WIP on branch-5: bd6122e2dfa find() bugfix
  stash@{2}:Tue Jul 12 15:05:31 2022 -0700 WIP on branch-7: 1221d0640c5 linter
Code:

  recent() 
  { 
      echo -e "${PURPLE}# Branches${COLOR_END}";
      for k in $(git branch | perl -pe 's/^..(.*?)( ->.*)?$/\1/');
      do
          echo -e $(git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset " $k -- | head -n 1)\\\t$k;
      done | sort -r | head;
      _num_stashes=$(git stash list | wc -l | while read l; do echo "$l - 1"; done | bc);
      echo -e "${PURPLE}# Stashes${COLOR_END}";
      for i in $(seq 0 ${_num_stashes});
      do
          echo -en "${CYAN}stash@{${i}}:${GREEN}" && git show --format="%ad%Creset %s" stash@{$i} | head -n 1;
      done
  }


I use something similar along with fzf, so I can search the branch and switch to it.

git alias:

  recent = "!f() { script -q /dev/null git for-each-ref --sort=-committerdate refs/heads/ --format='%(HEAD)%(color:yellow)%(refname:short)%(color:reset)|%(authordate:short)|%(color:red)%(objectname:short)%(color:reset)|%(subject) (%(color:green)%(committerdate:relative)%(color:reset))'|column -ts'|'; }; f"
zsh function:

  grecent() {
    local branch=$(git recent | fzf --ansi --no-multi --bind "q:abort")

    if [[ "$branch" = "" ]]; then
        echo "No branch selected."
        return
    fi

    local branch_name=$(echo "$branch" | awk '{print $1}')
    echo "git checkout $branch_name"
    git checkout "$branch_name"
  }
But sometimes it screws up the terminal and I have to run `reset` to fix. Will be great if someone helps with that.


Just stole this. Thanks a lot!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: