As developers, we all rely on Git in our daily tasks. However, many of us only use basic commands of Git like committing, pushing, or pulling! But Git has evolved significantly over the years, introducing powerful new features that can really simplify your complex workflow. They can make our life a whole lot easier and help us work more efficiently. Ready to level up your Git game? Let’s go! 🚀
1. ORIG_HEAD , Your Lifesaver
Have you ever found yourself in a frustrating situation where, after rebasing your branch with another one and battling through a mountain of conflicts, you end up with a messy codebase that doesn’t resemble either the base or the current branch? And to top it off, it doesn’t even work the way it’s supposed to? Trust me, I’ve been there too!
That’s exactly why I decided to dig deep and find a little trick to help us get back to where we were before that chaotic rebase.
Here’s the magic command:
git reset --hard ORIG_HEAD
In fact, ORIG_HEAD
is like a safety net in Git. Whenever you perform certain actions, like a rebase, reset or a merge, Git records the position of the HEAD before those operations. ORIG_HEAD points to the commit that was your HEAD (the current commit you’re working on) before the last operation. It’s a handy little reference that can save your day when things go sideways!
So , when you run git reset --hard ORIG_HEAD
, you’re telling Git to reset your current branch. This means you’re moving your branch pointer back to the commit that ORIG_HEAD is pointing to. In simpler terms, you’re undoing the rebase and going back to the state you were in before all the conflict resolution and messy code. The –hard option means that any changes in your working directory and staging area will be lost. So, make sure you really want to go back to that previous state because this command is a bit like hitting the reset button!
In short, this command is a lifesaver when you find yourself in a coding pickle after a rebase. Just remember to use it wisely, and you’ll be back on track in no time.
2. worktree, A Productivity Booster
Many times we had to work on multiple branches in a single repository at the same time. For example, we’re deep into a feature request when a critical bug pops up that needs our immediate attention. It can be a bit of a juggling act, right?
In these situations, we usually have two options: either clone multiple versions or branches of the repository, or stash/discard away what we were working on. But don’t worry, there’s a better way to handle this. Here’s a quick example to show you how it works:
git branch
# * dev
# master
git worktree list
# /.../some-repo ews5ger [dev]
git worktree add -b hotfix ./hotfix master
# Preparing worktree (new branch 'hotfix')
# HEAD is now at 5ea9faa Signed commit.
git worktree list
# /.../test-repo ews5ger [dev]
# /.../test-repo/hotfix 5ea9faa [hotfix]
cd hotfix/ # Clean worktree, where you can make your changes and push them
With this command, you can have multiple branches of the same repository open at the same time! In the example above, we have two branches: dev and master. Let’s say you’re busy working on a feature in the dev branch, but then you get a request to fix a bug urgently. Instead of stashing your changes and resetting your branch, you can simply create a new worktree in the ./hotfix subdirectory based on the master branch.
Once you’re in that directory, you can make your changes, push them, and then head back to your original worktree without missing a beat! You can check out this article for a more detailed explanation.
3. switch, Instead Of checkout
Since 2019, specifically with the release of Git version 2.23, we now have a handy command called git switch
that makes switching branches super easy! To switch to another branch, just type:
git switch other-branch
If you want to hop back to your previous branch, you can simply use:
git switch -
It’s kind of like using cd – to go back to your last directory!
And if you want to jump straight to a remote branch and start tracking it, you can do:
git switch remote-branch
Pretty cool, right? But you might be wondering, “Why do we need this new command when we’ve been using git checkout
to switch branches all along?” Good question!
While git checkout
is a super versatile command that can do a lot of things—like checking out specific files or even restoring specific commits—git switch is focused solely on switching branches. Plus, git switch
includes some extra safety checks that checkout doesn’t have. For instance, if switching branches would cause you to lose any local changes, git switch will stop you in your tracks, which is pretty handy!
So, if you’re looking for a simpler way to switch branches, give it a try!
Happy coding! 🚀
The stash command is also very useful to switch between branches without committing the changes.