Learn Version Control with Git
)}

git branch

Our git history corresponds to three commits and a tag, but there's also another element pointing to that history of commits and it's called a branch. The current branch we are in is the master branch.

let's programmatically observe which branch we are in:

git branch
>> * master

we are in the master branch

Creating a new branch

Let's create another branch and call it new-feature

git branch new-feature

We can observe our list of branches using git branch again:

git branch
>>  * master
      new-feature

Now that we have two branches pointing to our recent commit. Let's add some content to one of our files:

echo "more random text" >> project-1.txt

let's stage our changes:

git add .

then let's commit the changes:

git commit -m "added more random text"

We now have our fourth commit, let's view the history:

git log

Our commit was created in the master branch and not the new-feature branch. This is because the HEAD of our repository is pointing at master. Our diagram should now look something like this:

The new-feature branch is pointing at commit 3, and the master branch is pointing at commit 4. Since HEAD is pointing at master, the recent commit will be applied in the master branch.

Switching branches

Let's switch to the new-feature branch:

git checkout new-feature

Let's make sure we are in the new branch:

We successfully switched to the new-feature branch. you can tell by the asterisk behind it. Our diagram should now look something like this:

Our HEAD is now pointing at new-feature.

Let's add a new feature to our project:

echo "just added a really cool feature" >> project-1.txt

you should be getting into the habit of using git status to observe your changes. I won't be using it a lot moving forward, since it will clog up the course.

let's stage and commit our changes:

git add .
git commit -m "new feature added"
git log

Our HEAD is now pointing at new-feature and our recent changes have been applied in that branch:

Our diagram should look like this now:

Our newest commit is only available in the new-feature branch. Let's checkout to our master branch again:

git checkout master

Now open your project-1.txt file, you will notice that the change you recently made is not there. Don't be afraid, this new feature you added is still there, but in the new-feature branch. You can always checkout there to view the change.

git branch new-feature

Now open the file again, and your changes are back.

Feedback Summary
5.0
3 students
5

100%
4

0%
3

0%
2

0%
1

0%
Written Reviews
There are no written reviews yet.