Git Exercises

Git Branch

The `git branch` command is used to manage branches in Git. Here are some commonly used options and scenarios with `git branch`:


- **List Branches**: To list all branches in your repository, use the following command:

git branch
This will display a list of branches in your repository, with the current branch highlighted with an asterisk (*).


- **Create a New Branch**: To create a new branch based on your current branch, use the following command:

git branch <branch-name>
Replace `<branch-name>` with the desired name for your new branch. This command creates a new branch without switching to it.


- **Switch to a Branch**: To switch to an existing branch, use the following command:

git checkout <branch-name>
Replace `<branch-name>` with the name of the branch you want to switch to. This command allows you to start working on the specified branch.


- **Create and Switch to a Branch**: If you want to create a new branch and immediately switch to it, you can use the following command:

git checkout -b <branch-name>
Replace `<branch-name>` with the desired name for your new branch. This command combines branch creation and switching into one step.


- **Delete a Branch**: To delete a branch, use the following command:

git branch -d <branch-name>
Replace `<branch-name>` with the name of the branch you want to delete. Note that you cannot delete the branch you are currently on. To delete a branch forcefully, use `-D` instead of `-d`.


- **Rename a Branch**: To rename a branch, use the following command:

git branch -m <old-branch-name> <new-branch-name>
Replace `<old-branch-name>` with the current name of the branch, and `<new-branch-name>` with the desired new name for the branch.


These are some of the basic `git branch` commands that allow you to create, switch, delete, and rename branches. Branching is a fundamental feature of Git that enables parallel development, isolation of features, and easy collaboration among team members.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext