Create a Git branch from an earlier commit
Start a branch at an earlier commit or release tag, keep detached-HEAD work, and understand what happens when the branch later merges into main.
Find the commit you want, then run git switch -c <new-branch> <commit>. The new branch points to that commit, and Git switches your working tree to it. The existing branch stays where it was.
Find the starting commit
git log --oneline --graph
Suppose the commit is a1b2c3d. Replace that example hash with one from your repository:
git switch -c hotfix-from-old a1b2c3d
On Git versions without switch, the equivalent is:
git checkout -b hotfix-from-old a1b2c3d
Run one of these commands, not both. Git may refuse to switch if it would overwrite local changes; commit or stash those changes deliberately first.
You can also use a relative revision:
git switch -c bugfix HEAD~5
HEAD~5 follows the first parent five times. In a history with merges, that is not necessarily the fifth entry you saw in an unrestricted log.
Create the branch without switching
git branch hotfix-from-old a1b2c3d
This creates the branch reference and leaves your current checkout alone. Choose this instead of switch -c when you want to return to the work later.
Common starting points
For a fix against a deployed release, start at its tag:
git switch -c hotfix v1.2
For a commit no longer visible from your branches, inspect git reflog, verify the candidate with git show <hash>, and create a branch at it while the object still exists. Reflogs are local recovery aids with finite retention.
After a bisect identifies the first bad commit, its parent can provide a baseline for testing a fix. For a merge commit, choose the relevant parent explicitly; the first parent is not automatically the right baseline for every regression.
Publish the new branch
When you are ready to share it with your configured remote:
git push -u origin hotfix-from-old
This publishes that branch and sets its upstream. It does not move the remote’s main branch.
Frequently asked questions
What history does the new branch contain?
It reaches the selected commit and that commit’s ancestors, including ancestry through merge parents. It does not include every commit made before a particular date. To narrow a search, try git log --all --since="2026-09-01" --oneline, then inspect the result before choosing a starting point.
Will merging it restore main to the old snapshot?
No. A merge uses the commit graph and merge base to combine changes. The starting point matters to that calculation. An unchanged branch at an ancestor of main may have nothing to merge; a branch with new fixes may merge cleanly or conflict with later work. Rebasing the fixes is another workflow, but it rewrites those commits and should be coordinated if they have been shared.
How do I keep commits made in detached HEAD?
While still at the commit you want to retain, create a branch:
git switch -c recovered-work
If you have already moved away, locate the commit in the reflog first. Creating a branch at the right commit makes it reachable through a named reference.
References
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan