From b1899073ebe2eb3289aa19254c42d98e48e8c8b1 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 15 Mar 2024 23:01:55 +0300 Subject: [PATCH 1/5] Fix(rev-parse): Add missing `git` --- gitalias.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitalias.txt b/gitalias.txt index bbe0a47..178db3e 100644 --- a/gitalias.txt +++ b/gitalias.txt @@ -1146,7 +1146,7 @@ panic = "!tar cvf ../panic.tar -- *" # Create an archive file of everything in the repo - archive = "!f() { top=\"$(rev-parse --show-toplevel)\"; cd \"$top\" || exit 1 ; tar cvf \"$top.tar\" \"$top\" ; }; f" + archive = "!f() { top=\"$(git rev-parse --show-toplevel)\"; cd \"$top\" || exit 1 ; tar cvf \"$top.tar\" \"$top\" ; }; f" # Push with a force and lease, which means that you're pushing in order # to forcefully overwrite the remote, and you want a safety check first: From 7f5ce27df12042f6c9e4b89168a456fdc8a9a96f Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 15 Mar 2024 23:05:17 +0300 Subject: [PATCH 2/5] Refactor: Use `current-branch`. Everywhere! --- gitalias.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gitalias.txt b/gitalias.txt index 178db3e..c6dfd09 100644 --- a/gitalias.txt +++ b/gitalias.txt @@ -114,7 +114,7 @@ # branch with show description; ideally git will add this feature in the future as `git --show-description` bsd = "!f(){ \ - branch=\"${1:-$(git rev-parse --abbrev-ref HEAD 2>/dev/null)}\"; \ + branch=\"${1:-$(git current-branch)}\"; \ git config \"branch.$branch.description\"; \ };f" @@ -831,7 +831,7 @@ # summary: print a helpful summary of some typical metrics summary = "!f() { \ printf \"Summary of this branch...\n\"; \ - printf \"%s\n\" \"$(git rev-parse --abbrev-ref HEAD)\"; \ + printf \"%s\n\" \"$(git current-branch)\"; \ printf \"%s first commit timestamp\n\" \"$(git log --date-order --format=%cI | tail -1)\"; \ printf \"%s last commit timestamp\n\" \"$(git log -1 --date-order --format=%cI)\"; \ printf \"\nSummary of counts...\n\"; \ @@ -1183,8 +1183,8 @@ pull1 = "!git pull origin \"$(git current-branch)\"" # Track and untrack, with default parameters, and with printing the command - track = "!f(){ branch=\"$(git rev-parse --abbrev-ref HEAD)\"; cmd=\"git branch $branch -u ${1:-origin}/${2:-$branch}\"; echo \"$cmd\"; $cmd; }; f" - untrack = "!f(){ branch=\"$(git rev-parse --abbrev-ref HEAD)\"; cmd=\"git branch --unset-upstream ${1:-$branch}\"; echo \"$cmd\"; $cmd; }; f" + track = "!f(){ branch=\"$(git current-branch)\"; cmd=\"git branch $branch -u ${1:-origin}/${2:-$branch}\"; echo \"$cmd\"; $cmd; }; f" + untrack = "!f(){ branch=\"$(git current-branch)\"; cmd=\"git branch --unset-upstream ${1:-$branch}\"; echo \"$cmd\"; $cmd; }; f" # Track all remote branches that aren't already being tracked; # this is a bit hacky because of the parsing, and we welcome From 1f434acb2e7db987f9b82e31740c355047b4a8e4 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 15 Mar 2024 23:08:51 +0300 Subject: [PATCH 3/5] Add shorter and simpler way to find out upstream branch --- gitalias.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gitalias.txt b/gitalias.txt index c6dfd09..8bc2453 100644 --- a/gitalias.txt +++ b/gitalias.txt @@ -1416,6 +1416,8 @@ # Show the upstream branch name upstream-branch = "!git for-each-ref --format='%(upstream:short)' \"$(git symbolic-ref -q HEAD)\"" + # Another, shorter and simpler, way + upb = rev-parse --abbrev-ref "@{upstream}" # Execute shell scripts. Git always runs scripts in the top directory. # For example "git exec pwd" will always show you the top directory. From 7a95c8f056e64cecfe0663fe93f5efbcdab60200 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 15 Mar 2024 23:38:01 +0300 Subject: [PATCH 4/5] Refactor: Use --long-options where possible Advice: use short options in the command line. Use long options for git aliases, documentation, shell aliases, functions and scripts, StackOverflow questions and answers. --- gitalias.txt | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/gitalias.txt b/gitalias.txt index 8bc2453..3a91fde 100644 --- a/gitalias.txt +++ b/gitalias.txt @@ -172,11 +172,11 @@ # cherry-pick - continue the picking process cpc = cherry-pick --continue - # cherry-pick --no-commit a.k.a. without making a commit - cpn = cherry-pick -n + # cherry-pick -n a.k.a. without making a commit + cpn = cherry-pick --no-commit - # cherry-pick --no-commit a.k.a. without making a commit, and when when recording the commit, append a line that says "(cherry picked from commit ...)" - cpnx = cherry-pick -n -x + # cherry-pick -n a.k.a. without making a commit, and when when recording the commit, append a line that says "(cherry picked from commit ...)" + cpnx = cherry-pick --no-commit -x ### diff aliases ### @@ -202,8 +202,8 @@ ### grep aliases ### - # grep with -n (--line-number) means show line number - gn = grep -n + # grep -n means show line number + gn = grep --line-number # Search with our preferred options; a.k.a. `grep-group` gg = grep-group @@ -841,7 +841,7 @@ printf \"%d author count\n\" \"$(git log --format=oneline --format=\"%aE\" | awk '{a[$0]=1}END{for(i in a){n++;} print n}')\"; \ printf \"%d committer count\n\" \"$(git log --format=oneline --format=\"%cE\" | awk '{a[$0]=1}END{for(i in a){n++;} print n}')\"; \ printf \"%d local branch count\n\" \"$(git branch | grep -vc \" -> \")\"; \ - printf \"%d remote branch count\n\" \"$(git branch -r | grep -vc \" -> \")\"; \ + printf \"%d remote branch count\n\" \"$(git branch --remotes | grep -vc \" -> \")\"; \ printf \"\nSummary of this directory...\n\"; \ printf \"%s\n\" \"$(pwd)\"; \ printf \"%d file count via git ls-files\n\" \"$(git ls-files | wc -l)\"; \ @@ -1183,13 +1183,13 @@ pull1 = "!git pull origin \"$(git current-branch)\"" # Track and untrack, with default parameters, and with printing the command - track = "!f(){ branch=\"$(git current-branch)\"; cmd=\"git branch $branch -u ${1:-origin}/${2:-$branch}\"; echo \"$cmd\"; $cmd; }; f" + track = "!f(){ branch=\"$(git current-branch)\"; cmd=\"git branch $branch --set-upstream-to=${1:-origin}/${2:-$branch}\"; echo \"$cmd\"; $cmd; }; f" untrack = "!f(){ branch=\"$(git current-branch)\"; cmd=\"git branch --unset-upstream ${1:-$branch}\"; echo \"$cmd\"; $cmd; }; f" # Track all remote branches that aren't already being tracked; # this is a bit hacky because of the parsing, and we welcome # better code that works using more-specific git commands. - track-all-remote-branches = "!f() { git branch -r | grep -v ' -> ' | sed 's/^ \\+origin\\///' ; }; f" + track-all-remote-branches = "!f() { git branch --remotes | grep -v ' -> ' | sed 's/^ \\+origin\\///' ; }; f" ### reset-* & undo-* ### @@ -1293,7 +1293,7 @@ wip = "!git add --all; git ls-files --deleted -z | xargs -r -0 git rm; git commit --message=wip" # Restore the deleted files to the working tree. - unwip = "!git log -n 1 | grep -q -c wip && git reset HEAD~1" + unwip = "!git log --max-count=1 | grep -q -c wip && git reset HEAD~1" # Assume & Unassume # @@ -1329,7 +1329,7 @@ # assume = update-index --assume-unchanged unassume = update-index --no-assume-unchanged - assume-all = "!git st -s | awk '{ print $2 }' | xargs -r git assume" + assume-all = "!git status --short | awk '{ print $2 }' | xargs -r git assume" unassume-all = "!git assumed | xargs -r git update-index --no-assume-unchanged" assumed = "!git ls-files -v | grep ^h | cut -c 3-" @@ -1392,7 +1392,7 @@ # # Calls the `publish` and `unpublish` aliases. # - reincarnate = "!f() { [ $# -gt 0 ] && git checkout \"$1\" && git unpublish && git checkout main && git branch -D \"$1\" && git checkout -b \"$1\" && git publish; }; f" + reincarnate = "!f() { [ $# -gt 0 ] && git checkout \"$1\" && git unpublish && git checkout main && git branch --delete --force \"$1\" && git checkout -b \"$1\" && git publish; }; f" # Friendly wording is easier to remember. # Thanks to http://gggritso.com/human-git-aliases @@ -1649,7 +1649,7 @@ git checkout \"$old_branch\"; \ git pull --ff-only; \ git checkout -b \"$new_branch\" \"$old_branch\"; \ - git push -u origin \"$new_branch\"; \ + git push --set-upstream origin \"$new_branch\"; \ };f" # Stop a topic branch; this must be the current branch. From 007b29589b37962c305405d5f6b052cda8c0be68 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 15 Mar 2024 23:47:23 +0300 Subject: [PATCH 5/5] Fix(topic-move): Set upstream branch on push --- gitalias.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitalias.txt b/gitalias.txt index 3a91fde..719011d 100644 --- a/gitalias.txt +++ b/gitalias.txt @@ -1746,7 +1746,7 @@ new_branch=\"$1\"; \ old_branch=\"$(git current-branch)\"; \ git branch --move \"$old_branch\" \"$new_branch\"; \ - git push origin \":$old_branch\" \"$new_branch\"; \ + git push --set-upstream origin \":$old_branch\" \"$new_branch\"; \ };f"