git branch command
A branch is a version of the repository that diverges from the main working project. A Git project can have more than one branch. These branches are a pointer to a snapshot of our changes.
Create a Branch
We can create a new branch with the help of the git branch command.
Syntax git branch <branch name>
Points to be noted:
- This command will create a new branch locally based on the current HEAD.
- This command only creates the branch, it doesn't switch us to it. The HEAD remains the same branch on which we are at the time of executing the command.
The git switch command is used to switch between branches. Syntax git switch <branch name>
Here, <branch name> specifies the branch on which we want to switch to.
To create and switch simultaneously with only one step we can use the following command.
Bash
This is a convenient shortcut for:
Bash
git switch <new-branch>
List the branches
To verify this, first we list all the available branches in our repository by using the following command. Either we can use git branch --list or git branch command to list the available local branches in the git repository.
Syntax git branch
Or
git branch --list
Rename Branch
We can rename the branch with the help of the git branch command. Syntax git branch -m <old branch name><new branch name>
When we run the command "git branch -m demo practice", Git will rename the branch named "demo" to "practice". This can be useful when we want to give a more meaningful or accurate name to a branch or when we want to align branch names with the purpose or content of the changes in the branch.
Delete Branch
We can delete the specified branch. It is a safe operation. In this command, Git prevents us from deleting the branch if it has unmerged changes. Git Syntax git branch -d <branch name>
Delete a Remote Branch
We can delete a remote branch by running the command from the git bash itself. The following command syntax is used to delete a remote branch. Git Syntax git push origin --delete <remote-branch-name>