Git Add All Modified Files

28/12/2020
Git
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 <file>…" 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 <file>…" 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 <file>…" to unstage)
 
new file:   m.txt
new file:   n.txt
new file:   o.txt
 
Untracked files:
(use "git add <file>…" 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 <file>…" to unstage)
 
new file:   m.txt
new file:   n.txt
new file:   nested/d.txt
new file:   o.txt
 
Untracked files:
(use "git add <file>…" 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 <file>…" 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:

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Configure Git Server with HTTP on CentOS 8

In this article, I am going to show you how to configure Git Smart HTTP server on CentOS 8 for hosting your Git repositories...
29/12/2020

How to Squash Git Commits

How to Squash Commits in Git to Keep Your History Clean When you are working with Git, it’s a good idea to commit often,...
28/12/2020

Git Branch Basics

Basics of Git Branching The ability to easily branch is one of the best features of Git. Creating branches in other version...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024