branch
The git branch command is used to manage branches in a Git repository. Branches are an essential part of Git, allowing you to work on different features or bug fixes independently of the main codebase.
Detailed Explanation
Creating Branches: A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is
master(ormainin newer repositories). When you create a new branch, you are creating a new pointer to the current commit.Switching Branches: You can switch to an existing branch using the
git checkoutorgit switchcommand. This updates the working directory to match the state of the branch you are switching to.Listing Branches: You can list all branches in your repository using
git branch. This will show you all local branches and indicate the current branch with an asterisk.Deleting Branches: Once a branch is no longer needed, you can delete it using
git branch -dfor a safe delete (only if the branch has been merged) orgit branch -Dfor a force delete.
Examples
Creating a New Branch:
git branch new-featureThis command creates a new branch named
new-feature.Switching to a Branch:
git checkout new-featureThis command switches to the
new-featurebranch.Listing All Branches:
git branchThis command lists all branches in the repository, with the current branch indicated by an asterisk.
Deleting a Branch:
git branch -d new-featureThis command deletes the
new-featurebranch if it has been merged. Use-Dto force delete if it hasn't been merged.