DEV Community

Cover image for My Git Aliases Categorized by Usage
Brian Gaines
Brian Gaines

Posted on

My Git Aliases Categorized by Usage

These are the aliases I use in my .gitconfig file.

Most Used

[alias]

# status of commit
st = status -sb

# add, commit, stage your changes
acm = !git add . && git commit -m

# add, commit, stage, and push changes to origin
acmp = "!f() { git acm \"$@\" && git p; }; f"

# commit with message
cm = commit -m

# get branch
br = branch

# delete a branch only if it has been merged
bd = branch -d 

# force delete a branch
bDf = branch -D 

# checkout a new not yet existing branch
cob = checkout -b

# push changes to a remote
p = push

# fetch from repo and prune any remote-tracking refss that no longer exist on remote
f = fetch -p

Enter fullscreen mode Exit fullscreen mode

Less Commonly Used


# edit this config file
edit-config = config --global -e 

# show aliases
aliases = config --get-regexp alias 

# stash list
sl = stash list

# stash work (along with untracked files) with message
ss = stash save -u

# get a stash (ie. stash@{2})
sp = stash pop

# get most recent stash
sprecent = stash pop stash@{0} 

# logging
plog = log --graph --pretty='format:%C(red)%d%C(reset) %C(yellow)%h%C(reset) %ar %C(green)%aN%C(reset) %s'

Enter fullscreen mode Exit fullscreen mode

Never Really Used, but Exist


# remove all branches merged into the branch I'm on
bdm = "!git branch --merged | grep -v '*' | xargs -n 1 git branch -d" 

# checkout a branch
co = checkout

# push changes to master
pom = push origin master 

# tag a version
t = tag

# list contributors
rank = shortlog -sn --no-merges

# logging
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

tlog = log --stat --since='1 Day Ago' --graph --pretty=oneline --abbrev-commit --date=relative

# View unpushed commits
unpushed = !GIT_CURRENT_BRANCH=$(git name-rev --name-only HEAD) && git log origin/$GIT_CURRENT_BRANCH..$GIT_CURRENT_BRANCH --oneline

# View unmerged branches, except for those beginning with "archive/" and "staging"
unmerged-D = !git branch --no-merged develop | grep -Ev \"(archive/|staging)\"
unmerged-M = !git branch --no-merged master | grep -Ev \"(archive/|staging)\"

# View which branches need to be merged, except for those beginning with "archive/" and "staging"
active-merged-D = !git branch --merged develop | grep -Ev \"(archive/|staging)\"

Enter fullscreen mode Exit fullscreen mode

Comment below with your favorite git aliases!

Thanks for reading, and Happy Coding!
Brian

Top comments (4)

Collapse
 
eckelon profile image
JA Samitier

Great aliases, thanks!!

I'd suggest to replace the add . with add -u in the acm alias, to only add files that were already added before (e.g to avoid adding a big node_modules by accident)

Cheers!

Collapse
 
briang123 profile image
Brian Gaines

Thanks JA, for your feedback!

For me, I have node_modules in my .gitignore file so I won’t need to deal with that. Also, I use this command to initially stage and commit my files.

Collapse
 
esromneb profile image
esromneb • Edited
alias gitfind='git branch -avv | grep `git rev-parse HEAD | cut -c1-7`'
Enter fullscreen mode Exit fullscreen mode

This will tell you if there are any branches that match your current commit hash. This is useful after running

git submodule update --init --recursive
Enter fullscreen mode Exit fullscreen mode

which will move all of your submodules to detached head. This allows you to checkout a branch with the same hash as the detached head.

Collapse
 
briang123 profile image
Brian Gaines

Thank you! I’ll need to look more into that! I appreciate it!