DEV Community

Cover image for 4 Git shortcuts that define my workflow
Ben Holmes
Ben Holmes

Posted on • Updated on • Originally published at bholmes.dev

4 Git shortcuts that define my workflow

I started my first full-time job as a programmer this year, which means I've been picking up a lot of new tricks in a pretty quick timeframe. Naturally, I started automating the workflows I use day-in and day-out ๐Ÿ˜

Stop me if you've seen this workflow before:

  1. I picked up a ticket on JIRA
  2. I need to pull the latest main branch
  3. I need to checkout a new branch
  4. I need to push that branch to origin before opening my PR

I'll probably do this 5+ times in one day if we're in a bug-squashing flow. But when I'm hurrying, it's so easy to either a) work off an old "main" branch, or b) do the copy-paste of shame before your PR:

To push the current branch and set the remote as upstream, use

    git push --set-upstream origin crap-not-again
Enter fullscreen mode Exit fullscreen mode

You know you cringe a little every time this pops up ๐Ÿ˜ฌ

So let's discuss:

๐Ÿพ The importance of pushing and popping your stash

๐Ÿ™ˆ The joys of upstream HEAD

๐Ÿงถ Writing aliases for your terminal to tie it all together

Onwards!

The fantastic 4 aliases

Alright, let's do the big reveal. Here's my 4 shortcuts for slamming through daily tasks ๐Ÿ’ช

# Stash what I'm working on and checkout the latest master
alias gimme="git stash push -u && git checkout main && git pull -r"

# Grab the latest master and come back to the branch I was on (with stashing!)
alias yoink="gimme && git checkout - && git stash pop"

# Checkout a new branch and push it to origin (so I don't forget that set-upstream)
woosh() {
ย ย git checkout -b $1 && git push -u origin HEAD
}

# ALL TOGETHER NOW
# Stash my current WIP, checkout a new branch off the latest master, and push it to origin
alias boop="gimme && woosh"
Enter fullscreen mode Exit fullscreen mode

1-fantastic-4-snippets

These commands are written in bash. So if you want to use these in your own terminal, go find your bashrc and paste away! This is usually found in your user directory: ~/.bashrc. And for all you windows users out there: you'll probably need to install the Git Bash terminal (included with Git) and create the file like so.

โœ‹ But wait! Before you blindly copy these, let's unpack each of them a little.

gimme โœŠ

alias gimme="git stash push -u && git checkout main && git pull -r"
Enter fullscreen mode Exit fullscreen mode

This is my "return to home base" command. It assumes you might not be on the main branch just yet, so it'll stash everything you're working on using the stash command. It also saves "untracked" / new files with the -u flag.

Next, it checks out your main branch and pulls the latest contents. That -r flag will be sure to "rebase" onto the latest, preventing unecessary merge conflicts.

This is the simplest command of the bunch, but it's also my most-used! On my team, we run a lot of automated scripts on pull for installing dependencies and pulling content from our CMS. So, it's super useful to return to main and grab all this new content in one go.

yoink ๐ŸŽฃ

alias yoink="gimme && git checkout - && git stash pop"
Enter fullscreen mode Exit fullscreen mode

This phrase is certainly a close relative to gimme. For some etymology, I'll include the urban dictionary entry on what it means to yoink:

An exclamation that, when uttered in conjunction with taking an object, immediately transfers ownership from the original owner to the person using the word regardless of previous property rights.

...In other words, we are grabbing the main branch and bringing it back to the branch we're on ๐Ÿ˜ We pull this off using the git checkout - command after running gimme. This is super convenient for grabbing the latest changes to force rebase the branch we're working on.

๐Ÿ’ก Note: is easy to achieve with a fetch command as well:

git fetch origin master:master
Enter fullscreen mode Exit fullscreen mode

...but if your team runs automated scripts whenever you pull, you'll probably want the original command I described.

Clarification on using stash vs stash push

You also may be new to using push and pop on your stash, instead of apply and clear (as I often used to do). Here's the distinction:

  • Whenever you run "vanilla" git stash, you're merging your current changes with all the other changes in the stash. Think of this like throwing all your belongings in a bag, and git stash apply is like dumping everything out onto the floor.
  • Whenever you run git stash push, you're putting your current changes in a separate compartment from all the other changes in the stash. Think of this like choosing a different bag pocket every time you go to "stash" something. Then, when you pop, you pull from the last pocket you just used.

This is the reason we use push inside the gimme command, and pop inside the yoink command. It guarantees that we grab the same thing we just stashed, without getting mixed up with anything already in your stash. If you've ever dug around your bag trying to find one tiny thing stuck at the bottom, you know what I'm talking about here ๐Ÿ˜

woosh ๐Ÿ’จ

woosh() {
ย ย git checkout -b $1 && git push -u origin HEAD
}
Enter fullscreen mode Exit fullscreen mode

This is the solution to your --set-upstream woes. Instead of pushing to origin later, this lets you check out a new branch and push right away so you don't forget.

Yes, there's some cases when you don't want your local branch on the remote just yet, but this is pretty rare in my experience. I often create a branch corresponding to a given task on our Kanban board that I'm ready to push up right away.

And if you've never seen the HEAD parameter before... remember that one! It's a super slick way to auto-fill the name of your current branch instead of typing it out by hand ๐Ÿ”ฅ

boop ๐Ÿ‘‡

alias boop="gimme && woosh"
Enter fullscreen mode Exit fullscreen mode

Shout out to "lord of boops" Jason Lengstorf for the inspiration

This command goes full-circle. If you're somehow too lazy to check out the latest main branch before starting anew, this will let you gimme and woosh all in one go!

Not much to explain with this one. Just go forth into perfect boop-dom.

Learn a little something?

Awesome. In case you missed it, I launched an my "web wizardry" newsletter to explore more knowledge nuggets like this!

This thing tackles the "first principles" of web development. In other words, what are all the janky browser APIs, bent CSS rules, and semi-accessible HTML that make all our web projects tick? If you're looking to go beyond the framework, this one's for you dear web sorcerer ๐Ÿ”ฎ

Subscribe away right here. I promise to always teach and never spam โค๏ธ

Top comments (5)

Collapse
 
tnolte profile image
Tim Nolte • Edited

I setup a a bunch of aliases for my git workflow as week, but actually have them as aliases for git specifically, in my .gitconfig file. And example for me when creating a new feature branch would be to call git feature <Jira#-Decription>, which uses my git alias:

feature = "!fb() { git co-m; git pull; git co-b feature/$1; }; fb"
Enter fullscreen mode Exit fullscreen mode

Which in turn, like you, uses other aliases I've defined in git. Like git co-m is an alias I setup that checks out the main branch.

I'm going to need to go through yours again and see where I can maybe make some improvements to my own. Thanks for sharing!

Collapse
 
sirclesam profile image
Sirclesam

You win just for the cmd names.

I get paranoid about losing my stash, especially if I get pulled into somewhere else in the middle, so only use it when I'm going to apply it right away. For anything longer than a few min I've gotten comfortable with committing with an awful commit message (REVERT ME!!!! RABBLE RABBLE) then doing a soft reset to the prev commit when I need to resume.

It would be helpful if you repeated the command under where you're describing it. I found myself scrolling back up to see what you were talking about.

Collapse
 
bholmesdev profile image
Ben Holmes

Good callout on repeating the commands! Just added that edit ๐Ÿ‘

I also 100% agree on the stash itch. These days, I almost feel physically itchy when I'm hanging onto a stash or some important code in my clipboard. It's like I need to get it out of my head and into the computer again ๐Ÿ˜†

Collapse
 
sirclesam profile image
Sirclesam

I should note that I use a GIT GUI (smartgit) so my prev commit message are easily seen and I won't leave commits like the one above in any pushed code.

Collapse
 
stevewallone profile image
Steve Wall

Every time I do the copy-paste of shame I tell myself I'm going write an alias for that. It feels good to get that monkey off my back! Thanks for sharing Ben!