Branches

Creating a branch is essentially copying your repository and all its files. The copy can be made at any point and then can be edited, added to and improved upon all without affecting the master branch. See the diagram below:

Git Branches Diagram

Why take a branch? You may want to take your project in a different direction while other people work on the master version, or maybe you want to release a version of your work while continuing to improve on the master version.

You can swap from one branch to another, and even merge branches together.

 

Creating your first branch

Back in the command line, make sure you are in the git-tutorial folder and type in git branch you will see a list of all the branches that currently exist like below:

Creating a Git Branch

So unless you have been doing Git things without me, you only see one branch, called master. We will now add another one called experimental like this git branch experimental.

Adding a new Branch

Now if you type in git branch again, you will see two branches. You might notice that one has an asterisk and is green, this is the branch we are currently using.

Show list of branches

OK so we have two branches, but they have the same files so even if we switched from one to the other, they would both look the same.

Open up the project folder in a window and put a new file in it like this:

Adding a file to the repository folder

Now go back to the command line and add, then commit your changes, remember how to do that?

git add .

git commit


Switching from one branch to another

You have created a new branch, and then added a new file to the master branch, so you should have two branches, one of them has an extra file, the other is still the same. Want to see?

Open the folder in a window, because we are still in the master branch, we will see the extra file we added.

Now pop back to the command line and, making sure you are in the project folder, type in git switch experimental like this:

Switch from one branch to another

Now look back in your folder and you will notice that the new file we added has disappeared, where has it gone?

The file is still there, but the folder is showing us a different branch, use the switch command to switch back from experimental to master and you will see the file has returned.

Go ahead and create as many branches as you like and change the files up in any way you want. They can be changed, broken, ruined, if it goes wrong we will always have the original.

If yo forget which branch you are in, remember the git branch command, it will show you all your branches and have an asterisk next to the one you are in.

 

Merging branches together

What happens when you have been using a branch to experiment with an idea, and now you want the branch to stop being an experiment and for it to be the master branch, or maybe you have a whole bunch of branches to merge together?

We added a new file to one of our branches earlier, if we merge those two branches together, the new file will be in both branches, let’s do that now.

Open your project folder and take a look at the files, switch from the master branch to the experimental and watch how the files change. You should have the master branch with two files called new.odt and work.odt, and the experimental branch with only one file called work.odt.

Fire up the command line, get into the right folder:

  • Type in git switch experimental to make sure you are in the experimental branch

  • Now merge master and experimental like this git merge master

  • You will now see the following

Merging Git branches together

If you switch from one branch to the other, you will now see that although both still exist, they share the same files.

 

Conflicts

Adding or deleting files like that is easy, merging is simply a case of copying or removing files. But what if you changed the content of a file? How would Git know exactly what you wanted to do?

We are going to run a test, go into both branches, one at a time using the switch command and open the text documents we saved in each one, now change the content of the files a little, add some text, delete some text, whatever you like. Make sure you make different changes to each file so they are no longer identical.

Did you do it?

Did you remember to git commit your changes? If not go do it all again, I’ll wait.

Now run your git merge master command again, oh no! You have an error! The files are all different, but still have the same names, Git doesn't know which one you want.

What you have to do now is go through the files and see why they are different, decide which changes you want to keep and which you want to get rid of, then make those changes so that the files are identical, or why not rename a file if you want both versions.

When you have done this, go for the merge again, if there are no conflicts, both branches will be the same.

 

Deleting a branch

So you fixed your conflicts and merged the branches, now you would think merging two things means that there is now only one thing rihght? Like two lanes on a road merge to one, but Git doesn't mean this, Git means that the content is merged, but you still have two branches. So basically you now have two branches with the same files.

Let’s delete a branch now like this git branch -D experimental. Giving your branch the big D gets rid of it completely, and it doesn't even ask you if you are sure, that's some uncompromising D.

Here it is in real life:

Deleting a Git branch

 

What did I just learn?

  • A branch is a copy of your existing project (you start with the master branch)

  • View all your branches with git branch

  • Create a branch with git branch branchname

  • Switch from one branch to another with git switch branchname

  • Merge two branches by switching to one and using git merge branchname

  • Merge wont happen if there are conflicts Git can’t resolve, so get in there and change the files yourself

  • Delete a branch with git branch -D branchname

Tags

git
Branches
Git Switch
Git Merge
git add .

Add new comment

CAPTCHA
Answer the below question to help us fight spam

What is the opposite of light?