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

Asked

Viewed 10,525 times

7

The command git add --all It seems to me like the command git add ., but I’m not sure they’re the same thing. If they’re not, what’s the difference between them?

I am also doubtful about git add -u and git add *.

  • All of these options have in common the following: if you do not specify the path, all files are updated. Indeed, the peculiarities differ between git add --all and git add -u. Take a look at the [git add][https://git-scm.com/docs/git-add] documentation so you have a better understanding!

  • @Gabrielhardoim, the link is broken.

  • https://git-scm.com/docs/git-add tries that

  • On the question of not specifying the path I already knew, but I would like to know if there are any more procedural issues behind it. For example, put in Stage all the changes, or something like that.

  • That I can’t say for sure.. But the documentation shows the difference between --all and -u

2 answers

12


There are differences between these commands, but it will depend on the version of Git 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:

Commando New Modified Removed Explanation
git add --all X X X Puts all files (new, modified and removed) in index/Stage
git add . X X Put only new and modified files into Stage
git add -u X X Put only modified and removed files into Stage

In Git version 2.x:

Commando New Modified Removed Explanation
git add --all X X X Puts all files (new, modified and removed) in index/Stage
git add . X X X Puts all files (new, modified and removed) in index/Stage¹
git add -u X X Put only modified and removed files into Stage

I’m also doubtful about git add -u and git add *

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

1. The git add . adds only new files that are in the current directory. If you have a new directory, the git add -A will add this directory to the Stage but the git add . won’t do it.

  • 1

    Thank you! That’s what I was looking for!

  • What is the reason for the negative?

  • I did not deny the answer

  • @Joãopedroschmitz, no problem, it was definitely some other user. Thank you for answering! The question is for the author of the negative if he ever visits the question again.

6

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

  • git add --all: adds to staging files from the repository root through all subdirectories, and here’s the difference, no matter if you’re in the root or sub-directory.

  • git add .: using the dot, will be added to stagging only the files from the directory you are in, and the subdirectories of this.

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 directory src/Views/Home and execute:

git add .

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

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

  • git add -u, or git add --update will update stagging on files already being tracked by Git.

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

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 command git add -u, Only the About.cshtml file is placed in the staging area, the other files that are not yet in the repository continue as not tracked by Git. This command will work for both modified and deleted files.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.