Why?
What?
Most of today's explanations will be covered via a live demo. If you want to follow a written guide, then please checkout Atlassian's git guide.
Source: https://github.com/frappe/charts/issues/180
Your "master" branch is just a pointer to a particular commit on master (usually the latest).
You can create your own branch if you want to continue on a separate thread of working, unrelated to the master branch.
git checkout -b new_branch_name
This then allows you to continue making commits on a separate "branch".
There is no limit for the number of branches you can have in a repository.
Your local repository can also "check out" (work with) a single branch at a time. You can swap between branches using the checkout command.
It's generally good practice to ensure you have no staged or unstaged changes on your branch before swapping to another.
git checkout branch_to_swap_to
The process of "incorporating work on another branch into mine" is known as merging. The two most common cases of merging you'll see are:
The merge command let's you specify the branch you want merged into your current branch.
git merge master
# | Commits made on your branch | Commits made on master branch | Command & Outcome |
---|---|---|---|
1 | No | No | Nothing to do |
2 | Yes | No | from master, git merge branch-name Will "fast forward" merge (i.e. simply bring your branch pointer to the same commit as branch-name, effectively no merge) |
3 | No | Yes |
from your branch, git merge master Will "fast forward" merge (i.e. simply bring your branch pointer to the same commit as master, effectively no merge) |
4 | Yes | Yes |
from your branch, git merge master Will merge master into your branch, but a merge commit will get made (either automatically or manually) |
The following describe a scenarios of scenarios with respect to merging between your working branch and master
In most industries, you cannot merge your branch into master via the command line. Instead, we allow our git site (e.g. gitlab) to do this via a Merge Request (a web-based GUI that helps manage merges into master)