-
git switch -c <branchname>
(this will create branch and switch to it)- ex. "git switch -c feat/payments"
-
git push -u origin <branchname>
(this will configure the new branch in the remote repository)- ex. "git push -u origin feat/payments"
-
Work on the new branch and make as many commits as you need
- ex:
- git commit -m "implement shopping cart"
- git commit -m "implement css for shopping cart"
- ...
- ex:
-
git switch <branchname>
(switch to a different branch)- ex. "git switch main"
-
git merge <branchname>
(merge code from "branchname" into current branch)- ex. "git merge feat/payments"
After that, if you need to continue working on your branch (for example, to continue working on "feat/payments"):
- git switch feat/payments (switch again to "feat/payments" branch)
- make commit(s)
- repeat points 4. and 5.
Atlassian Tutorial: using branches
Git checkout vs. git switch:
- In older versions of git, we used to use a similar command called
git checkout
(git switch was introduced just some years ago). - You may still see "git checkout" in a lot of tutorials, stackoverflow etc. π¦. You can find more info here.
Visualizing Git (a playground where you can try commands and see a diagram with the result)
Learn Git Branching (guided tutorial to learn about git branches)
Sometimes we're not sure how to do something on git (or if the command that I'm about to use will destroy hours of work...).
In those cases, you can create a directory somewhere in your computer, open a command line, initialize a new repo... and practice with it in a safe place π―π