Difference between "git add --all", "git add." and "git add - u"

3

The command git add --all seems to equal the command git add . , but I'm not sure if they are the same thing. If not, what is the difference between them?

I'm also in doubt about git add -u and git add * .

    
asked by anonymous 30.08.2018 / 17:33

2 answers

1

There are differences between these commands, but it will depend of the Git version you are using.

As an example, in version 1.x of Git, the commands git add --all and git add . are different, but in version 2.x these two commands are the same.

See the tables below showing the differences. In Git version 1.x :

+---------------+-------+-------------+-----------+------------------------------------------------------------------------+
|               |       |   Arquivos  |           |                                                                        |
+---------------+-------+-------------+-----------+------------------------------------------------------------------------+
| Comando       | Novos | Modificados | Removidos |                                                                        |
+---------------+-------+-------------+-----------+------------------------------------------------------------------------+
| git add --all |   X   |      X      |     X     | Coloca todos arquivos (novos, modificados e removidos) no index/stage  |
+---------------+-------+-------------+-----------+------------------------------------------------------------------------+
| git add .     |   X   |      X      |           | Coloca no Stage apenas arquivos novos e modificados                    |
+---------------+-------+-------------+-----------+------------------------------------------------------------------------+
| git add -u    |       |      X      |     X     | Coloca no Stage apenas arquivos modificados e removidos                |
+---------------+-------+-------------+-----------+------------------------------------------------------------------------+

In Git version 2.x :

+---------------+-------+-------------+-----------+-----------------------------------------------------------------------+
|               |       |   Arquivos  |           |                                                                       |
+---------------+-------+-------------+-----------+-----------------------------------------------------------------------+
| Comando       | Novos | Modificados | Removidos |                                                                       |
+---------------+-------+-------------+-----------+-----------------------------------------------------------------------+
| git add --all |   X   |      X      |     X     | Coloca todos arquivos (novos, modificados e removidos) no index/stage |
+---------------+-------+-------------+-----------+-----------------------------------------------------------------------+
| git add .     |   X   |      X      |     X     | Coloca todos arquivos (novos, modificados e removidos) no index/stage¹|
+---------------+-------+-------------+-----------+-----------------------------------------------------------------------+
| git add -u    |       |      X      |     X     | Coloca no Stage apenas arquivos modificados e removidos               |
+---------------+-------+-------------+-----------+-----------------------------------------------------------------------+
  

I'm also in doubt about git add -u and git add *

The first command, obeying the table above, will add the files including those starting with . . git add * will add all files in the same way as git add . , but ignoring files that start with . .

1. git add . adds only new files that are in the current directory. If you have a new directory, git add -A will add this directory on stage but git add . will not do this.

    
31.08.2018 / 22:08
2

The git add -all and git add. commands may look the same but do very different actions:

  • git add --all : adds to staging files from the root of the repository by going through all subdirectories, and here's the difference, it does not matter if you're in the root or subdirectory.

    li>
  • git add . : Using the dot, only the files from the directory you are in, and the sub-directories of it, will be added to the stagging.

Example:

Using the following file and folder structure as an example:

.
.gitignore
src
├── Controllers
│   └── HomeController.cs
├── HelloWorld.csproj
├── Models
│   └── ErrorViewModel.cs
├── Program.cs
├── Properties
│   └── launchSettings.json
├── Startup.cs
├── Views
│   ├── Home
│   │   ├── About.cshtml
│   │   ├── Contact.cshtml
│   │   ├── Index.cshtml
│   │   └── Privacy.cshtml

Whereas only .gitignore is versioned in the repository, if you are in the src / Views / Home directory and run:

git add .

The files: About.cshtml, Contact.cshtml, Index.cshtml, and Privacy.cshtml will be added. But files from other directories will not be added, for example: HomeController.cs, which is in the src / Controllers / directory.

  • git add * : will work exactly like the previous command, adding only the files in the current folder to which the command was executed.

  • git add -u , or git add --update will do an update on stagging in files that are already being tracked by Git.

Continuing to use the previous example, after running git add . and then git commit -m "Primeiro" , there will still be files to be added to the repository; then the About.cshtml file is edited. Executing a git status , the repository state will be:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   About.cshtml

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ../../Controllers/
        ../../HelloWorld.csproj
        ../../Models/
        ../../Program.cs
        ../../Properties/
        ../../Startup.cs
        ../Shared/
        ../_ViewImports.cshtml
        ../_ViewStart.cshtml
        ../../appsettings.Development.json
        ../../appsettings.json
        ../../wwwroot/

Running the git add -u command, only the About.cshtml file is placed in the staging area, the other files that are not already in the repository remain unpatched by Git. This command will work for both modified and deleted files.

    
01.09.2018 / 22:59