Git part 4 - branches

Hi.

Last note was short but today I have something more… difficult? But only in theory. In practice it is simple and it make project more orderly. Branching today.

What is it branching? It is seperation developing part of project from other developing parts of project and from master branch, means the best version of our code. Imagine a tree. Tree have a trunk and branches beginning on trunk. Trunk is some like the oldest and the strongest branch on tree. In git project master branch is somethnig like trunk. Why I named it master? It’s a nomenclature. In this branch is the most advanced part of application, which contain only well-worked modules. It’s somewhat complicated. Compare it to prepared application. On branch master you pull only this part of application which work on production. Other branches contains now implementing modules. Git is like a real tree so it’s possible to make new branches from another without limitation.

If it is still complex for you let me give you another example. I created website for my client and implement it on production. Code of my application in git repository I pulled on master branch. Customer give me new task – make new tab with shop. I create new branch – shop and work on it. In the meantime my customer want have changes on one of existing tab. It is pretty important so I abandoning shop for a while, and make new branch. On this branch I do what I need, and in this same time my graphic designer make new branch from shop and make front for it. When I finished with changes I rebase my branch with master and back to work with shop. When I finished shop have to merged my code with front (two branches) and merge all project with master. Pretty simple, is it?

So, now I show you how to do it with git. It is simple. Just use a command

git checkout branch_name

In order to create new branch you can use command

git branch new_branch_name

On the new branch you find code like on actually branch. Remember if you use new branch in this way you don’t switch on new branch automatically. You have to switch on it using above command. If you want get away with it use

git checkout –b new_branch_name

Of course you can delete some branch. If you want to do this use -d option

git branch –d branch_name

If you don’t remember names of created branches you can check it using following command

git branch –v

The esiest way of merge branches is using merge function on main branch. If our code is useless we can use checkout. This function let us back all changes to level of other branch

git merge branch_name
git checkout branch_name

Remember that always when you rebase branches and you want to delete useless branch you have to make it with force option using -D instead of -d

Of course on the remote repositories you can push all branches. If you want push changes from one branch only, use it name behind push command.

git push origin branch_name

Download branches is a little more complicated. You can do it in two ways. First with using fetch function. It let get branches but you can’t add your changes on it. Second with pull will allow you to do it.

git fetch origin git pull origin branch_name

Earlier I mentioned that the easiest way for merging two branches is merge function. I didn’t tell any more cause it is not very often using function. More advanced and better I think is rebase.

git rebase branch_name

If two programmers changes same files, that then who rebase last is responsible for well application work. It mean that is responsible for code which should remain. In few words git search changes commit by commit. If wherein file is some change (after created new branch) git break rebase and display file which have to be reviewed. Additional git marks old and new part of code in file. User which rebase code decide which remain. After improve changes do command

git rebase --continue

Commit by commit. In large project you should commit responsibly, cause if you make a lot of commits in small part of project you will change the same files in each rebase step.

After rabase push branch on remote repository only with --force option.