To create and switch branches in Git using the command line, follow these steps:
1. Check the Current Branch
Before creating a new branch, check which branch you're currently on:
git branch
2. Create a New Branch
Use the following command to create a new branch:
git branch branch_name
For example, to create a branch named feature
:
git branch feature
3. Switch to the New Branch
To switch (checkout) to the newly created branch:
git checkout feature
Or you can create and switch to the new branch in a single command:
git checkout -b feature
4. Verify the Active Branch
Check the active branch again:
git branch
The active branch will be highlighted with *
.
5. Switch Between Branches
To switch between branches:
git checkout branch_name
For example, switching back to main
:
git checkout main
6. List All Branches
To list all branches:
git branch
7. Push the New Branch to Remote (GitHub)
If you want to push the new branch to GitHub:
git push -u origin feature
Now, you can work on your new branch and commit changes separately. 🚀