Gita

Git Add All Modified Files

Git Add All Modified Files
When you are dealing with Git add, you have multiple options to add all modified files. Let's look at a few scenarios to understand the possibilities.

Let's initialize a new project.

$ mkdir project
 
$ cd project
 
$ git init
Initialized empty Git repository in /Users/zakh_eecs/_work/LearnGIT/git_add/project/.git/
 
$ echo "New Project" > ReadMe.txt
 
$ git add ReadMe.txt
 
$ git commit -m "Initial Commit"
[master (root-commit) 47b9af1] Initial Commit
1 file changed, 1 insertion(+)
create mode 100644 ReadMe.txt

In this project, we have added a ReadMe.txt file. We used the “git add” command to add the ReadMe.txt. The add command is not only for adding files. It also adds any file modification. For this tutorial, we will only add and delete files to keep it simple. But think of the add command as adding changes to the staging area. Then, you have to use the commit command to make the changes official.

When you are dealing with a lot of files and folders, it's difficult to individually add each change. So you can use the following commands:

$ git add .
$ git add -A

Let's look at how the two commands behave:

$ touch a.txt b.txt c.txt
 
$ git add .
 
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD … " to unstage)
 
new file:   a.txt
new file:   b.txt
new file:   c.txt
 
$ git commit -m "Add a.txt, b.txt, c.txt"
[master 9ca90fc] Add a.txt, b.txt, c.txt
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
create mode 100644 b.txt
create mode 100644 c.txt $ touch x.txt y.txt z.txt
 
$ git add -A
 
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD … " to unstage)
 
new file:   x.txt
new file:   y.txt
new file:   z.txt
 
$ git commit -m "Add x.txt, y.txt, z.txt"
[master 8af8c12] Add x.txt, y.txt, z.txt
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 x.txt
create mode 100644 y.txt
create mode 100644 z.txt

Both options seem to work the same.

To investigate further, let's create a situation where we add something at the root level of the working directory and then add more files in a folder:

$ touch 1.txt
 
$ mkdir new
 
$ cd new
 
$ touch m.txt n.txt o.txt
 
$ git add .
 
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD … " to unstage)
 
new file:   m.txt
new file:   n.txt
new file:   o.txt
 
Untracked files:
(use "git add … " to include in what will be committed)
 
… /1.txt

Notice Git didn't add the 1.txt file in the higher level folder.

If we created a folder called nested with d.txt file and use the git add. command again, we see that o.txt has been added but 1.txt is not added yet.

$ mkdir nested
 
$ touch nested/d.txt
 
$ git add .
 
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD … " to unstage)
 
new file:   m.txt
new file:   n.txt
new file:   nested/d.txt
new file:   o.txt
 
Untracked files:
(use "git add … " to include in what will be committed)
 
… /1.txt

Now let's use the git add -A command:

$ git add -A
 
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD … " to unstage)
 
new file:  … /1.txt
new file:   m.txt
new file:   n.txt
new file:   nested/d.txt
new file:   o.txt

Now, 1.txt in the folder has been added to the staging area.
Here's how the folders look

project
|--1.txt
|--ReadMe.txt
|--a.txt
|--b.txt
|--c.txt
|--x.txt
|--y.txt
|--z.txt
'-- new
|--m.txt
|--n.txt
|--o.txt
'-- nested
|--d.txt

So, when you are using “git add .” command, it will add all the changes from that level. But when you use “git add -A” option it will look for modifications throughout the module and add them.

Conclusion

Git add command provide powerful ways to add modified files. You can use your codes natural directory hierarchy to control what gets added.

Further Study:

  • https://git-scm.com/docs/git-add
  • Git: Learn Version Control with Git: A step-by-step Ultimate beginners Guide
  • Version Control with Git: Powerful tools and techniques for collaborative software development
  • Pro Git, 2nd Edition
5 najlepszych ergonomicznych myszy komputerowych dla systemu Linux
Czy długotrwałe korzystanie z komputera powoduje ból nadgarstka lub palców?? Cierpisz na sztywne stawy i ciągle musisz uścisnąć dłonie? Czy czujesz pa...
Jak zmienić ustawienia myszy i touchpada za pomocą Xinput w systemie Linux?
Większość dystrybucji Linuksa jest domyślnie dostarczana z biblioteką „libinput” do obsługi zdarzeń wejściowych w systemie. Może przetwarzać zdarzenia...
Remap your mouse buttons differently for different software with X-Mouse Button Control
Maybe you need a tool that could make your mouse's control change with every application that you use. If this is the case, you can try out an applica...