Git creates a branch from a commit
This article will demonstrate how to create a new branch from a commit.
To create a branch from a SHA commit, use the command git branch <new_branch> <commit_sha>
with the commit as the last argument.
You can also use symbolic references instead sha
, for example git branch <new_branch> HEAD~4
.
$ git log
commit 1e087f5309ae647d16a0e1469dfd12a7cd91e22d (HEAD -> feature/changes-to-file)
Author: Cuong Nguyen
Date: Sat Dec 18 22:01:00 2021 +0700
Make some change to file.txt
commit ab38737fe95f4959139b995b960a0173b4dd2c7e
Author: Cuong Nguyen
Date: Sat Dec 18 21:26:31 2021 +0700
Hotfix #1
$ git branch branch-from-hotfix-commit ab38737fe95f4959139b995b960a0173b4dd2c7e
$ git checkout branch-from-hotfix-commit
Switched to branch 'branch-from-hotfix-commit'
$ git log
$ git log
commit ab38737fe95f4959139b995b960a0173b4dd2c7e (HEAD -> branch-from-hotfix-commit)
Author: Cuong Nguyen
Date: Sat Dec 18 21:26:31 2021 +0700
Hotfix #1
To checkout a branch when it was created, use git checkout -b <new_branch> <commit_sha>
.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Push to a specific branch in Git
Publish Date:2025/04/02 Views:53 Category:Git
-
In Git, we use branches to develop independent features directly from the main workflow of the project. Since Git is considered to be the best version control system so far, we have local and remote branches in our repository for different
Solve the Git Push Everything Up-To-Date Problem
Publish Date:2025/04/02 Views:120 Category:Git
-
Git is a free, open source version control system designed to work with projects quickly and efficiently. You can make changes to your repo and push them to master branches. This article explains how to use git push the command to resolve e
Git merge development branch into feature branch
Publish Date:2025/04/02 Views:126 Category:Git
-
Creating new branches and merging them is considered to be the most powerful tool of Git. The ability of Git to create new branches and merge them into the development code is very useful for developers working in a team environment. This f
Understanding Git conflict markers
Publish Date:2025/04/02 Views:112 Category:Git
-
In this article, we will discuss git conflict markers. Understanding Git conflict markers When pulling changes from a remote repository, you may encounter merge conflicts. Merge conflict files can sometimes be confusing. A typical merge con
Selectively merge changes from different branches in Git
Publish Date:2025/04/02 Views:116 Category:Git
-
This article will discuss merging specific changes from one branch to another. As we know, when merging branches, Git merges all files without exception. You may find yourself in a scenario where you have some commits in one branch and you
Complete the merge after resolving conflicts in Git
Publish Date:2025/04/02 Views:63 Category:Git
-
This article describes the process of completing a merge after resolving merge conflicts in Git. We will go through the merge steps, resolve the conflicts, and complete the merge. Complete the merge after resolving conflicts in Git For a si
Merge the development branch into Master in Git
Publish Date:2025/04/02 Views:167 Category:Git
-
This article provides an overview of merging a development branch into the master branch. Often we find ourselves creating a branch outside of the master branch for development. Once we are happy with the changes, we can merge them into mas
Recovering a conflicting Git merge
Publish Date:2025/04/02 Views:160 Category:Git
-
This article explains the revert command when a merge conflict occurs git merge . We will also take a quick look at how to undo a git merge that was successful and has been pushed to a remote repository. Recovering a conflicting Git merge I
Git merge-base determines the most recent common ancestor of two branches
Publish Date:2025/04/02 Views:126 Category:Git
-
This article explains how we can find the most recent common ancestor commit in Git. This comes in handy when you create a branch or merge one branch into another. Without further ado, let’s get into today’s topic. Determine the most re