Git is one of those tools that, at first, seems complicated to learn; we can reach this program, in unsuspected ways, for example, who has not been, ever, in class, creating folders for each advance of an “X” project (so as not to ruin the already written code), and suddenly. The teacher on duty arrives, mentions something about version control or VCS (Version Control System), which, in some students, can cause curiosity and prompt them to research on their own. Those who are dissatisfied with learning will search and find different tools such as SVN, TFS, Mercurial, Git, among others. In most cases, the enormous acceptance that Git has among the developer community, the amount of free and free services that exist on the internet, platforms such as GitHub, Gitlab and Bitbucket to share software code; and many companies that share their free software projects, for example, Bootstrap, NodeJs, JQuery, Reddit, Angular, Vue.js, Nba Go; the SDK of Facebook, Google, Amazon, among others. Given so much evidence of use by these companies, many choose to be part of this community, perhaps this is the reason why you are reading this article, and overcome the challenge of learning about this tool, which is a great challenge to face and achieve in a not very long period of time.

During this learning, we can find very important projects for the software world that were public and free on GitHub.com; starting with the source code of Git, made by many collaborators around the world led by Linus Torvalds, the creator of the Linux Kernel; companies like Google with Tensor Flow that, to personal opinion, is one of the best bookshops of Machine Learning; Facebook with React (excellent tool for front-end development); Nasa with theft creation projects, cryogenic systems, climate simulators (Interested to visit: https://code.nasa.gov/. Anyway, we can pass an entire article mentioning all the companies that use this tool.

Now let’s learn Git.

What is it?

It is a VCS of high efficiency and reliability of the maintenance of versions of applications when they have a large number of source files.

Previous knowledge
What is a version control system (VCS) ?: A VCS works by creating a project directory called REPOSITORY in which it manages versions of every change made in the code, but a VCS does not do it automatically, we have to specify when a code is ready, this is known as COMMIT, which allows to have a detailed history of changes of the entire project.

History

It had its first beginnings when Linus Torvalds developed the Linux kernel, an open source operating system, initially DVCS was used that was proprietary BitKeeper, in 2005 this relationship of community collapsed and the tool stopped being free; It was when they started their Git development with the lessons learned. The following objectives were established:
● Speed
● Simple design
● Strong support for non-linear development (thousands of parallel branches)
● Fully distributed
● Able to handle large projects (such as the Linux kernel) efficiently (speed and size of the data)

Since its birth, in 2005, it has evolved and matured to be easy to use and now it is one of the main tools for development, being a necessary skill when looking for a job or cooperating with the open source community.

How Git works

In order for Git to meet its main objectives and goals regarding its architecture, it had to be based on lessons learned modeling the information in a different way as well as its storage. Git models your data as a set of screenshots or snapshots of a mini file system. every time a change is confirmed (commit), Git captures the appearance of the files at that moment and saves a reference, if a file was not modified, it only saves a link to the previous file that has already been stored.
Giving it a huge advantage because almost any operation is done locally.

Work space

Git works basically modifying the state of the files, this is known as the three states, it is the basis to understand these three states to proceed without problems. The three main states in which you can find your files are: confirmed (committed), modified (modified), prepared (staged).
● Confirmed means that the data is stored securely in your local database. It is stored in the repository (hidden folder .git).
● Modified means that you have modified the file but you have not yet confirmed it to your database. It is known as the preparation area (staging area or index).
● Ready means that you have marked a modified file in its current version to go in your next confirmation. It is known as a working directory.

When changing to the confirmed state it is stored in the local Git database, saving a unique identifier of 40 hexadecimal characters (0-9 and af) is known as Hash SHA1 and looks like this: (24b9da6552252987aa493b52f8696cd6d3b00373), this It can be simplified in a practical way by selecting only the first 6 digits to perform actions.

Basic commands:

● Global Configuration:

git config−−global user.name “Manuel Guarniz”
git config−−global user.email mguarniz@example.com

● Local Configuration (Only in the directory):

git config user.name “Manuel Guarniz”
git config user.email mguarniz@example.com

● Start / clone a repository

git init
git clone git://github.com/schacon/grit.git

● Repository status

git status

● Change the status of the working directory to the staging area.

git add README (Changes status only to a file)
git add . (Changes status to all files in the working directory)

● Change the status of the staging area to the working directory

git reset HEAD README (Changes status only to a file)
git reset HEAD (Changes status to all staging area files)

● Make a confirmation (commit)

git commit −m “specify a message, it’s obligatory”

● Connect with a remote repository

git remote add origin git://github.com/schacon/grit.git

● Upload and download confirmations (commit) to the remote repository

git pull origin master (Download the confirmations of the master branch)
git push origin master (Upload confirmations to the master branch)

Branches and tags

● Create branches

git branch new_branch (Only creates the branch from where we are)
git checkout−b new_branch (Create the branch and immediately locate it)

● Merge branches

git merge new_branch (Situate yourself in the desired branch, to bring changes from the other branch.)
git merge−−abort (If a conflict is generated, we can abort the merge)

● Delete branches

git branch d new_branch (Delete obsolete or unchanged branches)
git branch D new_branch (Force erasure)

● List branches

git branch (branches in local)
git branchlist (List all the branches)

● Situate in another branch (checkout)

git checkout new_branch

● Create tags

git tag a v1.0 m “detailed description, it is mandatory”

● Delete tags

git tag d v1.0

● Upload branches and labels to the remote repository

git push origin new_branch (branches)
git push origin v1.0 (tags)
git push origin −−tags (all tags)

● Upload deleted branches and tags to the remote repository

git push origin: new_branch
git push origin: refs / tags / v1.0

● Download branches and tags from the repository

git pull (Situate in the master branch to execute command)
git pull origin new_branch (Always located in the branch to update)

Manipulating the story

● Recommendation
At any time you may want to undo something. Be careful, sometimes it is not possible to recover something after you have undone it. This is one of the few areas where Git can lose part of your work if you make a mistake.
● Modify the last confirmation: Useful if you want to change the message or forgot to add changes or files to the confirmation (do not do with commits already uploaded to the remote repository).

git commit−−amend−−m “new message”

● Undo a modified file: Useful to discard changes of the working directory, once done, the changes can not be recovered.

git checkout −− /folder/file.txt
git checkout . (Discards all changes in the working directory)

● Undoing confirmations
SOFT: The SHA1 hash of the commit is specified, which means it is placed in the specified confirmation, and the others (the most current one discards them and the changes of these commits are passed to the staging area)

git reset−−soft SHA1

MIXED: The SHA1 hash of the commit is specified, which means it is located in the specified confirmation, and the others (the most current one discards them and the changes of these commits are passed to the working directory)

git reset−−mixed SHA1

HARD: The SHA1 hash of the commit is specified, which means it is placed in the specified confirmation, and the other confirmations (the most current one discards them completely)

git reset−−hard SHA1

● Reverse confirmations: Execute the opposite action of the confirmation by restoring the repository to as it was before the confirmation.
git revert SHA1

git revert−−no-commit SHA1> (if you’re going to reverse more than one commit)
git revert−−continue (Confirm reversion, sometimes conflicts appear when more than one confirmation is reversed)
git revert−−abort (If you do not want to continue, abort and reset)

Shortcuts

● Create

git config−−global alias.unstage ‘reset HEAD –‘

● Use

git unstage /folder/file.txt (Practical example according to the command)

● Lists

git config−−global –get-regexp alias

● Recomendación

git config −−global alias.co “checkout”
git config −−global alias.br “branch”
git config −−global alias.cm “commit -m”
git config −−global alias.unstage “reset HEAD −−
git config −−global alias.s “status”
git config −−global alias.alias “config −−global −−get-regexp alias”

Resolving conflicts

● When carrying out a merger or downloading changes from a remote, the same line of text may have been written in more than one confirmation, these events are conflicts in which Git does not resolve them automatically and gives us the facilities to resolve it, after resolving you have to add the file to the staging area and perform the commit. A conflict in a file is displayed in the following way.

function hello() {<<<<<<< HEAD

print(‘hello world’)

=======

print(“hello world”)

>>>>>>> new_branch

<span”>}

Two sections are observed, separated by several consecutive equality signs (=======), from that point upwards it becomes the HEAD (Current position that mostly comes to be the last commit), then down indicates that the conflict was made when we tried to merge with new_rama, now we erase the code that we want to discard, being in the following way ready to add to the staging area and make the confirmation (commit).

function hello () {
print(“hello world”)
}

Useful tools

● Resolve conflicts
KDIFF3 (http://kdiff3.sourceforge.net/): It is a text editor of great use to resolve conflicts: it shows 3 sections: A: Initial code before being modified, B: It shows code that we have in the HEAD or position current, C: Changes that were tried to merge generating the conflict.
Two sections are observed, separated by several consecutive equality signs (=======), from that point upwards it becomes the HEAD (Current position that mostly comes to be the last commit), then down indicates that the conflict was made when we tried to merge with new_rama, now we erase the code that we want to discard, being in the following way ready to add to the staging area and make the confirmation (commit).
function hello () {
   print (” hello world ‘)
}

● Free remote repositories
Github (https://github.com): Free and unlimited public repositories, from private ones under membership payment.
Gitlab (https://gitlab.com): Free and unlimited public and private repositories, without losing functionality (alternative to Github)
Bonobo Git (https://bonobogitserver.com/): If you want to host your own Git server in IIS, you can do it quickly with Bonobo.
● Bibliography:
Updated Spanish book of Git: https://git-scm.com/book/en/v2

Summary:

  1. Clone a repository
    git clone URL_REPOSITORY_REMOTE

        2. We make a change and see the status of our local repository

     git status

       3. Add working directory files to the staging area

     git add .
     Git add NAME_FILE

      4. Make confirmation of the changes (commit)

    git commit -m ‘MESSAGE’

      5. Download changes if necessary (recommended in teamwork)

     git pull origin BRANCH

      6. Upload the changes to the remote repository

     git push origin BRANCH

Github, provides a quick help with the basic and intermediate level commands to be able to start using and get familiar with git

https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf