loading...
Cover image for Resolving tricky situations with git reset and rebase 🧶

Resolving tricky situations with git reset and rebase 🧶

aleccool213 profile image Alec Brunelle Originally published at Medium ・Updated on ・3 min read

Want more great content like this? Sign up for my newsletter, visit: alec.coffee/signup

You should care about how clean your Git history is. It provides a way to communicate changes to a codebase with other developers who you are working with, but doing so requires some thought and intention. Ever seen one as clean as this?

A clean Git history

Probably not. Why? Developers are often too lazy (for good reason) and throw Git organization alongside nice-to-haves and TODO in terms of priorities. With the power of git reset and git rebase, I’m going to show you that having a clean and easy-to-read history is fast and easy!

Let's start by introducing you to some Git tricks which you can easily integrate into your workflow. These tips should help you understand Git not only when using the CLI but also GUI clients. These topics I never saw in Git tutorials and have learned myself or from 🍻 with coworkers.

The WIP workflow

Often you will find yourself in this situation:

You just started working on a new branch, refactored a piece of code, installed a package, and wrote half of the necessary code for the feature.

6PM rolls around and you commit everything to save your progress.

When you come back to the project, you realize, “Oh no! Putting this into separate commits will take so much time!”. Okay, so you may not be thinking that, but eh, your probably lazy like me.

 git reset HEAD~1 

git reset is your friend here. This will reset your current HEAD to the commit before your wip commit, but your working tree (all of your file changes) will be preserved to continue editing.

This is one of the many capabilities of this great command. I would recommend you research this more.

Awesome! Now we can start committing things individually to make some nice commits!

Our commits are now making more sense! Let's throw a 🔧 into things.

Rewriting history... the right way

You've worked on a branch for a while, all of your commits are nice and organized, but then someone comments on an issue with the code that is back two commits 😱

You made a small error in your file and need to make a quick change.

opening-a-file-for-a-change

This change is technically related to an old commit, so how do we go back in time and insert it there?

 git stash; git rebase -i HEAD~insert_commit_history_depth 

This stashes our one-line change for use later and brings us to the interactive rebase screen. Let's go ahead and edit that commit we want to bring the change into.

Now that we are editing the old commit, we need to bring our change back.

 git stash pop; git add .; git rebase --continue 

This will bring our change we stashed earlier into the git index, add it the commit we want to edit and then continue on.

And that's it! One thing to get even faster at doing this is aliasing these string of commands into shorthands.

Shoutout to some kick-arse tools

S/O to two Git tools which recently made the cut into my workflow for being so functional and beautiful:

  • Gitup is a nice and minimal tool which doesn’t try and do too many things. It's branch view, stash viewer and integrations are fantastic. Great open-source companion to a Git CLI user.
  • gitmoji-cli makes it very easy to organize your commits visually. Not only does it make your commits organized by forcing you to categorize them, but EMOJI! 💸

Like this post? Consider buying me a coffee to support me writing more.

Want to receive quarterly emails with new posts? Signup for my newsletter

Discussion

pic
Editor guide
Collapse
tonymet profile image
Tony Metzidis

great article

in case your changes are patches and not files, another good trick is

git add -p

A good example would be if you added some experimental methods in 3 different files, and you want to control which commits those changes get applied to.

Finally, it's important to remember your reflog

git reflog |head
471af9a HEAD@{0}: commit (initial): init

that keeps tracks of the history of all refs (e.g. "master" and all branches are refs). That way you can easily reset to a ref from a few days ago in case you forget which commit was kosher.

Collapse
aleccool213 profile image
Alec Brunelle Author

Another two fantastic under-rated commands.

Collapse
lukejs profile image
Luke

This is exactly what I have needed on several occasions, thanks for sharing! Also the clips were very handy in seeing what's going on - I just wish I could start them when I'm ready and play/pause/rewind.

What's doing the autocomplete preview? I need that in my life. 😍

Collapse
aleccool213 profile image
Alec Brunelle Author

Thanks for the feedback! I am super happy at least one person found this handy ❤️

I use the Fish shell which has context-aware auto-complete based on your historical data 🐟

Collapse
jessekphillips profile image
Collapse
fpolster profile image
Florian Polster

Yeah, OP, checkout --fixup. It does what you describe in this post more conveniently

Collapse
aleccool213 profile image
Alec Brunelle Author

I'll check this out!

Collapse
gustavomtborges profile image
Gustavo Martins T Borges

Nice, thanks for sharing. This rebase technique I have never seen before.