Creating and Switching Between Branches

Creating a Branch

To create a new branch in Git, you can use the git branch command followed by the desired branch name. Let's say we want to create a branch named feature-branch:

git branch feature-branch

This command creates the branch but does not switch to it yet. You can list all branches in your repository using git branch without any arguments:

git branch

output:

* main
  feature-branch

The asterisk (*) indicates the currently active branch, which is main in this case.

We can also create a branch using the checkout command:

git checkout -b feature-branch

This command would create and checkout the newly created branch.


Switching Between Branches

To switch to a different branch, you can use the git checkout command followed by the branch name. For example, to switch to the feature-branch we created earlier:

git checkout feature-branch

The output should indicate that you have switched to the feature-branch.

Switched to branch 'feature-branch'

Now you are in the feature-branch and any changes you make and commit will be isolated within this branch. You can verify your current branch by running git branch:

git branch

output:

  main
* feature-branch

The asterisk (*) has moved to indicate that you are now on the feature-branch.

creating and switching between branches is a powerful feature of Git that enables effective collaboration and parallel development. By following best practices for branch naming and management, you can maintain a well-organised and efficient workflow.

Last updated