DEV Community

Cover image for Why I Bash Git (And Why You Should Too)

Why I Bash Git (And Why You Should Too)

Jimmy McBride on September 16, 2024

A lot of people these days use tools like oh-my-zsh that come packed with a ton of helpful features out of the box, including Git shortcuts. And do...
Collapse
 
cavo789 profile image
Christophe Avonture

For you gnew function : by adding the code below in your ~/.gitconfig, the branch will be automatically created in your repo with the first git push

[push]
    autoSetupRemote = true
Enter fullscreen mode Exit fullscreen mode

Using that config, your gnew function is perhaps useless.

Collapse
 
hugomcm profile image
hugomcm

Hello there! Good to know that there is a .gitconfig setting that does that ;).
But in some cases, the function is really usefull, for example if you want to create a remote branch with a different name.

gnew() {
  git checkout -b "$1"
  git push -u origin "$1:ready/$1/$$"
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jimmymcbride profile image
Jimmy McBride

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.

Collapse
 
cavo789 profile image
Christophe Avonture

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

Thread Thread
 
josephj11 profile image
Joe

Your linked post has 0 feedback. I am signed in to GitHub, but it wouldn't let me upvote it or add a comment.

Thread Thread
 
cavo789 profile image
Christophe Avonture

(Oh? Thanks for the intention anyway ;-))

Collapse
 
drazenbebic profile image
Drazen Bebic

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 -exec rm -rf {} +
}
# Usage: rmrfd node_modules
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jimmymcbride profile image
Jimmy McBride

Very cool! Now this is what I'm talking about. 😎

Collapse
 
syedrakib profile image
Rakib Al Hasan

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.

Collapse
 
jimmymcbride profile image
Jimmy McBride

I have a discussion going around this very topic right here! :)

Collapse
 
manchicken profile image
Mike Stemle

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.

Collapse
 
masudalimrancasual profile image
Masud Al Imran

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.

Collapse
 
jimmymcbride profile image
Jimmy McBride

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 :)

Collapse
 
manchicken profile image
Mike Stemle

Strong agree. Thanks for spreading the $TERM love.

Collapse
 
lazyguru profile image
Joe Constant

"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

Collapse
 
oculus42 profile image
Samuel Rouse

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 left main.

Collapse
 
lazyguru profile image
Joe Constant

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

Thread Thread
 
jimmymcbride profile image
Jimmy McBride

Yeah, so:

git fetch
git rebase origin/main
Enter fullscreen mode Exit fullscreen mode

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!

Collapse
 
jimmymcbride profile image
Jimmy McBride

Oh yeah, that's right! Because it's grabbing from local instead of remote. This is why we test things out before making changes ;)

Collapse
 
jimmymcbride profile image
Jimmy McBride

Thanks for the advice! I'll test it out and update my script. Good catch! 😎

Collapse
 
erickrodrcodes profile image
Erick Rodriguez

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...

Collapse
 
jimmymcbride profile image
Jimmy McBride

I hope people on Windows check this out!

Collapse
 
natescode profile image
Nathan Hedglin

Agreed! Especially now with AI, it is super easy to create new aliases and scripts.

Here is my Git config with aliases etc.

Also, I use Better Branch for pretty branch info

Collapse
 
darkwiiplayer profile image
π’ŽWii πŸ³οΈβ€βš§οΈ
main = !git checkout main
Enter fullscreen mode Exit fullscreen mode

This could just be

main = checkout main
Enter fullscreen mode Exit fullscreen mode

to make your git config a little less cluttered πŸ˜‰

Collapse
 
vigo profile image
Uğur "vigo" Γ–zyΔ±lmazel

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 :)

Collapse
 
dwgillies profile image
Donald Gillies

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. ..

Collapse
 
vikasnautiyal profile image
vikas nautiyal

Pretty cool, I like aliases as they come handy, In my bashrc I source an aliases file wherin I define git co for checkout, git st for git status git cp for cherry-pick, ..etc. I plan to build some Complex Git Workflows soon.

Collapse
 
jimmymcbride profile image
Jimmy McBride

Bash functions work perfectly for those more complex flows! :)

Collapse
 
shjordan profile image
Jordan Humberto de Souza

Is there a way to "SAVE" those aliases on git bash tho on windows?

Collapse
 
jimmymcbride profile image
Jimmy McBride

Inside gitbash I think you have a .bashrc in the home directory that you can ad these aliases and functions too :)

Collapse
 
shjordan profile image
Jordan Humberto de Souza

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

Thread Thread
 
jimmymcbride profile image
Jimmy McBride

you can run source ~/.bashrc or whatever file your storing your config in to reload the terminal with the new changes.

I have an alias in my rc file alias reload="source ~/.bashrc && clear" in mine so that I can easily run reload after I've made a change :)

Collapse
 
fritzmark profile image
fritzmark • Edited

For 2.2 just use git commit -am to get rid of git add ..

Collapse
 
jimmymcbride profile image
Jimmy McBride

Changed and updated! Thank you very much :)

Collapse
 
milanfaltus profile image
Milan Faltus

Just so you're aware - there is a difference between git add . and git commit -am. git commit -am will not add new files that git is not yet aware of.

Thread Thread
 
jimmymcbride profile image
Jimmy McBride

oh, that's actually really good to know! I had never heard of the am command before writing this article.

Collapse
 
chrisandrew profile image
Chris

A clear, quick write-up, and some useful comments, too. Thanks to everyone who meaningfully contributed!

Collapse
 
scanepa profile image
Stefano Canepa

I'm following your approach since I moved from oh-my-zsh to fish.

Collapse
 
ananyapaw profile image
Ananya Paw 🐾

By bash you don't mean "bashing" right?

Collapse
 
jimmymcbride profile image
Jimmy McBride

No, it's just a play on words. I use bash to write helpful aliases and functions to help improve my git flows in the terminal

Collapse
 
andigwandi profile image
Sanjeev Kumar

Nicely explained

Collapse
 
jimmymcbride profile image
Jimmy McBride

Thank you very much! :)

Collapse
 
adan_rao_3d31c0f2acd70dc1 profile image
Adan Rao • Edited

impressive code and details side-by-side just like roblox mod menu apk free is very learning way ....!!!

Collapse
 
jimmymcbride profile image
Jimmy McBride

Thank you very much! :)

Collapse
 
rickfactr profile image
Rick Culpepper • Edited

Did you really say that I should build my own tools instead of using someone else's well-crafted solutionl? Oh-my-zsh does all of this and more... who cares if I wrote it myself?

When I need a car, I go buy one... ready to roll. Do you fire up a smelter in your back yard to create some iron and then stainless steel...???

In the end, it is only by standing on this shoulders of those who came before us that we can reach new heights.

Collapse
 
jimmymcbride profile image
Jimmy McBride

I linked to my article about my opinion on building your own tools. I think your arguing against a point I'm not trying to make. I implore you to read it if you'd like a better understanding of my position on this matter. Maybe you could take up this conversation over there! :)

Collapse
 
darkwiiplayer profile image
π’ŽWii πŸ³οΈβ€βš§οΈ

If your own tools can work like 5% better for you than someone else's tools, and it takes you maybe 5 to 10 minutes to write them, then I say over the course of a programming career the effort is going to more than pay for itself.

Collapse
 
darkwiiplayer profile image
π’ŽWii πŸ³οΈβ€βš§οΈ

For aliases with longer names, it's better to just use git whatever instead of gwhatever, both to make it easier to remember, easier to read scripts, and easier to search your *sh history.

If your command is short, you can set up a git alias in your config; prepend them with a ! to run shell commands.

For longer scripts, if you create a script named git-whatever and have it in your path, you can call git whatever and git will call your script for you. You don't save much typing but that way you don't need to remember if some helper was a git alias or a standalone script.

Collapse
 
kgunnit profile image
kgunnit

I recommend Oh-my-zsh and their git plugin. There's a good amount of plugins to choose from to create aliases or functions to assist with your workflow.

Collapse
 
leob profile image
leob • Edited

Yeah I do this too, bash is great, never saw the need for oh-my-zsh ... sometimes "less is more" !

Collapse
 
wormss profile image
WORMSS

Maybe you will be better off with SmartGit.. What you are considering "complex" is literally the bare basics of git..
SmartGit will allow you to do proper things with Git that would be useful

Collapse
 
jimmymcbride profile image
Jimmy McBride

This isn't meant to replace other tools, but from my last blog post, it seems there's a lot people who use git from command line for mostly everything except a few things where they use a so e kind of GUI for diffs and conflicts.

I will check out SmartGit though! I've heard some things about and would like to see what it has to offer :)

Collapse
 
wormss profile image
WORMSS

SmartGit makes working with Submodules "a dream".. It's practically magic.
You have all the command line guru's who scream out their lungs that SubModules are horrible and evil, and should not be used at all costs.. and have invented hundreds of alternatives to submodules.. But it's only horrible to use submodules on the command line.. SmartGit makes them super easy to use.. Especially if you have multiple projects all using the same core code..
Nowadays people would do it with monorepos, but when we used submodules, it was great having versioned "core" code and versioned "projects"..

Thread Thread
 
jimmymcbride profile image
Jimmy McBride

Wow! This def makes it worth checking out. This could be the missing piece to my modular android stack when working on large enterprise projects. Thank you so much! XD