Understanding the git alias syntax and building a few examples:
git alias syntax
The ! tells Git:“Don’t prepend the git command; run ‘as-is’ in the system shell (Bash/Zsh).”
In a Git alias, the exclamation point ! tells Git: * “Don’t prepend the git command; run ‘as-is’ in the system shell (Bash/Zsh).”
With !: You can string multiple commands together using &&, use pipes (|), or even run non-git commands like echo.
Without !: Git expects a standard Git subcommand. For example, alias.st status just runs git status.
When you define an alias without the !, Git automatically prepends git to whatever you wrote.
- For example:
alias.sview "stash show -p"- Git executes:
git stash show -p
- Git executes:
- If you had written
alias.sview "git stash show -p",- Git would try to execute:
git git stash show -p, which would have failed.
- Git would try to execute:
Rule of Thumb:
- Simple alias: Leave
gitout, don’t use!. - Complex alias (pipes, logic, or multiple commands): Start with
!, and you must includegitfor every command in the chain.
Comparison Table
| Alias Style | Syntax Example | When to use? |
| Simple | co checkout | Renaming or shortening a single command. |
| Shell (!) | !git fetch && git status | Combining multiple commands or using logic. |
git cleanup
git config --global alias.cleanup "!git fetch -p && git branch --merged main | grep -v '^*\\| main' | xargs -r git branch -d"
git config --global alias.cleanup "!git fetch -p && git branch --merged main | grep -v '^*\\| main' | xargs -r git branch -d"
Alias break-down
- git fetch -p ~ This tells your local git: “If the branch was deleted on GitHub, forget about it here too”
- git branch –merged main ~ Lists branches
mainalready contains. - Excludes your current branch and the actual
mainbranch so you don’t accidentally delete the branch you’re standing on. - Deletes the remaining branches
xargs
xargs is a powerful tool that takes the output of one command and turns it into arguments for another.
- The Chain:
git branch --mergedproduces a list of names.
- The Problem:
- You can’t just pipe text into
git branch -d. - It doesn’t know what to do with the text.
- You can’t just pipe text into
- The Solution:
xargsgrabs those names and “pastes” them onto the end of the command.- So it effectively executes:
git branch -d branch1 branch2 branch3.
- The
-rflag:- This stands for
--no-run-if-empty. - If no branches are merged,
xargswill stay quiet. - Without
-r, it would try to rungit branch -dwith no name, which would throw an error.
- This stands for
git sview
git config --global alias.sview "stash show -p"
Alias break-down
- simple short cut to save typing “stash show -p”
- this alias shows what’s been stashed (example below)
diff --git a/lib/math/math_utils.sh b/lib/math/math_utils.sh
index ebd4403..4aec719 100644
--- a/lib/math/math_utils.sh
+++ b/lib/math/math_utils.sh
@@ -3,10 +3,10 @@
convert_to_bytes() {
local input=${1:-0} # default:=0
- local number=$(echo "$input" | sed 's/[^0-9]//g')
+ local number=$(echo "$input" | sed 's/[^0-9.]//g')
# If sed stripped everything and left nothing, set number to 0
[[ -z "$number" ]] && number=0
- local unit=$(echo "$input" | sed 's/[0-9]//g' | tr -d ' ')
+ local unit=$(echo "$input" | sed 's/[0-9.]//g' | tr -d ' ')
case "$unit" in
"B") echo "$number" | awk '{printf "%.0f\n", $1}' ;;
(END)
git lg
git config --global alias.lg "log --oneline --graph --decorate --all"
Alias break-down
- simple short cut to save typing and/or remembering the flags for: “log –oneline –graph –decorate –all”
- this alias shows the branch tree (example below)
* 477e895 (HEAD -> main, tag: v0.0.3, origin/main) Merge pull request 'ALL 8 TESTS ARE WORKING' (#3) from breakfix/testing-scripts into main
|\
| * 3d2c46f (origin/breakfix/testing-scripts, breakfix/testing-scripts) ALL 8 TESTS ARE WORKING
|/
* 7b05f0d (tag: v0.0.2, feature/show-all-sources, feature/fix-set-e) Merge pull request 'feature/docker' (#2) from feature/docker into main
|\
| * fa84711 (origin/feature/docker, feature/docker) refactor variables in progress_bar function
| * 27d76f9 Working with all commented out trys
| * d268d8b Added Poor Man's DEBUG_TIMERS
| * c75ef7b Added docker functions from docker-prune-review.sh
| * 38bff06 Added header function from docker-prune-review
|/
* 804f04f (tag: v0.01) Merge pull request 'feature/core' (#1) from feature/core into main
|\
| | * c6d4ff6 (refs/stash) WIP on core: 2c182f7 CLEAN-WORKING
| |/|
| | * aa350ff index on core: 2c182f7 CLEAN-WORKING
| |/
| * 2c182f7 (origin/feature/core, feature/core) CLEAN-WORKING
| * dc43d53 Updated README.md
|/
* dc62cb8 Initial
(END)
