
Git works in an interesting way to keep track of file changes . Although it might seem that you are looking for the changes in each of these, what you really do is carry a file called index, in which you are inserting the changes that are happening. In this way, just by reading the index file, you can know what files and what content within them has been changed.
Once we understand that concept of how Git carries the changes registered, it is when we can begin to get the real benefit of the tool, since it is at this time when we can start using the different commands to bring changes to our repository and manage them from a logical perspective.
Classification of files within Git
Before continuing with deeper points, we must see how Git classifies its files. This does not mean a classification by file type due to its extension, but by the state of the file in relation to our repository and its index .
Basically we have three types of files in GIT, each one has its own moment inside the repository, let's see what these are:
Tracked
This type of file is already added to our repository and is taken into account by the index and a file takes this state once we have added it with the following command.
git add filename
Ignored
In this classification an ignored file is not taken into account by Git at the time of bringing the changes, it is usually classified in this way to the files that undergo many changes or that are not vital for the stability of the project, for example the image files Due to its weight or maybe Database scripts we don't have to always modify it. To ignore a file you have to include its name in a file called .gitignore and add it to our repository.
Untracked
This type of file does not yet belong to our repository because we have not yet added it and it is new in the file system, usually occurs when we create a new file in an already initialized repository or when we initialize a repository in a folder that has existing files.
Practical example of file classification
Let's now see a small practical example of how we can detect file types in a Git repository, for this we must follow the following steps:
1 - We will create a new folder on our computer called Git files .
2 - Once the folder is created we will enter it from our console and there we will execute the git init command and then we will git status to see the status of our repository, let's see what the application of the previous commands looks like:
3 - When we have completed the previous step we will already have a Git repository initialized and ready to work, then we can create a new file in that folder and we will again make git status to see the change, we must have our new file under the untracked classification.
4 - We will repeat the previous step and create a new file, if we see the result of doing git status again we will count both files, let's see:
5 - Now we are going to create a new file called .gitignore, notice the point before the file and inside we will place the name of one of our previous files, we make git status again and we will see that now we only get the file that is not in he .gitignore and the .gitignore file we just created:
$config[ads_text5] not found6 - Then we will do a git add. in order to add all our files and finally we will execute a git commit -m "initial commit" with it by adding our files to the repository, if we make a change in the file that we do not place inside the .gitignore and save it, if we execute git status again we will see to a file in status or classification tracked .
The git add command
In the previous example we could see the use of git add and maybe we can think that it is another command of our tool but this is of utmost importance, it is the one that allows us to add a file to our repository if it does not already exist in it and we also allows you to add the changes that have happened in an existing file in our repository.$config[ads_text6] not found
How to use?
It has some ways to be used, the most common is to make git add filename, where we tell you which file to add or if we can't do git add. where the dot represents all files that have been modified or are not in the repository.
$config[ads_text5] not foundIt is very important that after making any changes in the repository we add the files with git add, since otherwise we will not be able to save our changes, in addition to being creating different versions of the file which can lead us to a possible future conflict.
Use of git add
Within our folder Git files that we created in the previous example of the tutorial, we are going to add a new file that we will put new File3 and then in the existing file that is not in it .gitignore we will make a change.
What we want to achieve with this is to test how to use our git add command, let's see what this looks like in our command console:
$config[ads_text5] not foundHaving followed the instructions above, we must have come up with something like the above, where we are shown a modified file and a completely new file in the repository.
Now we are going to add the new file to the repository, but we will not do it with the existing file or the one we have previously modified. To do this we just have to do git add filename . Then we will do git status . Let's see:
As we can already notice our repository takes into account the file that we have added with git add, this is the way in which we can basically work on the changes of our files.
$config[ads_text6] not foundDelete repository files
The next action we need to know is to delete the files from our repository, since it is very common that we have created something by mistake or we are simply tidying things up within it.
There are two things to take into account, we can delete the index file from our repository but keep that file in the system of our folder, so if we do a git status we will see it again available. Or if we cannot delete the file from both our folder and the index of our repository, for this we can use the git rm command.
The git rm - -cached command
When using the rm command with the added option of cached, what we do is delete the file in question from the index, however we will keep it on our computer, this command is used a lot when we do not want to add said file to our repository yet but we need to save The other changes.
To use it, we simply do the command when we have already added a file with git add, let's see what this looks like in our command console:
We noticed that the new file File3 that we had added to our repository is now missing and has the untracked classification.
The Git rm command
Now let's see how to use the git rm command, this command is much more powerful since it directly removes the index and folder file, that is why we must be careful at the moment we decide to use it in our repository, it is very likely Once applied we cannot recover the change.
Let's see in the following example how it works when we apply it to a file, in this case we will add new File3 with git add and then we will apply on this git rm :
We see that when we do it directly, git shows us an error and asks us to do a forced deletion when adding the -f parameter to the instruction, this is due to the importance of the change, we will finally make a git status and we will notice that this file disappeared from our repository in its entirety.
$config[ads_text5] not foundMore from .gitignore
We could see that we can add a specific file to our .gitignore file, however when we work in an environment where we handle hundreds or maybe thousands of files it is not very practical, that is why we can use patterns.
A pattern will allow us to indicate to Git that a file that meets the sequence of characters or expression must be ignored, with this we can indicate specific extensions, either throughout the project or within a particular folder. Let's look at an example of this.
* .webp will ignore all the .webp files of our project, but if we want to keep the trace of one in particular we should only add:
! filename.webpThat simple we have a strong and complex structure that allows us to keep our repository organized.$config[ads_text6] not found
With this we finalize this tutorial, we have seen extensively the way in which Git carries or handles changes in our files, it is important to master this matter, since with this we can work more efficiently in equipment environments where Git is handled As version controller.
Articles