Branch from a previous commit using git

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, alls 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?
Luckily, git has a inbuilt feature for you to create a branch from a previous commit. [You commit your code everytime 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 history of the solution, it only shows history for the file and not for 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.

No comments:

Post a comment