A GIT how-to for beginning and advanced developers
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.
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:
$ git checkout -b topic/newbranch
You need to keep in mind the following when using this method:
<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:
fatal: The current branch feature/NEWBRANCH has no upstream branch. To push the current branch and set the remote as upstream, usegit push --set-upstream origin feature/feature/NEWBRANCH
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?
- Right-click the previous commit in the Log/history
- pick “Branch…” from the menu
- enter a branch name
- click “Create 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.
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.