In Git, switching between branches is a common operation when working with multiple lines of development in a codebase. To switch between branches, you can use the git checkout command.
Here’s an example of how to switch to an existing branch:
# Switch to an existing branch called "new-feature"
git checkout new-feature
After running this command, you will be switched to the "new-feature" branch. Any changes you make to files in your repository will be made on this branch.
If you want to create a new branch and switch to it in one step, you can use the -b option with the git checkout command:
# Create and switch to a new branch called "new-feature"
git checkout -b new-feature
After running this command, you will be switched to the new "new-feature" branch, and any changes you make to files in your repository will be made on this branch.
To list all branches in your repository, you can use the git branch command:
# List all branches in the repository
git branch
This command will show a list of all branches in your repository, with the current branch highlighted with an asterisk (*).
In summary, switching between branches in Git is a simple and common operation when working with multiple lines of development. To switch to an existing branch, use the git checkout command followed by the name of the branch. To create and switch to a new branch in one step, use the -b option with the git checkout command. You can list all branches in your repository using the git branch command.