Short answer:
Yes, the git add
needs to be used. It not only serves to add files that are not yet part of the repository, but also to indicate which file modifications will enter the next commit.
Explaining the git add
The Basic
Suppose you have a repository with the files a.txt
, b.txt
, c.txt
and d.txt
. You have modified all the files except d.txt
, which is new. If you want to commit only the modifications of a.txt
and of b.txt
, you need to rotate:
git add a.txt b.txt
git commit -m "modificações em a e b"
Now, if you want to add a.txt, b.txt and include d.txt in the same commit, you can apply the git add a.txt b.txt d.txt
. Git itself, at the time of the commit, will show which files have been modified and which are new, if you just run the command git commit
.
Adding all modifications to the repository
When using git add .
you are basically putting all the files in the same "package", to commit them in a single commit.
Example:
git add .
git commit -m "Modificando todos os arquivos e adicionando d.txt"
Organization of commits
I usually use the git add
for each file, one by one, to better describe the modifications I made to each of them.
Example:
git add a.txt
git commit -m "removendo nomes não utilizados"
git add b.txt
git commit -m "incluindo o campo telefone na lista"
Visualizing what was added with git add
You can still see what was added in the queue to be committed from the command git status
. When applying the command git add
you can use git status
(before you run git commit) and you’ll see the files added in the queue written green.
Example:
git add .
git status
>>>
En la rama master
Cambios a ser confirmados:
(usa "git restore --staged <archivo>..." para sacar del área de stage)
modificado: a.txt
modificado: b.txt
modificado: c.txt
nuevo archivo: d.txt
Lo siento! Love Spanish, my Ubuntu is set to display in this language
Deletion of files
It is worth noting that, in addition to what has already been mentioned, the git add
can also be called to indicate the deletion of a repository file.
For example, if you delete the c.txt file and run git add c.txt
and then commit, you are indicating that this file will not be present in the repository from this commit. That is, when pulling on another machine, the same would be excluded.
Example:
rm c.txt
git add c.txt
git commit -m "removendo arquivo antigo"
Forcing inclusion of files present on .gitignore
It is important to mention that in rare cases you may need to add something that is in the .gitignore. The git add
by default does not work on files that are ignored. But, if you want to add one or more files to a specific commit, you need to add -f
as a parameter.
Thus:
git add arquivo_ignorado.pdf -f
This parameter forces the file to be added to "Stage" (the list of what goes pro commit).
Adding files from a folder
If you apply the command git add
pointing to a folder, everything that exists inside it will be added, except what is in the .gitignore
.
Example:
mkdir pasta
touch pasta/a.txt pasta/b.txt
git add pasta/
git status
>>>
En la rama master
Cambios a ser confirmados:
(usa "git restore --staged <archivo>..." para sacar del área de stage)
nuevo archivo: pasta/a.txt
nuevo archivo: pasta/b.txt
Undoing a git add
You can undo one git add
with the command git reset
. I will not go into detail, because we already have this content on How to undo a "git add" before a commit? here on the website.
Modifications after the git add
If you apply the git add
in the archive a.txt
, as in our example above, and after that, make a new modification, only the previous modification will be "saved" to the next commit.
Example:
echo "modificação "> a.txt
git add a.txt
echo "modificação de novo"> a.txt
git status
>>>
En la rama master
Cambios a ser confirmados:
(usa "git restore --staged <archivo>..." para sacar del área de stage)
modificado: a.txt
Cambios no rastreados para el commit:
(usa "git add <archivo>..." para actualizar lo que será confirmado)
(usa "git restore <archivo>..." para descartar los cambios en el directorio de trabajo)
modificado: a.txt
Note that in addition to the a.txt
be added to the list of files that will be commited, it appears again in the list of modified files. This is because, as stated earlier, each git add
saved in memory the changes at the time it was called.
This can be useful in case you have made a modification and wish to send only what was done before, without capturing the changes after you have called git add
.
If you want to add the file a.txt
after you have updated it, you need to call again git add a.txt
.
Additional readings
git-add basically adds the "list" items of things that will be part of the commit. So, I usually always apply a git add to the files I want to commit. This reply helps one to better understand why it is important to function like this.
– Wallace Maxters
Hello, have any of the answers helped you? If yes, consider ticking the button in the answer accepted.
– Wallace Maxters