How to Move Existing, Uncommitted Work to a New Branch in Git
So here’s a new one for ya.
I was working on a branch all day, closed the laptop, went home and fell asleep. Nothing out of the ordinary. The next day, I awoke, and upon returning to work, immediately began working on a new problem.
The issue arose when I discovered to my horror that I was still on the same branch from yesterday! *Gasp*
Lest we forget, we have Git at our disposal — so I take a breath and dive in:
This will probably return a list of changes not staged like this:
On branch fix-from-yesterday
Your branch is up to date with 'origin/fix-from-yesterday-'.
Changes not staged for commit:
(use "git add …" to update what will be committed)
(use "git checkout -- …" to discard changes in working directory)
Now that we have a pulse on things, we can do this:
git checkout -b new-branch-of-changes-for-today
This will leave your current branch as is, create and checkout a new branch and keep all your changes. You can then make a commit with:
git add <the files you want commit>
and finally, commit to your new branch with ol trusty:
git commit -m "Make sure your description is short and sweet
-bcauses a new branch to be created as if git-branch were called and then checked out. In this case you can use the
--no-trackoptions, which will be passed to git branch. As a convenience,
-bimplies branch creation; see the description of
-Bis given, <new_branch> is created if it doesn’t exist; otherwise, it is reset. This is the transactional equivalent of
Comments, discussion & feedback.
Was this post helpful? Does it need work? Let me know below.