You are just cruising along coding and then wham! You need to stop your work mid-way and fix a production issue or take on another higher-priority business requirement.
If your code was on a dedicated branch for the current sprint/release, all is well and good. Switch to the master, create another branch and you are good to go...
But what happens when you work on the master branch?
Luckily, Git has an inbuilt feature for you to create a branch from a previous commit. (You commit your code every time there's a change, right?😁)
You can create the branch via hash,
git branch branchname <sha1-of-commit>
(Visual Studio Team Explorer has a link "Copy Commit ID" which allows you to copy the sha1 of the commit you want to branch from)
or by using a symbolic ref.
git branch branchname HEAD~3
If you are on Visual Studio, you cannot do this from the IDE and would need to do it via the Git Bash console.
A word of caution when working with Git on Team Explorer in VS IDE.
When viewing the history of the solution, it only shows the history for the file and not for the entire solution as you might think. At this point, it would be better to use Git Bash or Git GUI to get the sha1 of the commit you want to branch from.
This article was originally written on Google's Blogger platform and ported to Hashnode on 17 Sep 2022.