Day Three - Come on guys! Let's collaborate!

“Leave the gun. Take the cannoli.”

Heyyyyyy,

Let’s talk about teamwork!! Yeah! I know that sometimes is very difficult to work with others but Git will keep it simple. Let’s deep dive into this session, my friends. Hope you’ll enjoy it.

Agenda


Branches

Every time we perform a commit operation, we are linking the new commit with the previous one. If you want to focus on new works without the pain to break the code…you can think about put in place some branches to split your work in small pieces.
You can think about branches as an independent line of development. A branch will give you a brand new working directory, staging area and project history.

When you launch the “git init” command, a new branch (the master/main branch) is created. It won’t be the branch used for deployment (even if it’s very common). Every branch can be used for code deployment. It’s up to you to create the strategy for your work.

HEAD is a reference to a specific commit im a branch that we are viewing. HEAD will give you a guide about which branch you are checking out.

If you are working on a branch and you want, at certain point, continue working on the master branch, you only need that HEAD points the master branch.

Let’s recap, HEAD allows us to navigate in our Git three between branches.

Branch Commands! Hands on!

Let’s do some excercises to better undestand branches and how to navigate between them:

So, for this excercise I’ve created a new folder on my Desktop and we moved in, like that:

cd Desktop
mkdir GitBranches
cd GitBranches

Let’s check that no repository are created:

git status

and then let’s initialize our repository (as usual):

git init

If you launch a

git log

command to check if there are some commits, you will be reported that our current branch doesn’t have any coomits yet. I will create a simple text file:

touch mytextfile.txt

Let’s add this file to our stagin area and then commit the changes

git add mytextfile.txt
git commit -m "File Created"

Now you can call git log to better understand how HEAD is working

git log

As you can see the history of commits, is telling you that you are working on the main branch (HEAD is pointing to main). Now we will create a new branch

git branch mynewbranch

The command above will create a new branche called “mynewbranch”. If you launch:

git branch

It will list all the available branches. Now if we run the following bunch of commands:

touch mymasterfile.txt
git add mymasterfile.txt
git commit -m "Master File Created"
git log

We can see in the history that HEAD is still pointing to the main branch and we have a new commit on this branch. But the new file won’t be part of the branch called “mynewbranch”. If you want to work on this particular branch, then we have to switch on it:

git switch mynewbranch

Now let’s create a new file and commit it:

touch mybranchfile.txt
git add mybranchfile.txt
git commit -m "Branch File Created"
git log

Now you will see that the commit is pointing the new branch and not the main one. If you switch back to the main repository, in your working directory you won’t find this new file but you will find the “mymasterfile.txt” file that is part of this commmit.

Rename/Delete a branch

If you want to rename an existing branch, you simply need to point that branch! So, let’s assume we would like to rename our branch from “mynewbranch” to “thisismynewbranch”. We need to do the following:

git switch mynewbranch
git branch -m thisismynewbranch

If you want to delete a branch, you won’t be in that branch. So, let’s assume we would like to delete the branch called “thisismynewbranch”. The steps we are going to do are:

git switch main
git branch -D thisismynewbranch

The option -D (in uppercase) will prevent us to receive a warning in case the branch you are going to delete is not merged.

Merge a branch

If you want to merge a specific branch to another one, you have to switich to the target branch and then launch

git merge branch_name

Git will create (or it tries to do that) the merge. If no conflicts are detected (updates on branch we would like to merge from are not part of destination branch), the destination branch will receive the updates from the source branch.

If some updates from the source branch are in conflict with other updates we have on the destination branch, we have to fix them.
These conflicts are well known as merge conflicts. Git will warn you about that and you must edit the files to remvoe conflicts.
Git will show you line by line the conflicts and some editors (like VS Code) will give you a better overview with syntax highlighting.

FAST FORWARD MERGE:

Imagine you create a new project with a new repo and you perform the first commit on the master.
Now you create a new branch from that point and you start working on the new branch.

git branch new_branch

You create some new commits on the new branch and no other commits on the master one.
In order to “fast forward” the merge you only need to switch to the master branch and launch the

git switch master
git merge new_branch

In this way the master branch will receive all the updates that were made on the new_branch and it will be synched with that branch.

In case of conflicts during the merge operation, you will be asked to fix the conflicts, choosing which version needs to be taken. At the end, the merging will succed.