118k views
2 votes
How to switch to a different branch in git

1 Answer

4 votes

Final answer:

To switch to a different branch in Git, use the 'git checkout' command along with the branch name. If you need to create a new branch and switch to it, use 'git checkout -b' followed by the new branch's name.

Step-by-step explanation:

To switch to a different branch in Git, you can use the git checkout command followed by the name of the branch you want to switch to. If the branch doesn't exist in your local repository, Git will inform you. To create and switch to a new branch, you would use the git checkout -b command along with the new branch's name.

Here is an example of how to switch to an existing branch:

  1. Open your terminal or command prompt.
  2. Navigate to your repository's directory.
  3. Type git checkout branch-name and press enter, replacing 'branch-name' with the name of your desired branch.

And if you need to create and switch to a new branch, use:

  1. Type git checkout -b new-branch-name and press enter, replacing 'new-branch-name' with the name you wish to give to the new branch.
User Psur
by
6.8k points