
Why I Bash Git (And Why You Should Too)
A lot of people these days use tools likeoh-my-zsh that come packed with a ton of helpful features out of the box, including Git shortcuts. And don’t get me wrong—they’re great. But I think it’s really important to understand how things work under the hood. You can slap on all the tools you want, but there’s real value in building your own workflow from the ground up.
If you’re curious about my take onwhy you should write your own tools, you can check out my thoughtshere. But for now, I want to show you howBash functions andaliases can make Git workflows faster, easier, and just plain better. I hope this post gets you excited to dig into your shell’s rc file and start writing your own custom functions and aliases, not just for Git, but for everything you do!
1.Git Aliases
First up, let’s simplify some of those common Git commands. Here are some aliases I’ve set up to make life a little easier in the terminal. Why type a long command every time when you can shorten it to two letters?
aliasgs="git status"# Show Git statusaliasga="git add ."# Add all files to the staging areaaliasgc="git commit -m"# Commit with a messagealiasgp="git push"# Push the current branch to the remotealiasgl="git pull"# Pull from the remote branchaliasglog="git log --oneline --graph --all --decorate"# View Git log in one-line formataliasgco="git checkout"# Checkout a branchaliasgcb="git checkout -b"# Create and switch to a new branchaliasgd="git diff --cached"# View the difference of staged changesaliasgrh="git reset --hard HEAD"# Hard reset to the latest commitaliasgb="git branch -vv"# Show branches and last commit in one-line formataliasgf="git fetch --all"# Fetch all remote branches
These aliases shave off seconds, but those seconds add up. Plus, they just feel good to use.
2.Bash Functions for More Complex Git Workflows
Now, let’s kick it up a notch with some customBash functions that automate a bit more of your workflow. Functions like these can save you from typing out multiple commands and ensure you don’t miss any steps.
2.1.Create a New Branch and Push It
gnew(){ git checkout-b"$1" git push-u origin"$1"}# Usage: gnew branch_name
2.2.Quick Commit and Push
gquick(){ got add. git commit-m"$1" git push}# Usage: gquick "commit message"
2.3.Rebase Current Branch onto Main
grebase(){ git fetch git rebase origin/main}# Usage: grebase
2.4.Undo the Last Commit
gundo(){ git reset--soft HEAD~1}# Usage: gundo
2.5.Squash Commits
gsquash(){ git reset--soft HEAD~"$1" git commit--amend}# Usage: gsquash 3 (to squash the last 3 commits)
2.6.Sync Fork with Upstream
gupdate-fork(){ git fetch upstream git checkout main git merge upstream/main git push origin main}# Usage: gupdate-fork
2.7.Interactive Rebase on Previous Commits
grebasei(){ git rebase-i HEAD~"$1"}# Usage: grebasei 3 (to interactively rebase the last 3 commits)
3.General Workflow Enhancers
These final functions enhance general Git workflows to make things even more efficient.
3.1.Show Git Tree
glogtree(){ git log--graph--oneline--decorate--all}# Usage: glogtree
3.2.Reset Branch to Remote
gresetremote(){ git fetch origin git reset--hard origin/"$(git rev-parse--abbrev-ref HEAD)"}# Usage: gresetremote
4.Add Aliases and Functions to Your.bashrc
or.zshrc
If you want these functions and aliases to persist across terminal sessions, you’ll need to add them to your.bashrc
or.zshrc
. Here’s how:
Open your shell configuration file:
nvim ~/.bashrc# OR ~/.zshrc
Paste the aliases and functions into the file.
After saving, refresh your shell:
source ~/.bashrc# OR ~/.zshrc
These are just some of the ways you can make Git work for you, rather than the other way around. By taking a few minutes to tweak your shell setup, you can save hours of typing and clicking over time.So what about you?
Top comments(54)

- LocationRacour, Belgium
- Joined
For you gnew function : by adding the code below in your~/.gitconfig
, the branch will be automatically created in your repo with the firstgit push
[push] autoSetupRemote = true
Using that config, your gnew function is perhaps useless.

- Email
- LocationKyle, Texas
- EducationLambda School
- WorkSenior Android Developer
- Joined
Woa! I've actually never edited my git config like that. Didn't even know it! Will have to check out the configuration options! Very cool.

- LocationRacour, Belgium
- Joined
A few days ago I've published an article about tips & tricks about.gitconfig
:avonture.be/blog/git-config

- LocationRacour, Belgium
- Joined
(Oh? Thanks for the intention anyway ;-))

- LocationVienna, Austria
- WorkMoonshiner
- Joined
Those are some amazing aliases, simple and efficient. Love it. Added them to my.zshrc
already :D
I generally feel like a lot of developers don't know how much easier they can make their lives with a few bash aliases/functions. Here is one of my new all-time favorites which I added today. It recursively deletes a directory with a specific name in your current working directory (use with caution!). I use it to delete all node_modules in a monorepo.
rmrfd(){ find.-name"$1"-type d-execrm-rf{} +}# Usage: rmrfd node_modules

- Email
- LocationKyle, Texas
- EducationLambda School
- WorkSenior Android Developer
- Joined
Very cool! Now this is what I'm talking about. 😎

Or use a GUI tool like sourcetree or fork. Really let's you achieve a lot very quickly and in short time. And also, discover many features and abilities of git by exploring the GUI tool - which otherwise we probably would've never actively enquired about.
Of course, all of this recommended after you have mastered basic Git CLI and know how git works under the hood.

- Email
- LocationKyle, Texas
- EducationLambda School
- WorkSenior Android Developer
- Joined
I have a discussion going around this very topicright here! :)

- LocationYardley, PA
- EducationN/A
- Pronounshe/him
- WorkPrincipal Architect at Arc XP
- Joined
I won’t use any of these samples (git commands are just baked into my brain at this point) but I love that you’re thinking of ways to improve your workflow, and that you’re using your shell to do it.

- LocationDhaka, Bangladesh
- EducationB.Sc.
- PronounsHe/Him
- WorkWeb Developer
- Joined
I will agree with@manchicken too. Over the years git commands kinda got baked into my brain as well. But its nice to see some amazing options. Thanks for sharing.

- Email
- LocationKyle, Texas
- EducationLambda School
- WorkSenior Android Developer
- Joined
The purpose isn't to exactly get people to follow what I do. But rather show people how I solve common problems using the shell. There's so much power at your fingertips. Whatever I can do to inspire people to become less afraid of the beautiful terminal :)

- LocationYardley, PA
- EducationN/A
- Pronounshe/him
- WorkPrincipal Architect at Arc XP
- Joined
Strong agree. Thanks for spreading the$TERM
love.

- LocationUSA
- EducationRidgewater College
- Pronounsawe/some
- WorkLead Software Engineer @ProAg
- Joined
Agreed! Especially now with AI, it is super easy to create new aliases and scripts.
Here is myGit config with aliases etc.
Also, I useBetter Branch for pretty branch info

- Pronounsshe/her;q=1, they/them;q=0.8, */*;q=0.2
- Joined
main = !git checkout main
This could just be
main = checkout main
to make your git config a little less cluttered 😉

"2.3. Rebase Current Branch onto Main"
Could be made faster if you just did:git rebase origin/main
. No need to switch branches at all

- LocationMaine
- Pronounshe/him
- WorkUI Architect at Bottomline Technologies
- Joined
That wouldn't guarantee you have the latest commits from origin. Git is "lazy" about fetching updates. If you don't specifically checkout and pull, you would rebase to the last state you leftmain
.

It doesn't pull from local. That's the point of using origin/main instead of just main. You might need to git fetch, but you don't need to switch to main and pull. You can rebase from your other branch

- Email
- LocationKyle, Texas
- EducationLambda School
- WorkSenior Android Developer
- Joined
Yeah, so:
git fetchgit rebase origin/main
should be a lot faster. It's been so long since I've wrote some of these aliases and functions. It's a commands shorter, and it would be faster technically. Tested it out this morning and everything seems like it works like it's supposed to! I appropriate the optimization :) You're correct on that call!

- Email
- LocationKyle, Texas
- EducationLambda School
- WorkSenior Android Developer
- Joined
Oh yeah, that's right! Because it's grabbing from local instead of remote. This is why we test things out before making changes ;)

- Email
- LocationKyle, Texas
- EducationLambda School
- WorkSenior Android Developer
- Joined
Thanks for the advice! I'll test it out and update my script. Good catch! 😎

you know... I can do what you do with Powershell. and is FASTER than git bash.
(git for windows is sort of garbage, but zsh is another realm that is for linux or macOs)
you can do it over here:powershellgallery.com/packages/git...

- Email
- LocationKyle, Texas
- EducationLambda School
- WorkSenior Android Developer
- Joined
I hope people on Windows check this out!

I tell people, "GIT makes hard things possible. It makes easy things possible, too!", LOL. Its the first version control system that requires you to memorize every detail of how its implemented to have a snowball's chance in hell of mastering it. ..

- LocationFeira de Santana, BA, Brasil
- EducationBachelor in Information Systems by UNIFTC
- WorkBackend Developer @ Instituto Oficial de Publicidade
- Joined
Is there a way to "SAVE" those aliases on git bash tho on windows?

- Email
- LocationKyle, Texas
- EducationLambda School
- WorkSenior Android Developer
- Joined
Inside gitbash I think you have a .bashrc in the home directory that you can ad these aliases and functions too :)

- LocationFeira de Santana, BA, Brasil
- EducationBachelor in Information Systems by UNIFTC
- WorkBackend Developer @ Instituto Oficial de Publicidade
- Joined
It did work for me after I've restarted the git bash. Seems there was some files missing so it created those for me: .bash_profile, .lesshst and .viminfo

- Email
- LocationKyle, Texas
- EducationLambda School
- WorkSenior Android Developer
- Joined
you can runsource ~/.bashrc
or whatever file your storing your config in to reload the terminal with the new changes.
I have an alias in my rc filealias reload="source ~/.bashrc && clear"
in mine so that I can easily runreload
after I've made a change :)

- Email
- LocationIstanbul
- EducationSelf taught
- WorkHead of Software Engineering
- Joined
humble tip:
When you constantly use custom bash aliases, you tend to forget the actual commands :) And when you switch to a teammate's computer where those aliases don't exist, you feel completely lost :)
Some comments may only be visible to logged-in visitors.Sign in to view all comments.
For further actions, you may consider blocking this person and/orreporting abuse