Branches

Work with branches is essential to have an organized project. Learn about how to work with them is one of the first things when we talk about git and versions.

So, why should I use branches?

When you create a new feature or add an update in your code, it is a good practice create a new branch, that is a copy in certain point of the master code. We do this to keep on master only the tested and refactored code.

In the image below, the master is represented with the blue color, and the branches with purple and green. The purple branch can symbolize a branch to fix a problem and the green one a new feature, for example.



Now you can safely work in your code, doing all the necessary changes without having to redo your code on master if something gets wrong, or if the code it's not in the final version yet.

You can note that in the image the master branch seems to have some commits ahead of the other branches. This happens mostly when you are working on collaborative projects. In this post we'll focus only in the branch concept, letting the idea of rebase and merge to another post.

Create and use branches

To manage your branches you can use the commands:
  1. git branch: to list your branches and also see your current branch;
  2. git branch <branchName>: to create your branch;
  3. git checkout <branchName>: to go to the branch that you want.
The first step after you clone your repository is list all your branches using the command  git branch. You'll probably see only one branch, named main, that is your master code. If you want to create a new branch you can just use git branch <branchName> and later go for your branch using git checkout <branchName>.

🧰 QGit: a great tool to manage your branches

When we start to create branches, rebase and work with remotes repositories, especially on larger projects, we can get confused about where is our branch based, if our remote repository is updated with local or where are we on main

For this I let here this tool tip, that will definitely help you. Using it you can see where are you on git tree, see all commits and have the full picture of your project. To install on your Linux you can just use (you have to be on the cloned folder to run the second command, that will open QGit): 
sudo apt install qgit
qgit &

Comments

Popular Posts