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

📅  最后修改于: 2023-12-03 14:41:25.167000             🧑  作者: Mango

Git Branch from Commit - Shell/Bash

As a programmer, you may need to create a new branch in Git from a specific commit. This can be done easily using the Git command-line interface (CLI).

Steps

Here are the steps to create a new branch from a specific commit:

  1. Open the terminal (or Git Bash for Windows users) and navigate to your Git repository.
  2. Run the following command to list all the available commits:
git log --oneline --decorate --graph

This command will display the commit logs, decorated with the branch names and in a graphical format. Note the commit hash of the commit you wish to create a new branch from.

  1. Run the following command to create a new branch:
git branch <new-branch> <commit-hash>

Replace <new-branch> with the name of the new branch you wish to create, and <commit-hash> with the actual commit hash of the commit you want to create the branch from.

  1. Switch to the newly created branch by running:
git checkout <new-branch>

This will move the head to the newly created branch.

  1. Finally, push the newly created branch to the remote repository:
git push -u origin <new-branch>

This will enable other developers to track your new branch.

Conclusion

Creating a new branch in Git from a specific commit can be done quickly using the Git CLI. By following the steps outlined above, you can create a new branch and push it to the remote repository in just a few commands.