📜  git create branch from commit - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:30:55.194000             🧑  作者: Mango

Git: Create branch from commit - Shell/Bash

As a developer, you may need to create a new branch in Git from a specific commit. This can be useful if you need to start working on a new feature or fix a bug in an older commit.

In this tutorial, we'll show you how to create a new branch from a specific commit using the shell/bash command line in a Git repository.

Requirements

Before we get started, you'll need the following:

  • A Git repository to work with
  • Access to a shell/bash command line terminal
Steps
  1. First, navigate to the Git repository on your command line using the cd command.
cd /path/to/repository
  1. Next, use the git log command to list all the commits in the repository. Take note of the commit ID you want to create the branch from.
git log

You'll see output similar to the following:

commit 38e1d46c74f1478430f7db356ce98adf3e70ee9b
Author: John Doe <johndoe@example.com>
Date:   Mon Oct 25 17:01:00 2021 +0000

    Updated README file
  1. Use the git branch command to create a new branch from the commit ID you noted in Step 2. Replace <new-branch-name> with your desired branch name.
git branch <new-branch-name> 38e1d46c74f1478430f7db356ce98adf3e70ee9b

You'll see output similar to the following:

* main
  new-branch-name
  1. Switch to the new branch using the git checkout command.
git checkout <new-branch-name>

You'll see output similar to the following:

Switched to branch 'new-branch-name'

Congratulations, you've successfully created a new Git branch from a specific commit!

Conclusion

In this tutorial, you learned how to create a new Git branch from a specific commit using the shell/bash command line. This can be useful when you need to start working on a new feature or fix an older bug. Remember to always use Git commands carefully and double-check before executing any critical operations.