Finally, as you are now ready to embark on your journey in discovering
the magic of Git, you need to learn about how branches work.
Branches allow for unlimited iteration of your project files. For
example, if you want to test a new feature or bit of code, you can
create a separate “branch” of the project.
By default, every Git project has a master branch, or “upstream”
branch that is meant to reference the final/complete version of your
project.
By creating branches you can make unlimited changes to your project,
and commit them, without affecting the master copy.
When working with branches you can always switch back to the master
branch—or other branches to “checkout” the files or modifications in
that branch.
However, one caveat of branches is that you can only “checkout” other
branches from a clean state. Which means if you have uncommitted
changes in your current branch you cannot checkout other branches.
But if you’re unready to commit those changes on your current branch,
you can stash them aside for later.
In order to see what branch you’re currently on, you can run the git
branch command by itself:
git branch
If you haven’t created any alternative branches yet, you will see a
listing for the “master” branch, which you’re on by default.
To create a new branch you can run git branch followed by the name
of the branch you want to create, like so:
git branch <name-of-branch>
Now when you run git branch you’ll see “master” and the new branch
listed. Master will still be selected as your active branch.
In order to “checkout” a different branch, run the git checkout
command followed by the name of the branch you want to check out.
git checkout <name-of-branch>
You can check to make sure you’re on the new branch by running git
branch again to see that the new branch is selected/highlighted.
(It’s always good to check your work.)
Now, notice, if you make changes to the files on this branch, you will
not be able to switch back over to the “master” branch unless you
commit your changes or stash them.
To stash your changes, run git stash.
git stash
When you return to the new branch, you can unstash, or “pop,” your
changes. (Actually, you can pop changes on any branch. See the full
guide on git stash to learn more.)
Those are the very basics of Git. From there, you can master the
entire program because you have grounded yourself in the fundamentals.
If you have any issues, or unexpected errors, leave a comment below
and the InMotion community team will try to help out.