How To Create A GIT Branch With Your Current Changes

This article shows you how you can create a GIT branch with your current changes in it. Being a developer with 16+ years of experience, I know for sure this is something you will use on a regular basis. 

Read on if you want to find out the simple & useful solution on how to move your pending code changes into a new GIT code branch.

Example Scenario

So, you started working on your branch and made some changes. You feel like you’re the master of your code, implementing great stuff.

But then, you notice somethings scary….

  • you’re working on the master branch, and forgot to create a new one
  • the little change you were going to make and commit on the develop branche appears to take you some more work, and it is best to put this in a separate branch so you can work on it like the coding master that you are
  • an unmentioned scenario, which still makes you desire to get your uncommitted changes into a new branch.

Moving Your Pending Changes To A New Branch

So, let’s get to business and talk about how you can create a new branch and take all pending code changes along into a new branch.

❌ There is no need to stash your code, create a new branch, and apply a stash (removing the stash afterwards, etc..). I mean, it IS possible but it is oh so tedious.

✅ Luckily for us, you can “move” your uncommitted code changes into a new branch with a single command:

[crayon-65f97941ac71a395208700/]

According to the GIT-scm documentation, specifying -b causes a new branch to be created as if git-branch were called and then checked out.

*POW* Just. Like. That.

Some Sidenotes

You need to keep in mind the following when using this method:

  • If -B is given, <new_branch> is created if it doesn’t exist; otherwise, it is reset.
  • the branch you specified is not reset/created unless “git checkout” is successful
  • If you try to push the newly created branch, you will get the following message:
[crayon-65f97941ac731186176327/] [crayon-65f97941ac733947603217/]

Just do as suggested to create the branch remotely, and you’ll be on track again:

git push --set-upstream origin feature/feature/NEWBRANCH

How To Do This in SourceTree?

As I indicated in a previous article, I’m a fan of Atlassian’s SourceTree visual GIT client. So, for completeness sake, I’ll show you the steps you need to take in SourceTree below:

  • Right-click the previous commit in the Log/history
  • pick “Branch…” from the menu
  • enter a branch name
  • click “Create Branch”
Select the previous commit, and create a new branch…

After you’ve created the new branch, your unstaged uncommitted work will be waiting for you while you have checked out the new branch.

From this point on, you can work as usual and stage & commit your changes.

Concluding

In this post, you’ve seen how to create a git branch with your current changes preserved and tagging along.

I hope this post helped you to get your changes back on track, and remember:

Code Hard, Ship Harder ?

This post was inspired by this SourceTree answer that I wrote back in 2011.

This post is also published on Medium