In this tutorial, we'll see what Git and GitHub are and what they're used for. Personally, I think having a clear understanding of these concepts is essential in our daily lives, whether at work or while studying.
Table of Contents
1 - What is Git?
Git is a version control system. From a developer's perspective, it's a system that stores and tracks changes to the files we modify.
Also, git is a distributed version system, which means we have a remote repository on the server and a local repository, which is stored on each developer's machine. This means that the code is both on the server and on every machine that modifies it.
It is important to include all this information, since the vast majority of companies use a version control system, commonly git, although there are others like subversion or csv.
1.1 - What is Git used for?
In a real work environment, it allows us to work in parallel with several developers. Thanks to git, we can check all code changes as well as conflicts.
When we make a change, we say we make a commit
, which includes information such as the author, date, a comment, and the files that have changed.
Here's an example of several commits I have in a library:
1.2 - Download Git
If you have Visual Studio 2019 installed, you'll have git by default on your machine, but you can still check if it's installed with the following command in your terminal (cmd)
git --version
It will tell you if git is installed; if not, you can download it from the official page for the version you need
https://git-scm.com/downloads
2 - What is GitHub?
It's a service for sharing and publishing code. Basically, it's like a social network for programmers. It uses git
behind the scenes.
We usually work with both Git and GitHub in parallel. Personally, I think it's the best combination. If for some reason you don't like GitHub's interface, you can always use others like Bitbucket.
2.1 - Create a GitHub account
To create a GitHub account, just go to their website
https://github.com/So you can create your first repository.
2.2 - What is a repository?
On GitHub, our code is divided into sections, for example, an app to upload images, an app to write posts, etc. Each of these applications is in a repository, although of course a repository may contain more than one app.
Repositories are usually organized internally by business logic.
2.3 - Create a repository
To create a repository, you just need to click the green button and you'll be ready to start working with it.
Give it a name and click "create repository."
You can also specify several things, for example:
If you want the repo to be public or private
If you want to include a README
file (these files are usually written in markdown).
Specify the .gitignore
file, which prevents files you don't want to be published from being uploaded. We usually put all temporary files, like those from the /bin
or /obj
folders, inside it.
2.4 - Using Git on our computer
To do this, we'll need to put the repository on our computer.
As mentioned earlier, on GitHub we have a copy on the server and one locally. To copy the directory, click the button that says "clone or download" and then click the icon to copy.
Next, open Visual Studio and click on clone or checkout code
And paste the URL into the location field. As you can see, you can change it or leave it as the default.
You can use the following command
git clone [email protected]:<tuCuentadeGithub>/ejemploGitHun.git
Now we already have the repository linked on our computer. Next, you can create a file, a project, or whatever you like, it also works for audio files, images, etc. It's a version control system for any type of file.
3 - Making changes
When you make a change, whether you modify, add, delete, or move a file, this change will be reflected in the version controller.
So we create a plain text file (you can use the README) and add some text to it.
As you might guess, these changes are pending to be sent to the server. To do this, there are two steps:
3.1 - Make a commit
*Note: In the video, all steps are done using Visual Studio's graphical interface. Here in the post, we'll use the command line.
The first step is to make a commit
, which is a command that tells the version control server that this file version is the current one.
To do this, using the command line or powershell, go to the folder where your file is and type the following:
git commit -m “mensaje descripción”
As you can see, the command uses git commit
and the -m
option, which allows you to write a comment. It's recommended, and should be mandatory, to add a comment when making a commit, as this allows you to quickly see what the change is about.
3.2 - Make a push
As mentioned earlier, the change is in your machine's version controller, but to move it to the server, you need to do a push with the following command.
git push
This will move the changes to the server. If another developer has made a change to the file while you've been working on it, when you push, it will indicate that there is a conflict, and you'll have to resolve it. But we'll see that in the next post.
3.3 - Incorporate changes from others
Let's suppose you went on vacation, and someone else fixed issues on your project while you were away. This means your local machine has an older version of the application.
Now you need to update the version on your machine.
To do this, run the following two commands:
git fech
git merge
which will update the application version with the latest changes.
Another option is to run both commands as one:
git pull
but there could be some issues with conflicts, so the safest is to run both git fetch
and git merge
.
If there is any problem you can add a comment bellow or contact me in the website's contact form