Just about everyday, I’m working on and committing code to a repository using Git. This is pretty standard for developers today, and just like any developer, I decided that writing out git status
, git add -p
, git commit -m...
, etc., was just too much work ๐.
This lead me to spend several hours putting together some quick Bash scripts (“scripts” sounds waaaaaay cooler than what I actually did). Here are the replacements I’ve made, and if you want to, you can add these to your .bash_aliases
, and make sure that’s included in your .bash_profile
, included in your PATH
, etc. You can see the full gist of the alias commands below.
The Basics
These are the simple starters, commands I use all the time:
-
alias gits
: is just shorthand that for getting the status of the branch. -
alias gita
: is also shorthand for staging changes in the current branch. -
alias gitap
: just adds the-p
flag to theadd
command so I can step through changes one at time. I try to use this one by default, as I’ll catch things I didn’t mean to change. -
alias gitco
: is shorthand to checkout a branch. -
alias gitcob
: allows me to check out a specified branch, e.g.$ gitcob feature/cool-thing
.
Pretty standard, right?
The Fun Stuff…
Ok, here are some more, and where this whole project started to get fun:
-
alias gitcop
: is a quick command to check out the previously checkout branch. As I’m usually switching back and forth between a feature branch and adevelop
this saves keystrokes and cognitive load. I don’t have to think “Which branch am I switching to? What’s the name again?” It just gos to the previous branch. -
alias gitmprev
: is the same idea as the above. When merging a branch, you are typically checking out a branch, and merging the branch you were on previously into the current branch. Again, no cognitive load, just merging the previous branch. -
alias gitprettylog
: this just gives more detail to thegit log
command, in a format I like. The main thing here is I don’t have to remember anything that comes aftergit log
in this command. -
alias gitnah
: I’m pretty sure this is from Jeffrey Way over at Laracasts. Just and easy way to clear out what I’ve been working on if I don’t want to keep it.
The Really Fun Stuff
These commands are the ones I enjoy the most:
-
alias gitdep
: this combines the branch I’m usually deploying to, checks that out, merges the branch I was working on, pushes that deploy branch to our remote repo, and then checks out the previous branch. This six letter command is probably my favorite as both saving keystrokes and getting me back to the branch I was working on. I can’t tell you how many times I’ve forgotten to switch off a non-working branch… -
gitc
: this is a function that replaces the standard commit plus message flag. I can just typegitc "this is what I did..."
and I’m good to go. -
gitcc
: like the previous command, this takes in a message, but then formats it to our commit standards, again, so I don’t have to think about it. -
gitpo
: pushes the current branch to theorigin
… so much nicer than typing out lots of words.
The Full Gist
Here’s the full gist if you want to see what it looks like:
#############################
# GIT STUFFS
#############################
alias gits="git status"
alias gita="git add ."
alias gitap="git add -p"
alias gitco="git checkout"
alias gitcop="git checkout @{-1}"
alias gitcob="git checkout -b"
alias gitmprev="git merge @{-1}"
alias gitprettylog="git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
alias gitnah="git reset --hard"
alias gitdep="git checkout develop; gitmprev; gitpo; gitcop;"
alias gitresetdev="git checkout develop; git fetch && git reset --hard origin/develop; gitcop;"
# This will check out a merge request from the upstream repo and create a new branch with the same name as the merge request, syntax is `gitcomr 1234 branch-name`.
gitcomr () {
git fetch upstream merge-requests/$1/head:$2
git checkout $2
}
# This simply commits with a message, syntax is `gitc "message"`
gitc () {
git commit -m"$1"
}
# This will commit with a message that includes the branch name, syntax is `gitcc "message"`
gitcc () {
BRANCH=$(git branch --show-current)
# Which follows this syntax: string/pattern/replacement/global
# 's/\//(/g' looks for all occurances of `/` and replaces with (
# 's/-ABC-[0-9]\{4,\}/): $1/g' looks for all occurances of `-ABC-` appended with 4 or more digits and replace them with `: string-passed-in`
echo $BRANCH | sed -e 's/\//(/g' -e "s/-ABC-[0-9]\{4,\}/): $1/g"
MESSAGE=`echo $BRANCH | sed -e 's/\//(/g' -e "s/-ABC-[0-9]\{4,\}/): $1/g"`
git commit -m"$MESSAGE"
}
# This will push the current branch to origin, syntax is `gitpo`
gitpo () {
BRANCH=$(git branch --show-current)
git push origin $BRANCH
}
And that’s it! Nothing super fancy, just more “quality of life” improvements. While figuring some of these out took a couple hours and lots of testing, I’m glad I’ve got a Git workflow down that works for me and is easy to use.
Other Random Commands
Here’s a few other random commands I like using:
# Delete any branches that start with "feature/"
git branch | grep '^ feature/' | xargs git branch -d
# Fetch a merge request on an upstream in a fork-based dev env:
# Run:
git fetch upstream merge-requests/9/head:white-bar-variation
# You should see:From git.name.org:name-of-repo/name-of-branch
* [new ref] refs/merge-requests/9/head -> white-bar-variation
* [new ref] refs/merge-requests/9/head -> upstream/merge-requests/9
# Then run:
git checkout white-bar-variation
Have fun!