Last active
March 24, 2020 14:33
-
-
Save timnew/8e2370e49f2406474f8abc413523438c to your computer and use it in GitHub Desktop.
Shell command to manage .gitignore file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alias git-clean-squashed='basebr=${BASE:=master}; git checkout -q $basebr && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base $basebr $branch) && [[ $(git cherry $basebr $(git commit-tree $(git rev-parse $branch\^{tree}) -p $mergeBase -m _)) == "-"* ]] && git branch -D $branch; done' | |
alias git-clean-merged='git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d' | |
alias gbcm='git-clean-merged' | |
alias gbcs='git-clean-squashed' | |
alias gbca='before_branches=$(git branch);git-clean-merged;git-clean-squashed;after_branches=$(git branch);diff <(echo "$before_branches") <(echo "$after_branches")' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Download the file as /usr/local/bin/git-ignore | |
# reset environment variables that could interfere with normal usage | |
export GREP_OPTIONS= | |
# put all utility functions here | |
# make a temporary file | |
git_extra_mktemp() { | |
mktemp -t "$(basename "$0")".XXX | |
} | |
# | |
# check whether current directory is inside a git repository | |
# | |
is_git_repo() { | |
git rev-parse --show-toplevel > /dev/null 2>&1 | |
result=$? | |
if test $result != 0; then | |
>&2 echo 'Not a git repo!' | |
exit $result | |
fi | |
} | |
is_git_repo | |
function show_contents { | |
local file="${2/#~/$HOME}" | |
if [ -f "$file" ]; then | |
echo "$1 gitignore: $2" && cat "$file" | |
else | |
echo "There is no $1 .gitignore yet" | |
fi | |
} | |
function show_global { | |
show_contents Global `git config --global core.excludesfile` | |
} | |
function add_global { | |
local global_gitignore=$(git config --global core.excludesfile) | |
if [ -z "$global_gitignore" ]; then | |
echo "Can't find global .gitignore." | |
echo "" | |
echo "Use 'git config --global --add core.excludesfile ~/.gitignore-global' to set the path to your global gitignore file to '~/.gitignore-global'." | |
echo "" | |
else | |
add_patterns `git config --global core.excludesfile` "$@" | |
fi | |
} | |
function show_local { | |
cd "$(git root)" | |
show_contents Local .gitignore | |
} | |
function add_local { | |
cd "$(git root)" | |
add_patterns .gitignore "$@" | |
} | |
function add_patterns { | |
echo "Adding pattern(s) to: $1" | |
local file="${1/#~/$HOME}" | |
for pattern in "${@:2}"; do | |
echo "... adding '$pattern'" | |
(test -f "$file" && test "$pattern" && grep -q -F -x -- "$pattern" "$file") || echo "$pattern" >> "$file" | |
done | |
} | |
if test $# -eq 0; then | |
show_global | |
echo "---------------------------------" | |
show_local | |
else | |
case "$1" in | |
-l|--local) | |
test $# -gt 1 && add_local "${@:2}" && echo | |
show_local | |
;; | |
-g|--global) | |
test $# -gt 1 && add_global "${@:2}" && echo | |
show_global | |
;; | |
*) | |
add_local "$@" | |
;; | |
esac | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment