Git Version Control on Linux: Commits, Branches, and Repositories
Version control in Linux with git of the source code during software development is important, even for those who develop software without collaboration from 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 various 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, since 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 to understand 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. Usually it is a regular directory on the machine;
- Index: is a hidden directory that is in the working directory (workspace), with the name ".git". Git maintains an index of the workspace files, with checksum, timestamps, etc. In addition to the index, git maintains a compressed copy of all versions of all files in the form of an object.
- Local repository: maintained inside the .git directory. Contains the history of each project file, in the form of a snapshot (an image), at each "commit" action.
Create a New Project with Git
To create a new project and version it with Git, you need to follow these steps:
First create a directory using the mkdir command:
$ mkdir projeto$ cd projeto
Then you can initialize the directory to use Git, with the init command:
$ git initInitialized empty Git repository in /home/uira/projeto/.git/
Initialization creates the hidden directory .git:
$ ls -1 .git/branchesconfigdescriptionHEADhooksinfoobjectsrefs
Configure a project with git
Then you must change the user and email used in Git, to enable tracking of changes, 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 save the identification 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 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
Clone a Project from GitHub
If you are not starting a new project, you can clone an existing project on GitHub.
To clone a project from a repository on GitHub (or another repository), you need to copy the project URL.
Github |
The clone command can be used to clone a project from a remote repository:
$ git clone https://github.com/uiraribeiro/aulalpi.git aulasCloning into 'aulas'...remote: Enumerating objects: 23, done.remote: Total 23 (delta 0), reused 0 (delta 0), pack-reused 23Unpacking objects: 100% (23/23), done.
This way, all files from the latest version will be copied to the newly created working directory called "aulas".
Add files to the git Index
Every time you add a file to the project, or change a file, you need to send it to the index.
To add files to the index, you must use the "add" command. It requires a parameter, which can be the file name, or a dot "." or asterisk "*" to designate the entire directory tree of the Workspace.
$ git add *
The "add" command produces no output.
If you do not want the "add" command to add a certain directory to the index, you should create a file called .gitignore in the root of that directory.
Add Files to the local git Repository
After adding the files to the index, you can create a snapshot (an image) of the added or changed files in the local Git repository with the "commit" command:
$ git commit -m "alteracoes" [master a5a18f7] alteracoes 1 file changed, 2 insertions(+)
The "-m" option of commit writes a comment in the repository, with observations 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, so the user can describe the changes made.
Viewing the git Change Log
The log command can be used to view the changes in the repository.
$ git logcommit a5a18f7cff53e0c3b4ea6ecfd4d53f704652ba79 (HEAD -> master)Author: Uira Ribeiro <[email protected]>Date: Sun Jan 12 16:58:14 2020 -0300 alteracoescommit 2323684b1a8e861250a4250dec8c400e0ce7ca6d (origin/master, origin/HEAD)Author: Prof. Uirá Ribeiro <[email protected]>Date: Fri Apr 27 18:25:30 2018 -0300 first commit
Pushing from the Local Repository to the Remote git Repository
Once the project files are ready, you can share the changes from the last commit with the remote repository, through the push command.
The push command requires that you have a username and password on the remote repository:
$ git pushUsername for 'https://github.com': uiraPassword for 'https://[email protected]': Enumerating objects: 5, done.Counting objects: 100% (5/5), done.Delta compression using up to 2 threadsCompressing objects: 100% (3/3), done.Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done.Total 3 (delta 2), reused 0 (delta 0)remote: Resolving deltas: 100% (2/2), completed with 2 local objects.To https://github.com/uiraribeiro/aulalpi.git 2323684..a5a18f7 master -> master
This way, it enumerates the files that were changed, added and deleted, compresses, and sends to the remote repository.
Updating the local git Repository
To update the repository and local Workspace with the latest changes from the remote repository, you can use the pull command:
$ git pullAlready up to date.
If there are no changes, git will do nothing.
Figure 57 illustrates the basic Git versioning process:
Figure 57 - Git |
Creating a Branch with git
A branch is a branching 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 their own branch), and then join into something that works, merging the different branches.
This way, the goal of creating a branch is to organize the ability to merge the different branches through a merge to the parent project.
The branch also allows new software to emerge from existing projects. When this happens it is called a fork. For example, MariaDB is a fork of MySQL.
Git automatically creates a main, or parent, branch called "master".
To create a project files branch, you can use the "branch" command to create a branch:
$ git branch livro
In this example, a branch of the project aula was created, called "livro". Git does not use the new branch until you do a checkout:
$ git checkout livroSwitched to branch 'livro'
From this moment on, all files added with the "add" command and sent to the local repository with commit, will be recorded in the livro branch.
Merging a Branch with git
Once you want to merge the files from a branch to the original project (master branch), you can use the merge command.
To do this, you must first checkout to the master branch:
$ git checkout masterSwitched to branch 'master'Your branch is up to date with 'origin/master'.
You can confirm which is the working branch with the branch command:
$ git branch livro* master
The asterisk (*) indicates which branch you are currently working on.
To merge the livro branch with the master branch, the merge command is used:
$ git merge livroUpdating a5a18f7..5b7568dFast-forward arquivo | 1 + 1 file changed, 1 insertion(+) create mode 100644 arquivo
Conclusion
Git is a powerful package manager, used even in the versioning of the Linux kernel. Surely the most used for version control in the programming world. If you write any software, you certainly need to use Git.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.
Github
Figure 57 - Git