How to auto-scroll to location of git hash commit on git tree on CLI

Viewed 225

Background

I normally stick to git cli for all my git operations. The only time I use a git gui tool is when i want to see the location of a git commit relative to the tree.

Right now I have this command line that shows me the git tree:

git log --graph --decorate --pretty=oneline --abbrev-commit --all

which shows me a nice tree illustration:

* 6068f4b (origin/develop) exclude arm64 architecture
* d49b4be Fix crash for global mark jump in Xcode12
*   6e32166 Merge branch 'master' into develop
|\
| * 6356710 (HEAD -> master, origin/master, origin/HEAD) README.md
* | b126142 update project
* | 3b050e5 Failing Unit Test is commented out with FIXME comment addition
* | caad84d improve test
* |   0bc3054 Merge branch 'master' into develop
|\ \
| |/
| * a4a4fdb Fix Yank
* |   39d38ce Merge pull request #308 from chml/fix/yankAndPut
|\ \

Problem

The problem is that if i want to see the location of a git hash in this tree, I have to do the following:

  • run git checkout <target-hash>
  • run git log --graph --decorate --pretty=oneline --abbrev-commit --all
  • scroll on the git tree manually for a while until i see where HEAD is pointing to (this can take a while for git repos that are years old)
  • switch back to my original commit git checkout <original-commit>

Question

I would like a command that would make all these steps into one (so I should avoid changing my git HEAD commit). For git GUI's that's a pretty straight forward operation (both gitkraken and sourcetree offer that out of the box). For example in sourcetree this is a jump to commit operation:

enter image description here

Suggestions?

1 Answers

Let's do it in bash, open .bashrc or ~/.bash_profile

vim ~/.bashrc

let's create an alias for the git command you use at first, so you don't have to keep typing it

alias gitTree="git log --graph --decorate --pretty=oneline --abbrev-commit --all"

Now, let's write a function that shows the head

function gshow(){
    git checkout "$1" --quiet
    git log --graph --decorate --pretty=oneline --abbrev-commit --all | grep "(HEAD)"
    git checkout - --quiet
}

close the file, and run this from the terminal source ~/.bashrc or source ~/.bash_profile

now you call it from the terminal

gshow <target-hash>

I added the --quiet command so it only shows you what you want to see, and when done, it returns to the previous branch.

update

I updated the script to make it default to showing a 10 line context, but that 10 line can be expanded in case the user wants more context:

function gshow(){
    ARG2=${2:-10}
    git checkout "$1" --quiet
    git log --graph --decorate --pretty=oneline --abbrev-commit --all | grep -C $ARG2 "(HEAD)"
    git checkout - --quiet
}

example usage:

with no context arg

$ gshow 0bc3df2
|\ \ \
| * | | dff86b7 Add an FAQ on why users need to resign Xcode to use Xvim2
| | |/
| |/|
* | |   d267b5f Merge pull request #173 from 99arobe/task/update-README-to-remove-dead-link
|\ \ \
| |/ /
|/| |
| * | 64ecb94 Remove contributing guidelines from the README
|/ /
* | 0bc3df2 (HEAD) add comment
* | 2bddec1 add header update command comment
* | e244543 update Xcode10 beta3 header
* |   763b317 Merge branch 'master' into develop
|\ \
| |/
| *   2e50b5a Merge pull request #154 from rick-kilgore/master
| |\
| | * fc3eb9c fudge for the lines that might be only partially shown
| | * 62efd89 move logic into switch for XVIM_SCROLL_TYPE_LINE
| | * c4da83f allow scrolling to the first and last lines.  ensure cursor is in the right place

with context arg

$ gshow 0bc3df2 20
|\ \ \
| |/ /
| * | ad28881 update branches and release readme
* | | 07d6820 deployment target 10.12 -> 10.13, swift -> 4.0
* | |   b06fbdf Merge pull request #177 from timothycosta/develop
|\ \ \
| * | | 2c48bcf Implemented 'source' command
| * | | 98cf07d Source xvimrc on toggle xvim
|/ / /
* | |   c725592 Merge pull request #175 from 99arobe/task/update-README-to-include-reasons-for-resigning-Xcode
|\ \ \
| * | | dff86b7 Add an FAQ on why users need to resign Xcode to use Xvim2
| | |/
| |/|
* | |   d267b5f Merge pull request #173 from 99arobe/task/update-README-to-remove-dead-link
|\ \ \
| |/ /
|/| |
| * | 64ecb94 Remove contributing guidelines from the README
|/ /
* | 0bc3df2 (HEAD) add comment
* | 2bddec1 add header update command comment
* | e244543 update Xcode10 beta3 header
* |   763b317 Merge branch 'master' into develop
|\ \
| |/
| *   2e50b5a Merge pull request #154 from rick-kilgore/master
| |\
| | * fc3eb9c fudge for the lines that might be only partially shown
| | * 62efd89 move logic into switch for XVIM_SCROLL_TYPE_LINE
| | * c4da83f allow scrolling to the first and last lines.  ensure cursor is in the right place
| | * 17ab836 don't move cursor on ctrl-e, ctrl-y
| |/
* | 7f4943d update the mangled signature of addSelectedRange
* | ac3faea fix #161 Xcode 10: Crash while edit in 'build phases'
* | 7dc4ea9 fix #158 not work C-f,C-b,C-u,C-d in Xcode10
* | 6397eff improve swift source
* | 9415303 add logger for swift
* | dae173f change swift function pointer address for Xcode10
* | d95e598 change from Xcode9.3 header to Xcode10
* | da8b31d class-dump Xcode10
Related