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 branch2. Create a New Branch
Use the following command to create a new branch:
git branch branch_nameFor example, to create a branch named feature:
git branch feature3. Switch to the New Branch
To switch (checkout) to the newly created branch:
git checkout featureOr you can create and switch to the new branch in a single command:
git checkout -b feature4. Verify the Active Branch
Check the active branch again:
git branchThe active branch will be highlighted with *.
5. Switch Between Branches
To switch between branches:
git checkout branch_nameFor example, switching back to main:
git checkout main6. List All Branches
To list all branches:
git branch7. Push the New Branch to Remote (GitHub)
If you want to push the new branch to GitHub:
git push -u origin featureNow, you can work on your new branch and commit changes separately. 🚀