git branch Command


git branch Command

The git branch command is used to manage branches in a Git repository. It can create, list, and delete branches, but it does not switch between them or merge them. The command helps you manage the different lines of development in your project.


Key Uses of git branch

1. Listing Branches

To list all branches in your repository:

git branch
  • This command shows all local branches. The currently active branch is highlighted with an asterisk (*).

Example Output:

* main feature/login feature/signup

To list all branches, including remote branches:

git branch -a
  • -a: Lists both local and remote branches.

2. Creating a New Branch

To create a new branch:

git branch <branch-name>
  • <branch-name>: The name of the new branch you want to create.

Example:

git branch feature/login

This command creates a new branch named feature/login, but it does not switch to it.

3. Deleting a Branch

To delete a local branch:

git branch -d <branch-name>
  • -d: Deletes the branch if it has been fully merged into the current branch or another specified branch.

To forcefully delete a branch, even if it hasn't been merged:

git branch -D <branch-name>
  • -D: Force deletes the branch, regardless of its merge status.

Example:

git branch -d feature/login

This command deletes the feature/login branch if it has been merged.

To delete a remote branch:

git push origin --delete <branch-name>

Example:

git push origin --delete feature/login

This command deletes the feature/login branch from the remote repository.

4. Renaming a Branch

To rename the current branch:

git branch -m <new-branch-name>
  • -m: Renames the current branch to <new-branch-name>.

To rename a different branch:

git branch -m <old-branch-name> <new-branch-name>

Example:

git branch -m old-feature new-feature

This command renames the branch old-feature to new-feature.


Examples

Create and Switch to a New Branch

To create a new branch and switch to it in one command:

git checkout -b <branch-name>
  • -b: Creates a new branch and switches to it.

Example:

git checkout -b feature/login

This command creates and switches to the feature/login branch.

List All Branches

To list all local branches:

git branch

To list all branches, including remote:

git branch -a

Delete a Branch

To delete a local branch:

git branch -d feature/login

To forcefully delete a branch:

git branch -D feature/login

To delete a remote branch:

git push origin --delete feature/login

Summary

  • Purpose: git branch is used to manage branches in a Git repository, including creating, listing, deleting, and renaming branches.
  • Basic Commands:
    • git branch: List local branches.
    • git branch -a: List all branches (local and remote).
    • git branch <branch-name>: Create a new branch.
    • git branch -d <branch-name>: Delete a local branch.
    • git branch -D <branch-name>: Forcefully delete a branch.
    • git branch -m <new-branch-name>: Rename the current branch.
    • git branch -m <old-branch-name> <new-branch-name>: Rename a different branch.

The git branch command is essential for managing different lines of development and organizing your work in a Git repository.