We will learn in this post a brief introduction to Git. Git is a code management tool that provides a simple way to create and track codes of any project in a collaborative environment, where a programmer of a project can develop their code segment simultaneously without affecting performance from others, as well as reducing code dependencies.
Its operation has inspired the creation of tools and sites such as Github, Gitlab, etc. that have improved the programming industry, using the same mechanisms to manage the management and cleaning of codes. This has also allowed the dissemination of open source projects (open source) and even the management of codes with a proprietary (or private) source.
As an introduction to Git, first we learn how to use git as a tool to manage our codes, regardless of whether it is a personal project or our company, we must learn the basic commands for its use and thus be able to take advantage of all the benefits it offers us.
By studying the following commands well, you will be able to avoid common errors in the handling of Git, that its mishandling could sometimes lead to catastrophic errors such as replacing good code and even losing code.
If we have not installed git on our local computer, we can execute the command in superuser mode:
yum install git # for Fedora and CentOS apt-get install git # for Ubuntu and Debian
Clone a package
git clone url_origen
This command is used to clone a package from its source url, download a copy of the package from the url_origen to the current directory, these packages are easily identifiable with the extension .git
Creation of a branch (or branch)
In order to simplify code management and avoid conflicts, it is recommended that each developer work on their own branch. A branch is a copy of the code that originally contains the main project code (master) and when modifying or adding features to it, the master does not change.
To list the existing branches in our project, we write:
git branch -l
To change to an existing branch, we write:
git checkout filename
And once in the desired branch, we create a new branch with:
git branch new_branch
For example if we want to create a new master-based branch, we write
git checkout master
git branch new_banch
Most common commands in Git
Update existing branches
To download the latest changes in the structure of the project, we fetch
Incorporate changes from a remote branch to the local
To incorporate the most recent changes from a remote branch to a local branch, we write
git pull remotebranch localbranch
Or we can do the same with the git merge command, using the sequence of these two commands:
git merge remotebranch/localbranch
Incorporate local changes to a remote branch
This process must be done every time we want to integrate our local changes into the remote branch.
Before sending the changes to the remote branch, we must update the changes in our local branch, if we want to add all the changes made, as well as the other files (as long as there are no remote changes that are in danger of being ironed, if this is the case, first make a git pull remotebranch localbranch), we use the following commands to add the changes to our local branch:
git add -A git commit -m "my changes"
Once located in our local branch, we upload them to our remote branch using the following command:
git push remotebranch localbranch