Add branch-agnostic git aliases

This commit is contained in:
Tony Grosinger 2020-11-04 09:45:58 -08:00
parent 82649dc92d
commit c71e7f6634
5 changed files with 69 additions and 17 deletions

28
.gitattributes vendored Normal file
View File

@ -0,0 +1,28 @@
*.c diff=cpp
*.h diff=cpp
*.c++ diff=cpp
*.h++ diff=cpp
*.cpp diff=cpp
*.hpp diff=cpp
*.cc diff=cpp
*.hh diff=cpp
*.m diff=objc
*.mm diff=objc
*.cs diff=csharp
*.css diff=css
*.html diff=html
*.xhtml diff=html
*.js diff=javascript
*.ts diff=javascript
*.ex diff=elixir
*.exs diff=elixir
*.go diff=golang
*.php diff=php
*.pl diff=perl
*.py diff=python
*.md diff=markdown
*.rb diff=ruby
*.rake diff=ruby
*.rs diff=rust
*.lisp diff=lisp
*.el diff=lisp

View File

@ -12,7 +12,6 @@
untracked = 32 # Brighter blue
[alias]
bv = branch -vv
co = checkout
amend = commit --amend
aq = commit --amend --no-edit
@ -27,6 +26,14 @@
fi >/dev/null 2>&1; git rebase --autosquash -i \"${r}\"; }; f"
patch = !git --no-pager diff --no-color
diff = diff-so-fancy | less --tabs=4 -RFXS --pattern '^(Date|added|deleted|modified): '
review = "!f(){ BRANCH=\"$(git rev-parse --abbrev-ref HEAD)\"; git push -u origin \"$BRANCH\":user/$USER/\"$BRANCH\" --force; }; f"
# Branch-agnostic aliases
# https://aj.codes/post/branch-agnostic-git-aliases/
default-branch = "!git symbolic-ref refs/remotes/origin/HEAD | cut -f4 -d/"
pom = push origin $(git default-branch)
merged-branches = "!git branch --merged $(git default-branch)"
sync = "!git fetch -p && git rebase origin/$(git default-branch)"
[push]
default = simple
@ -35,6 +42,7 @@
editor = vim
excludesfile = /home/tgrosinger/.gitignore_global
pager = diff-so-fancy | less --tabs=4 -RFX
attributesfile = /home/tgrosinger/.gitattributes
[diff]
tool = meld

View File

@ -86,6 +86,7 @@ function performSetup() {
echo "Linking Git..."
linkFile ".gitconfig"
linkFile ".gitattributes"
echo "Linking tmux..."
linkFile ".tmux.conf"

View File

@ -11,9 +11,10 @@
# Summary
# --------------------------------------------------------
#
# Performs a safe deletion of a branch by pulling master from origin, then
# rebasing the specified or current branch on master. Once complete, a branch
# deletion is attempted, however aborted if the branch is not fully merged.
# Performs a safe deletion of a branch by pulling the default branch from
# origin, then rebasing the specified or current branch on the default branch.
# Once complete, a branch deletion is attempted, however aborted if the branch
# is not fully merged.
#
# Author: Tony Grosinger
#
@ -21,7 +22,7 @@
# Dependencies
# --------------------------------------------------------
#
# * Git Top (Used to pull from master and rebase)
# * Git Top (Used to pull from the default branch and rebase)
#
# --------------------------------------------------------
# Installation
@ -37,6 +38,10 @@
# fi
# }
#
# Ensure the following is in your .gitconfig alias section:
#
# default-branch = "!git symbolic-ref refs/remotes/origin/HEAD | cut -f4 -d/"
#
VERSION="v0.0.2"
@ -45,6 +50,8 @@ BLUE="\e[34m"
RED="\e[91m"
CLEAR="\e[0m"
DEFAULT_BRANCH=$(git default-branch)
version() {
echo "git safedel ${VERSION}"
echo ""
@ -55,7 +62,7 @@ usage() {
echo ""
echo "If branch name is omitted, current branch will be deleted"
echo "Only safe deletes are attempted. If delete fails, then command"
echo "only acts as a rebase against master."
echo "only acts as a rebase against the default branch."
}
print_cmd() {
@ -119,7 +126,7 @@ require_clean_work_tree() {
delete_branch() {
local startbranch=${1}
exec_cmd "git checkout -q master"
exec_cmd "git checkout -q ${DEFAULT_BRANCH}"
exec_cmd "git branch -d ${startbranch}"
if [ $? != 0 ]; then
@ -156,8 +163,8 @@ main() {
print_status "Attempting to delete branch: ${startbranch}"
if [ ${startbranch} = "master" ]; then
echo "Cannot delete branch \"master\""
if [ ${startbranch} = "${DEFAULT_BRANCH}" ]; then
echo "Cannot delete branch \"${DEFAULT_BRANCH}\""
echo "Please switch branch or provide a branch name as an argument."
exit 1
fi

View File

@ -11,8 +11,8 @@
# Summary
# --------------------------------------------------------
#
# Performs a pull from origin onto master before rebasing the current branch on
# master.
# Performs a pull from origin onto the default branch before rebasing the
# current branch on to the default branch.
#
# Author: Tony Grosinger
#
@ -30,6 +30,10 @@
# fi
# }
#
# Ensure the following is in your .gitconfig alias section:
#
# default-branch = "!git symbolic-ref refs/remotes/origin/HEAD | cut -f4 -d/"
#
VERSION="v0.0.2"
@ -38,6 +42,8 @@ BLUE="\e[34m"
RED="\e[91m"
CLEAR="\e[0m"
DEFAULT_BRANCH=$(git default-branch)
version() {
echo "git top ${VERSION}"
echo ""
@ -113,21 +119,21 @@ checkout() {
}
pull_remote() {
checkout "master"
checkout "${DEFAULT_BRANCH}"
exec_cmd "git pull -r -q"
if [ $? != 0 ]; then
echo >&2 "Could not pull on master, please check that upstream is set and try again."
echo >&2 "Could not pull on ${DEFAULT_BRANCH}, please check that upstream is set and try again."
exit 1
fi
}
rebase() {
local startbranch=${1}
exec_cmd "git rebase -q master ${startbranch}"
exec_cmd "git rebase -q ${DEFAULT_BRANCH} ${startbranch}"
if [ $? != 0 ]; then
echo >&2 "Could not rebase ${startbranch} on master, rolling back changes."
echo >&2 "Could not rebase ${startbranch} on ${DEFAULT_BRANCH}, rolling back changes."
echo >&2 "Please manually resolve conflicts."
git rebase --abort
@ -168,8 +174,10 @@ main() {
require_clean_work_tree
if [ ${startbranch} = "master" ]; then
print_status "Already on master, only performing git pull"
git fetch -p
if [ ${startbranch} = "${DEFAULT_BRANCH}" ]; then
print_status "Already on ${DEFAULT_BRANCH}, only performing git pull"
pull_remote
else
pull_remote