Branches

What's a branch?

Branches allow us and others to work on different things, like features, at the same time. Let's find out what branch we're on!

$ git branch 

//Output Below

* master

Ah yes! The master branch is the default branch of every repository. Every time we add a commit, we're moving along the master branch.

Psst: as soon as we have additional branches, git branch will display those branches as well.

Creating a branch

We can create branches to work in isolated environments. A new branch is just an offshoot of an existing branch.

Let's create another branch called develop.

$ git branch develop
$ git branch

//Output Below

* master
develop

Great! Using git branch with a branch name, we can create a new branch, an environment for commits that won't change the master branch.

Psst: the * indicates that we're still on the master branch.

Switching branches

With multiple branches, we can have different versions of fortune_cookie.txt that we can access by moving between branches.

Let's create an alternate_reality branch and switch to it!

$ git branch alternate_reality
$ git checkout alternate_reality

//Output Below

Switched to branch 'alternate_reality'

Sweet! Our reliable checkout command can be used with the branch name to switch to another branch.

Committing again

Whenever we commit something, it'll be committed to the currently checked-out branch.

Let's switch back to master and commit to it.

$ cat cookie.txt
Chocolate Chip Cookie
$ git branch alternate_reality
$ git checkout master 
$ echo Frosted Snowman Cookie > cookie.txt
$ git add .
$ git commit -m "Modify cookie"

//Output Below

[master a8ed121] Modify cookie
 1 file changed, 1 insertion(+), 1 deletion(-)

Wohoo! We switched to master, so the commit happened in that branch. If we would switch to alternate_reality, so would the commit.

Logging with flags

There's a way to display logs that makes reading them a lot easier.

$ git log --graph

//Output Below

* commit 86b289d
| Author: Elliot <elliot@allsafe.com>
| Date:   Tue Nov 15 11:41 2016
| 
|    Add cake
|
* commit 52ebf24
  Author: Elliot <elliot@allsafe.com>  
  Date:   Tue Nov 15 11:36 2016
    
     Add cookie

Way better! With flags like --graph, we can specify what we want Git to do in a more precise way.

Logging with branches

There's also a flag that allows us to view branches and the divergence of commits.

$ git log --graph --all 

//Output Below

* commit 86b289d
| Author: Elliot <elliot@allsafe.com>
| Date: Tue Nov 15 11:41 2016
| 
|    Add cake
|
| * commit 168291b
|/ Author: Elliot <elliot@allsafe.com>
|  Date:   Tue Nov 15 11:38 2016
|
|    Change cookie
|
* commit 52ebf24
  Author: Elliot <elliot@allsafe.com>
  Date: Tue Nov 15 11:36 2016

     Add cookie

Sweet! When we use the --all flag along with log --graph, we can see all commits from all branches.

Information overflow

When there are tons of commits, however, we might not want to see all of the available information at once.

$ git log --graph --all --oneline 

//Output Below

* 86b289d Add cake
| * 168291b Change cookie
|/
* 52ebf24 Add cookie

Nice! What a beautiful piece of condensed information we have there!

Psst: --oneline is a short version of --pretty=oneline --abbrev-commit.