Almost every other developer uses git for source control. But hardly a few of them know subtle difference between git add . and git add -A. Let’s see what help document says.

-u, –update
Only match against already tracked files in the index rather
than the working tree. That means that it will never stage new files, but
that it will stage modified new contents of tracked files and that it will
remove files from the index if the corresponding files in the working tree
have been removed.

If no is given, default to “.”; in other words, update all
tracked files in the current directory and its subdirectories.

-A, –all
Like -u, but match against files in the working tree in
addition to the index. That means that it will find new files as well as
staging modified content and removing files that are no longer in the
working tree.

If this explanation doesn’t help your then following example will definitely help you to understand the difference. Take the following git repository as an example.

gittest
├── changeMe.txt
└── deleteMe.txt

Assume that changeMe.txt and deleteMe.txt is already present in the git repo. Execute the following command and check the status you will understand the difference.

/>echo "New file added">newFile.txt
/>echo "Added one more line">>changeMe.txt
/>rm deleteMe.txt
  # On branch master
  # Changes not staged for commit:
  #   (use "git add/rm <file>..." to update what will be committed)
  #   (use "git checkout -- <file>..." to discard changes in working directory)
  #
  #	modified:   changeMe.txt
  #	deleted:    deleteMe.txt
  #
  # Untracked files:
  #   (use "git add <file>..." to include in what will be committed)
  #
  #	newFile.txt
  # no changes added to commit (use "git add" and/or "git commit -a")
/>git add .
/>git status
  # On branch master
  # Changes to be committed:
  #   (use "git reset HEAD <file>..." to unstage)
  #
  #	modified:   changeMe.txt
  #	new file:   newFile.txt
  #
  # Changes not staged for commit:
  #   (use "git add/rm <file>..." to update what will be committed)
  #   (use "git checkout -- <file>..." to discard changes in working directory)
  #
  #	deleted:    deleteMe.txt
  #
/>git reset    #to reset the repo to previous state.
/>git add -u
/>git status
  # On branch master
  # Changes to be committed:
  #   (use "git reset HEAD <file>..." to unstage)
  #
  #	modified:   changeMe.txt
  #	deleted:    deleteMe.txt
  #
  # Untracked files:
  #   (use "git add <file>..." to include in what will be committed)
  #
  #	newFile.txt
/>git reset    #again reset the repo to previous state.
/>git add -A
/>git status
  # On branch master
  # Changes to be committed:
  #   (use "git reset HEAD <file>..." to unstage)
  #
  #	modified:   changeMe.txt
  #	deleted:    deleteMe.txt
  #	new file:   newFile.txt
  #

"git add -A" is equivalent to "git add .; git add -u". I always prefer "git add -A" because index all the files and you don’t miss any file. Use these commands after understanding their real affect.

Note:

  • git add -A stages All
  • git add . stages new and modified, without deleted
  • git add -u stages modified and deleted, without new

If you interested in git commands please check this post.


Hope this blog helped you in some way. If you like this blog then please share it. You can also leave your comment below. You can find Facebook page here.

, ,
Trackback

no comment untill now

Add your comment now