Creating a new branch from a specific commit can be useful in many scenarios, such as recovering lost changes, starting a new feature based on an older stable state, or simply exploring what-if scenarios from a certain point in the project's history. Creating a branch from a commit involves pointing a new branch reference to a specific commit in the repository's history.
This guide explains how to create a branch from a commit in Git, covering various scenarios and commands.
Step-by-Step Guide to Creating a Branch from a Commit
1. Find the commit id
First, you need to locate the commit from which you want to create a new branch. You can find the commit ID by looking at the repository's log:
git log --oneline
This command will display a simplified commit history. Identify the commit hash (a short SHA-1 hash) for the commit from which you want to branch off.
2. Creating the branch
Once you have identified the commit hash, you can create a new branch from it using the following command:
git branch <new-branch-name> <commit-hash>
Replace <new-branch-name>
with your desired branch name and <commit-hash>
with the actual commit hash.
For example:
git branch feature-update 9fceb02
This command creates a new branch called feature-update
from the commit with the hash 9fceb02
.
3. Checkout the new branch
If you want to switch to the new branch immediately, use:
git checkout <new-branch-name>
Alternatively, you can create the branch and check it out in one step with:
git checkout -b <new-branch-name> <commit-hash>
4. Creating a branch without switching to it
If you wish to create a new branch but stay on your current branch, simply use the git branch
command without the checkout:
git branch <new-branch-name> <commit-hash>
This is useful when you need to create multiple branches or do not want to disrupt your current workflow.
Advanced scenarios
Creating a branch from an old or previous commit: If the commit is far back in the history, or you want to branch from a commit that is not the latest, follow the steps outlined but ensure you have the correct commit hash from the
git log
.Creating a branch after making changes: If you have made changes and want to preserve them in a new branch, first stash your changes:
Terminalgit stashThis command takes both your staged and unstaged changes and saves them in a local stack-like structure called a "stash." It then reverts your working directory and index to match the HEAD commit, essentially giving you a clean working state without losing your changes.
Once your changes are stashed, create and checkout the new branch as described above, and finally apply your stashed changes:
Terminalgit stash popThis command removes the top stash from the stack and applies the changes contained in it to your current working branch. If there's a conflict between the stashed changes and the current branch's state, you'll be prompted to resolve these conflicts manually.
To learn more about branching in Git, see the official Git documentation.