0% found this document useful (0 votes)
5 views

Git+&+Github +branching

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Git+&+Github +branching

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Git Branching

987fac676ce7dd765e 236ff654e1adf35d897 171615141d1f1eeffd8765

parent none parent 987fac676... parent 236ff654e1a...


message remove x... message fix typo message add banner
Contexts
On large projects, we often work in multiple contexts:
1. You're working on 2 different color scheme variations for
your website at the same time, unsure of which you like best
2. You're also trying to fix a horrible bug, but it's proving tough
to solve. You need to really hunt around and toggle some
code on and off to figure it out.
3. A teammate is also working on adding a new chat widget
to present at the next meeting. It's unclear if your company
will end up using it.
4. Another coworker is updating the search bar autocomplete.
5. Another developer is doing an experimental radical design
overhaul of the entire layout to present next month.
Branches
Branches are an essential part of Git!

Think of branches as alternative timelines for a project.

They enable us to create separate contexts where we can try


new things, or even work on multiple ideas in parallel.

If we make changes on one branch, they do not impact the


other branches (unless we merge the changes)
New Color Scheme
New Color Scheme

Bug Fix
New Color Scheme

Bug Fix

Experimental redesign
New Color Scheme

Bug Fix

Experimental redesign
New Color Scheme

Merge!

Bug Fix

Experimental redesign
You Probably Have
Seen This Before
On branch master
nothing to commit
The Master
Branch
In git, we are always working on a branch* The
default branch name is master.

It doesn't do anything special or have fancy


powers. It's just like any other branch.
*technically that's not 100% true as we'll see later
Master
Many people designate the master branch as their
"source of truth" or the "official branch" for their
codebase, but that is left to you to decide.

From Git's perspective, the master branch is just like


any other branch. It does not have to hold the
"master copy" of your project.
Master?
Main?
In 2020, Github renamed the default branch
from master to main. The default Git branch
name is still master, though the Git team is
exploring a potential change.
We will circle back to this shortly.
Branching
Master Branch
987fac...
Branching
Master Branch
987fac... 236ff...
Branching
Master Branch
987fac... 236ff...

92faa...

Experimental Branch
Branching
Master Branch
987fac... 236ff...

92faa... 232fa...

Experimental Branch
Branching
Master Branch
236ff... 566ff...
987fac...

92faa... 232fa...

Experimental Branch
A Common Workflow
Master Branch

Merging back
to master

Adding A New Feature


Ethel's Branch

Master Branch

Fitz's Branch
commit bca5fc8b05deda4a13e7588c7ca352e47560c1dd
(HEAD -> master)
WTF IS THAT
CRAZINESS
HEAD? What Is That?
HEAD
We'll often come across the term HEAD in Git.

HEAD is simply a pointer that refers to the current "location" in


your repository. It points to a particular branch reference.

So far, HEAD always points to the latest commit you made on


the master branch, but soon we'll see that we can move around
and HEAD will change!
Ethel's Branch

Master Branch

HEAD

Fitz's Branch
HEAD
Ethel's Branch

Master Branch

Fitz's Branch
Ethel's Branch

Master Branch

Fitz's Branch
HEAD
My First Commit
On Master
Master HEAD

987fac...
Another Commit
Master HEAD

987fac... 236ff...
I make a new branch!
Master

987fac... 236ff...

DarkMode

HEAD
I make a commit on
the new branch
Master
HEAD

987fac... 236ff...

DarkMode

92faa...
2nd Commit
On New Branch
Master
HEAD
987fac... 236ff...

DarkMode

92faa... 232fa...
Return to Master
Master HEAD

987fac... 236ff...

DarkMode

92faa... 232fa...
Going Back To My
Dark Mode Branch
Master
HEAD
987fac... 236ff...

DarkMode

92faa... 232fa...
Merge Changes
Into Master Branch?

987fac... 236ff... 92faa... 232fa... f9de1....


Master

HEAD
92faa... 232fa...

DarkMode
Viewing
Branches
Use git branch to view your existing branches.
The default branch in every git repo is master,
though you can configure this.
❯ git branch

Look for the * which indicates the branch you


are currently on.
Creating
Branches
Use git branch <branch-name> to make a
new branch based upon the current HEAD
❯ git branch <branch-name>

This just creates the branch. It does not switch


you to that branch (the HEAD stays the same)
Master HEAD

987fac... 236ff...
❯ git branch bugfix

Master HEAD
I've made a new branch,
but I'm still working on master.
987fac... 236ff...

Bugfix
Switching
Branches
❯ git switch <branch-name>
Once you have created a new branch,
use git switch <branch-name> to switch to it.
❯ git switch bugfix
I've now switched over to the
Master new bugfix branch!

987fac... 236ff...
Notice that HEAD is now
pointing to Bugfix, not master.

Bugfix

HEAD
When I make a new commit, it
exists only on my new branch,
not on master!

Master
HEAD

Bugfix
987fac... 236ff...

92faa...
And another commit...

Master
HEAD

987fac... 236ff...
Bugfix

92faa...
❯ git switch bugfix

master branch The commit exists only on my


new branch, not on master!
987fac... 236ff...

92faa... HEAD

bugfix branch
Another way of
switching??
Historically, we used git checkout <branch-name>
to switch branches. This still works.

The checkout command does a million additional


❯git checkout <branch-name>
things, so the decision was made to add a
standalone switch command which is much simpler.

You will see older tutorials and docs using checkout


rather than switch. Both now work.
Creating &
Switching
Use git switch with the -c flag to create a new
branch AND switch to it all in one go. ❯ git switch -c <branch-name>

Remember -c as short for "create"


master branch
987fac... 236ff... HEAD
❯ git switch -c refactor

the -c flag makes the new


master branch refactor branch and switches
987fac... 236ff... to it all at once!

92faa... HEAD

refactor branch
master branch Remember, branches are
987fac... 236ff...
made based on the HEAD

92faa... 2456... HEAD

bugfix branch
If I branch from the bugfix branch,
master branch my new branch includes all the
987fac... 236ff...
commits from bugfix.

92faa... 2456... HEAD

bugfix branch
92faa...

new branch!
Viewing
❯ git
More Info
branch -v

Use the -v flag with git branch to view more * master 88df51f add documentation
information about each branch. bugfix 7c2a586 fix hover bug
chatdemo afadcf9 add chat widget

You might also like