Skip to content

Instantly share code, notes, and snippets.

@gyandeeps
Last active November 2, 2023 18:30
Show Gist options
  • Save gyandeeps/c7872187585de4fa7153 to your computer and use it in GitHub Desktop.
Save gyandeeps/c7872187585de4fa7153 to your computer and use it in GitHub Desktop.
Bash alias for git
# Global variables
re='^[0-9]+$'
jira_name="MINT"
# Will create a new branch with name ($1) from master
# it will also make sure master is up to date from origin
workstartFunc() {
if ! [[ $1 =~ $re ]]
then
val=$1
else
val="$jira_name-$1"
fi
git checkout $(git_main_branch)
pullFunc
git checkout -b $val
}
# Will create remove the branch with name ($1) from local and origin
# before it removes, it will make sure master is up to date from origin
workdoneFunc() {
if ! [[ $1 =~ $re ]]
then
val=$1
else
val="$jira_name-$1"
fi
git checkout $(git_main_branch)
pullFunc
git branch -D $val
git push origin :$val
}
# Updates your current branch with origin/master even if you have uncommited changes
# D stands for dirty sio it will use stash to temporarly store your changes,
# sync with origin/master and then put your changes back
updateDFunc() {
git stash
updateFunc
git stash pop
}
# Updates your current branch with origin/master even, make you dont have uncommited changes
# sync with origin/master and then put your changes back
updateFunc() {
if currentBranch=$(git_current_branch)
then
git checkout $(git_main_branch)
pullFunc
git checkout $currentBranch
git rebase $(git_main_branch)
else
echo not on any branch
fi
}
# Rebase your current branch for last n($1) commits
# Its going to be interactive
rebaseFunc() {
if [ -z $1 ]
then val=2
else
val=$1
fi
git rebase -i HEAD~$val
}
# Force push current branch to origin
# It will not force push the master branch
fpushFunc() {
local branch=$(git_main_branch)
local branch_to_push=$1
if [ -z $branch_to_push ]
then branch_to_push=$(git_current_branch)
fi
if [ "$branch_to_push" = "$branch" ]
then echo "Cant force push to $branch"
else
git push --force-with-lease origin $branch_to_push
fi
}
# Regular push current branch to origin
# It will not push the master branch
pushFunc() {
local branch_to_push=$1
if [ -z $branch_to_push ]
then branch_to_push=$(git_current_branch)
fi
git push origin $1
}
# Log of commit messages in one line
logFunc() {
git log --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit --date=short
}
# Pulls the changes for current branch from origin and merges them.
pullFunc() {
if currentBranch1=$(git_current_branch)
then
git pull origin $currentBranch1 --rebase --prune
else
echo not on any branch
fi
}
# Commit the files (on current branch) with the given message
# It will incluide all the files that have been modified and deleted.
# For new files you have to manually stage them using 'git add .'
commitFunc() {
local message=$@
if [ -z "${message// }" ]
then echo "Commit message missing"
else
git commit -S -am "$message"
fi
}
# Updates your fork from upstream master and pushes the updates to your origin fork
updateForkFunc() {
git checkout $(git_main_branch)
git fetch upstream $(git_main_branch)
git merge upstream/$(git_main_branch)
pushFunc $(git_main_branch)
}
# exposed as alias on bash
alias ws=workstartFunc
alias wd=workdoneFunc
alias rebase=rebaseFunc
alias fpush=fpushFunc
alias push=pushFunc
alias update=updateFunc
alias updateD=updateDFunc
alias log=logFunc
alias pull=pullFunc
alias s=gst
alias c=commitFunc
alias uf=updateForkFunc
@silentsudo
Copy link

Nicelydone gyandeep

@lucascaton
Copy link

lucascaton commented Oct 20, 2017

@gyandeeps nice one :)

I use this one daily: .zshrc#L231-L242

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment