Linux version control with Git
Version control on Linux with git of the source code during software development is important, even for those who develop software without the collaboration of other people.
In the open-source world, whose premise is to distribute the source code freely so that it can be improved through the contribution of several developers around the world, version control is absolutely necessary.
There are several free and open source version control software available, such as CVS, SVN, and GIT.
The latter, created by Linus Torvalds, became popular because it uses free software repositories to distribute and control the source code.
Several repositories emerged, such as GitHub (acquired by Microsoft), GitLab, BitBucket, among others.
The git package can be installed on Linux using the apt, yum, or dnf package manager.
Important terms for understanding Git:
- Remote repository: is the repository hosted on a server, which will control users and version control;
- Workspace: is the local directory, which allows the developer to work on their local copy of the source code. Normally it is a common directory on the machine;
- Index: it is a hidden directory located in the working directory (workspace), with the name “.git”. Git maintains an index of the workspace files, with checksums, timestamps, etc. In addition to the index, git maintains a compressed copy of all versions of all files in object form.
- Local Repository: maintained within the .git directory. It contains the history of each project file, in the form of a snapshot (an image), with each “commit” action.
Create a New Project with Git
To create a new project and version it with Git, it is necessary to follow the following steps:
First, a directory is created using the mkdir command:
Then you can initialize the directory to use Git, with the init command:
Initializing creates the hidden .git directory:
Setting up a project with git
Then, the user and email used in Git must be changed to allow the changes to be tracked with the config command:
If the “—global” option is used, git will store the ID in a file called .gitconfig in the user’s home directory:
Now you can use the Workspace directory created to write the programs.
To add a new project to a remote repository, you must first create the remote repository through the GitHub website.
Then, you can use the remote command:
Cloning a Github Project
If you’re not going to start a new project, you can clone an existing project on GitHub.
To clone a repository project on GitHub (or another repository), you must copy the project URL.
Github |
In this way, all the files from the latest version will be copied to the newly created working directory called “classes”.
Add files to the Git Index
Every time you add a file to the project, or change a file, it is necessary to send it to the index.
To add files to the index, you must use the “add” command. It needs a parameter, which can be the file name, or a dot**.”** “or asterisk “*” to designate the entire Workspace directory tree.
The “add” command doesn’t produce any output.
If you do not want the “add” command to add a particular directory to the index, you must create a file called .gitignore at the root of this directory.
Adding Files to the Git Local Repository
After adding the files to the index, you can create a snapshot (an image) of the files added or changed in the local Git repository with the “commit” command:
The commit option “-m” writes a comment to the repository, with notes about the changes made. If the “-m” option is not used, git will open the default text editor, with the list of changed and added files, for the user to describe the changes made.
Viewing the git changelog
The log command can be used to view changes to the repository.
Author: Prof. Uira Ribeiro Date: Fri Apr 27 18:25:30 2018 - first commit ### Submitting from Local Repository to Git Remote Repository
Once the project files are ready, you can share the changes from the last commit with the remote repository using the push command.
The push command requires that you have a username and password in the remote repository:
To https://github.com/uiraribeiro/aulalpi.git
2323684.. a5a18f7 master -> master In this way, it lists the files that were changed, added and deleted, compresses, and sends them to the remote repository.
Updating the Git Local Repository
To update the repository and the local Workspace with the latest changes from the remote repository, you can use the pull command:
If there are no changes, git won’t do anything.
Figure 57 illustrates the basic Git versioning process:
Figure 57 - Git |
A branch is a branch in version control and software development management.
Basically, it allows the duplication of an object under version control (a source code file or a set of directories), so that modifications can occur in parallel along several branches.
Basically, it is a methodology that allows several developers to collaborate on the same project, with the ability to separate what each one did (each with its own branch), and then combine it into something that works, merging the different branches.
Thus, the purpose of creating a branch is to organize the ability to merge the different branches by merging the parent project.
The branch also allows new software to emerge from existing projects. When this happens it’s called a fork. For instance, MariaDB is a fork of MySQL.
Git automatically creates a main branch, or parent, called “master”.
To create a branch of project files, you can use the “branch” command to create a branch:
In this example, a branch of the class project, called “book”, was created. Git doesn’t use the new branch until a checkout is done:
From this moment on, all files added with the “add” command and sent to the local repository with commit will be saved in the book branch.
Merging a Branch with Git
Once you want to merge the files from a branch into the original project (master branch), you can use the merge command.
To do this, you must first check out from the master branch:
You can confirm which branch you are working with the branch command:
The asterisk (*) indicates which branch you are working on.
To merge the book branch with the master branch, use the merge command:
Conclusion
Git is a powerful package manager, used even when versioning the Linux kernel. Certainly the most used for version control in the programming world. If you write any software, you definitely need to use Git.
Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.
Did you like it?
Share