Main branch keeping a clean history of releases
Room for experimentation without breaking anything
Simultaneous work for many people
Easy to reverse work on a feature and continue with main
Separate teams can work at the same time on: larger patches, hotfixes, new features, optimisation, etc.

(Source: Git Better)
Recall master or main branch is an indicator
You create a new branch using git branch <branch-name>
The new branch is a new indicator with its own staging area and working directory
Scroll down to see what happens step-by-step
main
Recall: HEAD points to 'where we currently are'
main points to the latest commit on branch
git branch feature
This creates a new indicator pointing to where we are.
But HEAD still points to the main branch.
git checkout feature to get to the new "feature" branch
The HEAD indicator goes to the feature branch.
git branch [-l] list/create/delete branches
git checkout move the HEAD pointer (switch between branches)
git fetch sync cached branches with remote
git rebase [x] make a branch [x] start from a different point
git merge [x] combine two branches (by joining [x] to the current)
dev1 create a short history of commits (minimum two) and push themdev2 pull the informationfeature1git branch option to do that)git checkout <branch-name>)You can add commits only to one of the branches at a time.
When you switch between branches, Git 'refreshes' your staging area and working directories to match HEAD.



In a big project, e.g. if you're the one working on the feature, changes to main might not be yours (other people push their features/fixes, etc. in the meantime).
feature branch with git checkout feature


main with git checkout main
feature1 branch, add a file, stage and commit it. Then do a push.Read the message and follow the hints.
main branch and check what's going on in your working directory (e.g. in File explorer in Windows or Finder in MacOS).What happens? (put it somewhere as part of a new commit and push it)
git pull and check git branch -lgit checkout feature1 and check git branch -l againmain branchfeature1 branchgit status on the main branch, push the changesgit status on the feature1 branch, push the changesYou might recall merge from how we united main with origin/main.

main do git merge feature
Recall: merge creates a new commit.
feature branchIt doesn't include the merge (unless you also do git merge main while on the branch feature).

Can you trace what's been going on in this project?

(Source: Git Better)
feature1 branch to the main branch using git merge <branch-name>. Push.Conflict resolution is the same as we once did.
Rebasing is making your branch start from a different point

git checkout feature)
git rebase main
Important: rebase destroys original commits and creates new ones! Probably not a good thing if the original ones were shared with others or in a remote repository!
Assume you start with this:

feature1 branch proved a dead end, but feature2 is promisingfeature2 is actually unrelated to feature1main branch introduced something relevant to feature2feature2 to maingit checkout feature2
git rebase --onto main feature1 feature2
git rebase --onto <new-base> <old-base> <moved-branch>

Can you see how rebase could come in handy for the lower feature branch?

(Source: Git Better)
Check this tutorial for an easy step-by-step description (the image comes from there)

Or try this one if you like X-Men - it's actually helpful (if you watched Days of Future Past)!
Typically HEAD points to the branch ("you're currently here").
You can point it to an older commit instead: git checkout 87ec91d

This puts you in a "detached HEAD state".
Here you can:
git checkout master
(and turn it into a new branch if you want to keep it)

You do the following:
What's the new situation? Draw it on a piece of paper or send me a photo :)
Read Merging vs rebasing on Atlassian for a nice description of both approaches (also their individual pages).
Also, take a look at git bisect, to understand when rebasing might be helpful.
Also, take a look at git stash.