From a0a475f6cb733fc54890026acf8b7210d4c7bc6d Mon Sep 17 00:00:00 2001 From: limingshuang Date: Fri, 17 Jan 2025 16:55:42 +0800 Subject: [PATCH 1/6] refactor: compatible with multiple remotes, automatically get the current remote name Signed-off-by: limingshuang --- git-first | 5 +++-- git-last | 7 ++++--- git-next | 5 +++-- git-prev | 1 - 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/git-first b/git-first index 96efc91..98b7ff4 100755 --- a/git-first +++ b/git-first @@ -1,7 +1,8 @@ #!/bin/sh first() { - branch=`git symbolic-ref refs/remotes/origin/HEAD` - git log --reverse --pretty=%H $branch | head -1 | xargs git checkout + REMOTE=$(git remote) + branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")} + git log --reverse --pretty=%H "$branch" | head -1 | xargs git checkout } first "$@" diff --git a/git-last b/git-last index 5da950c..ce38918 100755 --- a/git-last +++ b/git-last @@ -1,7 +1,8 @@ #!/bin/sh last() { - branch=`git symbolic-ref refs/remotes/origin/HEAD` - git log --pretty=%H $branch | head -1 | xargs git checkout + REMOTE=$(git remote) + branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")} + git log -n 1 --pretty=%H "$branch" | git checkout } -last "$@" \ No newline at end of file +last "$@" diff --git a/git-next b/git-next index 93862c3..0e2c71e 100755 --- a/git-next +++ b/git-next @@ -1,12 +1,13 @@ #!/bin/sh next() { - branch=`git symbolic-ref refs/remotes/origin/HEAD` + REMOTE=$(git remote) + branch=$(git symbolic-ref "refs/remotes/$REMOTE/HEAD") if [ -z "$1" ]; then n=1 else n=$1 fi - git log --reverse --pretty=%H $branch | grep -A $n $(git rev-parse HEAD) | tail -1 | xargs git checkout + git log --reverse --pretty=%H "$branch" | grep -A $n $(git rev-parse HEAD) | tail -1 | xargs git checkout } next "$@" diff --git a/git-prev b/git-prev index 89102ec..7a01088 100755 --- a/git-prev +++ b/git-prev @@ -1,7 +1,6 @@ #!/bin/sh prev() { - branch=`git symbolic-ref refs/remotes/origin/HEAD` if [ -z "$1" ]; then n=1 else From 14834005aade7b633b82c5d6d078ea5ecb8c1c7f Mon Sep 17 00:00:00 2001 From: limingshuang Date: Fri, 17 Jan 2025 16:56:29 +0800 Subject: [PATCH 2/6] feat: add git-paging-alias.txt Signed-off-by: limingshuang --- git-paging-alias.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 git-paging-alias.txt diff --git a/git-paging-alias.txt b/git-paging-alias.txt new file mode 100644 index 0000000..38b18f1 --- /dev/null +++ b/git-paging-alias.txt @@ -0,0 +1,21 @@ +[alias] + swc-first = !"first() { \ + REMOTE=\"$(git remote)\"; \ + branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ + git switch --detach $(git log --reverse --pretty=%H \"$branch\" | head -1); \ + };first" + swc-last = !"last(){ \ + REMOTE=\"$(git remote)\"; \ + branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ + git switch --detach $(git log -n 1 --pretty=%H \"$branch\"); \ + };last" + swc-prev = !"prev(){ \ + n=\"${1:-1}\"; \ + git checkout HEAD~$n; \ + };prev" + swc-next = !"next(){ \ + REMOTE=\"$(git remote)\"; \ + branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ + n=\"${1:-1}\"; \ + git switch --detach $(git log --reverse --pretty=%H \"$branch\" | grep -A $n $(git rev-parse HEAD) | tail -1); \ + };next" From e14ed451ae7cb3229928829f58b5223415a71f6e Mon Sep 17 00:00:00 2001 From: limingshuang Date: Fri, 17 Jan 2025 16:56:57 +0800 Subject: [PATCH 3/6] feat: add `git hist` - show history commits Signed-off-by: limingshuang --- git-hist | 14 ++++++++++++++ git-paging-alias.txt | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 git-hist diff --git a/git-hist b/git-hist new file mode 100644 index 0000000..c3d5ccb --- /dev/null +++ b/git-hist @@ -0,0 +1,14 @@ +#!/bin/sh + +hist() { + if test -z $1;then + REMOTE=$(git remote) + params=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")} + else + params="$@" + fi + git log --color --graph --abbrev-commit \ + --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)%Creset' \ + $params +} +hist "$@" diff --git a/git-paging-alias.txt b/git-paging-alias.txt index 38b18f1..bcc580b 100644 --- a/git-paging-alias.txt +++ b/git-paging-alias.txt @@ -1,4 +1,18 @@ [alias] + hist = !"hist() { \ + if test -z $1;then \ + REMOTE=\"$(git remote)\"; \ + params=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ + else \ + params=\"$@\"; \ + fi; \ + git log \ + --color \ + --graph \ + --abbrev-commit \ + --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)%Creset' \ + $params; \ + };hist" swc-first = !"first() { \ REMOTE=\"$(git remote)\"; \ branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ From ecff24c101c155fc95308ca4d56a8e39e138a035 Mon Sep 17 00:00:00 2001 From: limingshuang Date: Fri, 17 Jan 2025 16:57:23 +0800 Subject: [PATCH 4/6] refactor: update install.sh - install commonds by git alias Signed-off-by: limingshuang --- install.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/install.sh b/install.sh index 945ea4e..93e1886 100755 --- a/install.sh +++ b/install.sh @@ -1,2 +1,6 @@ #!/bin/sh -install -m 755 git-* /usr/local/bin +if test -f git-paging-alias.txt;then + git config --global --add include.path $(readlink -f git-paging-alias.txt) +else + install -m 755 git-* /usr/local/bin +fi From e1108646bacb5ece8f3d4b438d4f147d0d979690 Mon Sep 17 00:00:00 2001 From: limingshuang Date: Fri, 17 Jan 2025 16:57:42 +0800 Subject: [PATCH 5/6] refactor: update README.md Signed-off-by: limingshuang --- README.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 30553dd..11e7eab 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,26 @@ Treat git log as a book, exec `git next` or `git prev` to checkout the next or the previous commit. -### 像翻页一样跳转到上一(n)条或下一(n)条 Git 历史节点 + +## How to install? + +1. Clone this repository +2. Execute command : `./install.sh` + +or + +1. Download the `git-paging-alias.txt` +2. Execute `git config --global --add include.path PATH/git-paging-ali.txt` + +## Git alias guidelines + +1. `git hist [ or []]` - show all history commits +2. `git swc-first` - switch the first commit +3. `git swc-last` - switch the last commit +4. `git swc-prev` - switch previous commit +5. `git swc-next` - switch next commit + +## 像翻页一样跳转到上一(n)条或下一(n)条 Git 历史节点 请查看博客文章 [阅读开源代码小技巧](https://hutusi.com/git-paging) 获得更多使用帮助。 + From 6d50944be0c0bfd22cf956404780a6155c50c7c4 Mon Sep 17 00:00:00 2001 From: MinsonLee Date: Sat, 18 Jan 2025 16:25:15 +0800 Subject: [PATCH 6/6] fix(alias-next): git alias swc-next - get current branch error Signed-off-by: MinsonLee --- git-paging-alias.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-paging-alias.txt b/git-paging-alias.txt index bcc580b..061618a 100644 --- a/git-paging-alias.txt +++ b/git-paging-alias.txt @@ -29,7 +29,7 @@ };prev" swc-next = !"next(){ \ REMOTE=\"$(git remote)\"; \ - branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ + branch=\"$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")\"; \ n=\"${1:-1}\"; \ git switch --detach $(git log --reverse --pretty=%H \"$branch\" | grep -A $n $(git rev-parse HEAD) | tail -1); \ };next"