Skip to content

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:

$ mkdir project $ cd project

Then you can initialize the directory to use Git, with the init command:

$ git init Initialized empty Git repository in /home/uira/project/. git/

Initializing creates the hidden .git directory:

$ ls -1 .git/ branches config description HEAD hooks info objects refs

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:

$ git config --global user.name “Uira Ribeiro” $ git config --global user.email "[email protected]"

If the “—global” option is used, git will store the ID in a file called .gitconfig in the user’s home directory:

$ cat ~/.gitconfig [user] name = Uira Ribeiro email = [email protected]

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:

$ git remote add origin https://github.com/uira/projetoteste

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
![](https://learnlinux.com.br/editor/files/github_CompT.jpg)The clone command can be used to clone a project from a remote repository:
$ git clone https://github.com/uiraribeiro/aulalpi.git classes <br></br>Cloning into 'classes'... <br></br>remote: Enumerating objects: 23, done. <br></br>remote: Total 23 (delta 0), reused 0 (delta 0), pack-reused 23 <br></br>Unpacking objects: 100% (23/23), done.

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.

$ git add*

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:

$ git commit -m “changes” [master a5a18f7] changes 1 file changed, 2 insertions (+)

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.

$ git log commit a5a18f7cff53e0c3b4ea6ecfd4d53f704652ba79 (HEAD -> master) Author: Uira Ribeiro Date: Sun Jan 12 16:58:14 2020 00:00 changes commit 2323684b1a8e861250a4250dec8c400e0ce7ca6d (origin/master, origin/ HEAD)

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:

$ git push <br></br>Username for 'https://github.com': uira <br></br>Password for 'https://[email protected]': <br></br>Enumerating objects: 5, done. <br></br>Counting objects: 100% (5/5), done. <br></br>Delta compression using up to 2 threads <br></br>Compressing objects: 100% (3/3), done. <br></br>Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done. <br></br>Total 3 (delta 2), reused 0 (delta 0) <br></br>remote: Resolving deltas: 100% (2/2), completed with 2 local objects.

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:

$ git pull <br></br>Already up to date.

If there are no changes, git won’t do anything.

Figure 57 illustrates the basic Git versioning process:

Figure 57 - Git
![](https://learnlinux.com.br/editor/files/git_CompT.jpg)### Creating a Branch with 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:

$ git branch book

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:

$ git checkout book Switched to branch 'book'

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:

$ git checkout master <br></br>Switched to branch 'master' <br></br>Your branch is up to date with 'origin/master'.

You can confirm which branch you are working with the branch command:

$ git branch book * master

The asterisk (*) indicates which branch you are working on.

To merge the book branch with the master branch, use the merge command:

$ git merge Updating book a5a18f7.. 5b7568d Fast-forward file | 1 + 1 file changed, 1 insertion (+) create mode 100644 file

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