#!/linuxSucks/How to work with Git Branches
Jan 10, 2021Listing all branches with “git branch” command
git branch -a
## Switch branch using git checkout
Switching to a existing branch with “git checkout” command
git checkout <existing_branch>
Create and switch to a new branch with “git checkout -b” command
git checkout -b <new_branch>
Switch to a non-existing branch, the following error message will show up
error: pathspec 'non-existing-branch' did not match any file(s) known to git
To solve this error, you will have to append the “-b” (for “new branch”) option to the checkout command.
Switch branch using git switch
A quick way of switching branch on Git is to use the “git switch” command and specify the name of the branch you want to switch to.
git switch <existing_branch>
Create and switch to a new branch with “git switch -c” command
$ git switch -c <new_branch>
Switch to a non-existing branch, the following error message will show up
fatal: invalid reference: non-existing-branch
To solve this error, make sure to append the “-c” option to the “git switch” command to specify that you want to switch to a new branch.
Merged branch to the master
First commit all your changes in your branch like your normally do, like
git status
git add –a
git commit –m "Some commit message"
Then switch to the master branch
git checkout master
Check that you are in the master branch, now run “git merge” command
git merge <branch>
If all goes well then our job is done. The new feature commits now appear in the master branch.
Delete a local branch
To delete a local branch with “git branch -d” command
git branch -d <branch>