Create and Switch Branches
Now that you understand the concept of branches, let’s create and switch between them. This is how you start working on a new feature or bug fix without disturbing the main code.
Creating a New Branch
To create a branch, use:
git branch branch-nameFor example, to create a branch called feature-login:git branch feature-loginThis creates the branch but does not switch to it. Your current branch remains unchanged.Switching Branches
To switch to an existing branch, use:
git checkout branch-nameOr the newer, more intuitive command:git switch branch-nameCreate and Switch in One Command
To create a new branch and immediately switch to it:
git checkout -b branch-nameorgit switch -c branch-nameWorking on a Branch
After switching, any commits you make will belong to that branch. For example:
git switch -c feature-login
echo "Login feature" > login.txt
git add login.txt
git commit -m "Add login feature"Now the commit is only on feature-login, not on main.Listing Branches
To see all branches and which one you’re on:
git branchThe current branch is highlighted with a *.Two Minute Drill
git branch branch-namecreates a new branch.git switch branch-nameswitches to an existing branch.git switch -c branch-namecreates and switches in one step.git branchlists all local branches.- Commits made on a branch stay on that branch until merged.
Need more clarification?
Drop us an email at career@quipoinfotech.com
