@@ -27,6 +27,9 @@ https://github.com/naokazuterada/MarkdownTOC#usage -->
2727 - [ Git with 2FA] ( #git-with-2fa )
2828 - [ Mac OSX specifics] ( #mac-osx-specifics )
2929 - [ Aliases] ( #aliases )
30+ - [ Shorthands] ( #shorthands )
31+ - [ Cleaning] ( #cleaning )
32+ - [ Rewriting history] ( #rewriting-history )
3033 - [ Split diff] ( #split-diff )
3134 - [ Mergetool] ( #mergetool )
3235 - [ Cleanup \( different options\) ] ( #cleanup-different-options )
@@ -359,34 +362,69 @@ git config --global push.followTags true # https://git-scm.com/docs/git-config#D
359362
360363##### Aliases
361364
365+ Some of these aliases depend on one another, in which case it's noted in the comments.
366+
367+ ###### Shorthands
362368``` bash
363- git config --global alias.st " status"
364- git config --global alias.cm " commit"
365- git config --global alias.ca " commit -am"
366- git config --global alias.cap ' ! f() { git commit -am "$@" && git push --set-upstream origin "$(git rev-parse --abbrev-ref HEAD)"; }; f'
367- git config --global alias.camend " commit --amend -am"
368- git config --global alias.amend " commit --amend --no-edit -a"
369369git config --global alias.br " branch"
370+ git config --global alias.ca " commit -am"
371+ git config --global alias.cm " commit"
370372git config --global alias.co " checkout"
371373git config --global alias.mt " mergetool"
372- git config --global alias.lg " log --graph --decorate --pretty=oneline --abbrev-commit"
373- git config --global alias.fp " fetch -p --all" # purge and fetch all remotes
374- git config --global alias.defaultbranch ' ! f() { echo $(git remote show origin | grep "HEAD branch" | cut -d ":" -f 2 | xargs); }; f' # https://stackoverflow.com/questions/28666357#comment101797372_50056710
375- git config --global alias.df ' ! f() { git icdiff --color=always "$@" | less -eR; }; f' # no FX (keep output in terminal)
376- git config --global alias.pr ' ! git push --set-upstream origin "$(git rev-parse --abbrev-ref HEAD)"' # push a new branch. will be overwritten if git-extras is installed
377- git config --global alias.dm ' ! git fetch -p && for branch in `git branch -vv | grep ' " ': gone] ' | awk '" ' {print $1}' " '" ' `; do git branch -D $branch; done' # 'delete merged' - local branches that have been deleted on remote
378- git config --global alias.gg ' ! f() { git checkout "${1:-$(git defaultbranch)}" && git dm && git pull; }; f' # git gg develop -- no arg: defaultbranch. Return to default branch (or specified branch), delete merged, pull branch
379- git config --global alias.pall ' ! f() { START=$(git branch | grep "\*" | sed "s/^.//"); for i in $(git branch | sed "s/^.//"); do git checkout $i; git pull || break; done; git checkout $START; }; f' # 'pull all' - pull local branches that have been updated on remote
380- git config --global alias.undo ' ! f() { git reset --hard $(git rev-parse --abbrev-ref HEAD)@{${1-1}}; }; f' # https://megakemp.com/2016/08/25/git-undo/
381- git config --global alias.squashlast ' "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"' # Squash the last x commits; will prompt you with auto-squashed commit messages
374+ git config --global alias.st " status"
375+ # split diff - needs icdiff (see below) - use `git icdiff` to keep output in terminal after less quits
376+ git config --global alias.df ' ! f() { diff=$(git icdiff --color=always "$@") && test "$diff" && echo "$diff" | less -eR; }; f'
377+ # who needs the default verbose git log? - also try `git lg --all`
378+ git config --global alias.lg " log --graph --oneline"
379+ # tested with GitHub remote - ref https://stackoverflow.com/questions/28666357#comment101797372_50056710
380+ git config --global alias.defaultbranch ' ! f() { echo $(git remote show origin | grep "HEAD branch" | cut -d ":" -f 2 | xargs); }; f'
381+ # summary of all configured aliases
382382git config --global alias.alias " ! git config --get-regexp '^alias\.' | sed -e s/^alias\.// | grep -v ^'alias ' | sed 's/ /#/' | column -ts#"
383+ # "commit all & push" - needs ca,pr - usage `git cap 'Fix bug'` - runs autoformatting pre-commit hooks, commits all modified tracked files with message, and push
384+ git config --global alias.cap ' ! f() { git ca "$@" && git pr; }; f'
385+ # "pull request" - push new or existing branch skipping the usual --set-upstream error - alias will be overwritten when git-extras is installed
386+ git config --global alias.pr ' ! git push --set-upstream origin "$(git rev-parse --abbrev-ref HEAD)"'
387+ ```
388+
389+ ###### Cleaning
390+ ``` bash
391+ # "delete merged" - delete all local branches (-D) that have been deleted (merged) on remote
392+ git config --global alias.dm ' ! git fetch -p && for branch in `git branch -vv | grep ' " ': gone] ' | awk '" ' {print $1}' " '" ' `; do git branch -D $branch; done'
393+ # "fetch purge" - before fetching, remove any remote-tracking references that no longer exist on the remote
394+ git config --global alias.fp " fetch -p --all"
395+ # "rinse & repeat" - needs defaultbranch,dm - usage `git gg [develop]` - return to default branch (or specified branch), delete merged and pull
396+ git config --global alias.gg ' ! f() { git checkout "${1:-$(git defaultbranch)}" && git dm && git pull; }; f'
397+ # "pull all" - pull all local branches and return to original branch
398+ git config --global alias.pall ' ! f() { \
399+ START=$(git branch | grep "\*" | sed "s/^.//"); \
400+ for i in $(git branch | sed "s/^.//"); do \
401+ git checkout $i; \
402+ git pull || break; \
403+ done; \
404+ git checkout $START; \
405+ }; f'
406+ ```
407+
408+ ###### Rewriting history
409+ ``` bash
410+ # "commit all amend" last commit, adding all modified tracked files to the it without editing the commit message
411+ git config --global alias.amend " commit --amend --no-edit -a"
412+ # "commit all amend with message" - add all modified tracked files to the last commit with a new commit message
413+ git config --global alias.camend " commit --amend -am"
414+ # "squash last" X commits - allowing to edit a pre-generated commit message before committing - known caveat: when trying to squash into an initial commit, the reset fails
415+ git config --global alias.squashlast ' "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"'
416+ # "undo" whatever you did last, for instance an erroneous squashlast - ref https://megakemp.com/2016/08/25/git-undo/
417+ git config --global alias.undo ' ! f() { git reset --hard $(git rev-parse --abbrev-ref HEAD)@{${1-1}}; }; f'
383418```
384419
385420
386421##### Split diff
387422
423+ - ` git df ` (above) uses less that keeps a clean terminal
424+ - ` git icdiff ` (below) uses new core.pager that leaves less output in terminal after exiting
425+
388426``` bash
389- pip install git+https://github.com/jeffkaufman/icdiff.git # usage: 'git df' using 'less' or 'git icdiff' without 'less'
427+ pip install git+https://github.com/jeffkaufman/icdiff.git
390428git config --global --replace-all core.pager ' less -+$LESS -eFRSX' # with double quotes, $ will be evaluated
391429git config --global icdiff.options " --highlight --line-numbers --numlines=3"
392430git config --global difftool.icdiff.cmd ' icdiff --highlight --line-numbers --numlines=3 $LOCAL $REMOTE'
0 commit comments