Hello everyone! Today I dedicate this article in particular to those who for free time or passion develop programs or applications.
I place you in front of the typical scenario:
- You are planning your application, and as you make changes you get closer and closer to the final objective of your program.
- The program is finished and working, so release the first version
- You notice that the program has a slight flaw and needs a correction
- Go rush to correct the leak after minutes if not hours of debugging
- Correct hoping that this is the cause of the leak
- Restart the program and completely crash !!
At this point, however, you have already saved the file and if you have not kept a copy of the working program it will be difficult to go back after many changes. It is precisely here that Git comes into play.
More on Git
Git is a version control software, in simple terms, it allows us to create a sort of “timeline” for every file controlled by Git.
As we develop then we can save our file safely and only when we are sure of the correct functioning we can send a commit with a personalized message that will help us to quickly recognize what changes we made in that submission.
What is the purpose of all this? Well being able to track changes to our choice allows us to easily return “back in time” to a version of the code in which everything worked, in the event that our program starts to have malfunctions and bugs.
But it did not end here! Not only does git allow you to draw a timeline on your files, but it also allows you to take the entire git-controlled project and clone it into another branch (from the English branch), which is another separate line of work . Often these branches are created to test new features parallel to the main track.
How does it work
Below I will tell you in brief the most common use of this tool:
We open the terminal
For this guide I will explain the procedure for unix and linux systems, for those who use windows I recommend installing the linux bash from the store in order to allow you to use a linux shell even in windows:
We install git
For Ubuntu, type the following command in the terminal:
sudo apt install git-all
sudo dnf install git-all
For MacOS you should install Xcode but install 7Gb of IDE only for git would be a waste, who does not want to install Xcode can instead install Github Desktop which is an excellent GUI we will discuss later to visually analyze the changes in our files.
Click on this link: Git Windows
If you have installed git correctly and during the installation phase you have not had any problems, the only thing left is to type the following command:
Now open your favorite editor and start developing your project, nothing big, just a small start to not leave the files unnecessarily empty. When you are ready to use git, open the terminal and move to the project directory using the cd command.
Now that you have initialized git to track your project, you could use the command line interface to execute all the commands but it would be very inconvenient and slow, especially if you have project deadlines, so what I suggest you do is download Github Desktop or better still GitKraken.
I consider this better because it allows us to see very clearly the progress of the project, showing all the branches and commits on a timeline, where the commits are sorted by submission date.
Both allow you to easily subdue new commits, create branches and switch between them, merge two branches and much more.
In the next article on git I will explain these details better but now we do our first commit!
Open GitKraken and open the project you are tracing with git, the interface is very intuitive, just click on File> Open Repo and select the folder of your project. GitKraken will notice that the project is under git and if you have selected the wrong folder it will warn you that git has not been initialized, so in that case start again and select the right folder.
To the right of the interface you will see a list of all the files that have undergone changes since you have initialized the project, click on Stage All to tell git to enter all the changes in the commit you are about to run, of course if you want to do two separate commits You can manually select the files to commit to.
Now that you have chosen the changes to be recorded in the next commit, you can enter a short explanatory message to understand on the fly what changes have been made in that commit, such as “correct bug in the registration form” or “optimize imports”, whatever let me know what kind of changes you have made to the project.
Click on the confirmation button at the bottom right and here you are, you will see in the central section the first your commit in the form of a sphere on the timeline that goes from the bottom to the top. and if you click on it gitkraken will show you all the changes made to the files you have chosen to record.
Click on File> Add local repo to add the project to github desktop, after which on the left side of the interface there will be all the changes made to the files since initialization.
Choose which files to insert in the next commit using the check marks on the left, insert the commit message and confirm with the button at the bottom. If you go to the “History” tab at the top you can see the commit timeline, but unlike gitkraken you will not have the interface that shows you the branches
As you change files, choose intelligently when you create a commit, do not make any changes, do it only when you are in a situation where your project is stable and does not have too many bugs, it will be easier for you to organize your work and eventually return “back in time” if something should create a flaw in the whole project.
In the next articles I will also explain some best practices to use git at best using branch and merge to separate and organize work.
If in the meantime you have found it difficult to understand or install git, or for any other questions please write us in the comments!
I hope I have been of help, see you in the next article!
Copyright © SviluppoMania. All rights reserved.