Creating and Switching Branches
Creating Branches
Creating a new branch in Git is a straightforward process. You can use the git checkout
command with the -b
option to create and switch to a new branch simultaneously:
git checkout -b feature/new-functionality
This command will create a new branch named feature/new-functionality
and switch your working directory to it.
Alternatively, you can first create the branch and then switch to it using two separate commands:
git branch feature/new-functionality
git checkout feature/new-functionality
Switching Branches
To switch between existing branches, you can use the git checkout
command without the -b
option:
git checkout existing-branch
This will update your working directory to the specified branch, allowing you to work on the corresponding codebase.
Listing Branches
You can view a list of all branches in your local repository using the git branch
command:
git branch
This will display all the branches, with the currently active branch marked with an asterisk (*
).
To view both local and remote branches, you can use the following command:
git branch -a
This will show all the branches, including those that are hosted on remote repositories.
Deleting Branches
When a branch is no longer needed, you can delete it using the git branch
command with the -d
option:
git branch -d feature/old-functionality
This will delete the feature/old-functionality
branch from your local repository. If the branch has already been merged into another branch, Git will allow you to delete it. If the branch has not been merged, Git will refuse to delete it, unless you use the -D
option instead.
By understanding these basic commands for creating, switching, and managing branches, you can effectively navigate the Git branching ecosystem and streamline your development workflow.