DEV Community

Dzhavat Ushev
Dzhavat Ushev

Posted on • Updated on • Originally published at dzhavat.github.io

Getting productive with git checkout

This post was originally posted on my blog.


As a developer, I use git on a daily basis. It has become an essential tool for my work. It’s great and I love it.

I also enjoy interacting with it directly in the Terminal. There’s something magical in writing a command and seeing it do something. I’ve tried using GitHub Desktop for some time but always came back to the Terminal.

The problem

There’s one command, though, that I used to like and hate at the same time. It’s git checkout.

I like it because of its simplicity and effectiveness. With it, I can create a new branch and switch to it all at once. Just like this:

git checkout -b new-feature
Enter fullscreen mode Exit fullscreen mode

Beautiful.

But, in some cases, I hated it too because of the way it works. Using it as above creates a branch that is based off of the one I’m currently on and that isn’t something I always want.

Here’s a use case that I might have during my work.

Say I’m on a new-feature branch and have some changes, not related to the current task, I want to transfer to a new branch. Simply using git checkout -b new-awesome-feature will not help me because all commits made thus far will follow as well. In those scenarios my workflow was:

  1. Stash everything.
  2. Switch to the branch I want to base the new one off.
  3. Use git checkout command to create a new branch and switch to it.
  4. Pop the changes from the stash and continue working with them.

In code that looks like this:

git stash
git checkout master
git checkout -b new-awesome-feature
git stash pop
Enter fullscreen mode Exit fullscreen mode

Definitely not the best workflow.

The solution

About a month ago I decided to see if I could streamline this process because I wasn’t quite happy with its current form. So I opened the git checkout docs and started to dig in. Fortunately, the solution was right there! Among the many options on that page, there’s one called <start_point>. By using it, I can specify a starting point for the new branch. It says that I can use “the name of a commit” but there’s actually more to it. Following the link to git branch reveals that ”[<start-point>] may be given as a branch name, a commit-id, or a tag”. This was exactly what I was looking for!

So now, whenever I want to transfer changes to a new branch that must have a different starting point than the current one, I use:

git checkout -b <branch-name> <start-point>
Enter fullscreen mode Exit fullscreen mode

All four steps from above are down to just one! Pure enjoyment!

Ever since I found this, I’m at peace with the checkout command. I like it even more. Hope you do as well!

Top comments (4)

Collapse
 
oligospermia profile image
Mesalazine

You can also hard reset your new branch in case you forgot to provide starting point:

# when on <master>
git checkout -b new-awesome-feature
# <new-awesome-feature> is now at <master>
git reset --hard old-but-still-awesome-feature
# <new-awesome-feature> will now be at <old-but-still-awesome-feature>
Collapse
 
dzhavat profile image
Dzhavat Ushev

Oh, that‘s awesome! Will definitely use it. Thanks for the tip.

Collapse
 
nervewax profile image
nervewax

Oh wow 🤯 yeah I'm going to use this all the time now, thanks @dzhavat

Collapse
 
alexparra profile image
Alex Parra

Ah ah! Had this exact issue today.
Best!