top of page
Search
  • Tech Explore

Deep dive into Git Add


In this post let us discuss some common ways of using "git add" command.


The "git add" command adds a change in the working directory to the staging area.

It tells Git that you want to include updates to a particular file in the next commit.

However, "git add" doesn't really affect the repository in any significant way—changes are not actually recorded until you run "git commit".


Generic usage of git add command is :

>> git add <path>


path can be point to a directory or an individual file

path should be specified as the relative file path to the file or directory from where the command is executed.


We can specify multiple files / file paths :

>> git add <file_path_1> <file_path_2>


Git Add without Path


We cannot do a "git add" without specifying the path. User will be prompted to use "git add ." pointing to the current working directory.

"git add" command without specifying the path is equivalent to saying "git add :/". Add everything from top-level git repo folder.


Git Add Current Working Directory


To only stage the local changes in the current working directory >> git add .


Git Add Additional Parameters


There are several different variations of "git add" command that are used for different purposes.


>> git add .

>> git add -A (git add --all)

>> git add -u (git add --update)

>> git add *

>> git add -A .

>> git add -u .


The main additional options are "git add -A" and "git add -u".

git add -A === git add --all

git add -u === git add --update

The functionality of these options differ across different Git versions. So we need to consider the Git version when working with these additional options.


Git local changes OR dirty files fall into 3 main types : new files, modified files and deleted files.


Git Version 1.x

Git Version 2.x


 

In Git version 1.x,


git add <path> :

Only staged the "new and modified files in the specified path and ignored the deleted files (removals)". This command has been changed in the Git version 2.x to stage removals as well.


git add . :

Stage new and modified files only (ignore the deleted files or removals) in the current working directory.


git add (no path specified) :

Will not add add anything, request the user to execute "git add ."


git add -A <path> :

Stage new, modified and deleted files in the specified path.


git add -A . :

Stage new, modified and deleted files in the current working directory.


git add -A (no path specified) :

In the code level "." will be added and executed as "git add -A ." on the current working directory.

git add -A === git add -A .

This has been modified in Git version 2.x to add ":/" in the code level if the path is not specified and executed on the entire working tree.

git add -A !== git add -A .

git add -A executed on entire working tree from the root.


git add -u <path> :

Stage modified and deleted files (ignore new files) in the specified path.


git add -u . :

Stage modified and deleted files (ignore new files) in the current working directory.


git add -u (no path specified) :

In the code level "." will be added and executed on the current working directory.

git add -u === git add -u .

This has been modified in Git version 2.x to add ":/" in the code level if the path is not specified and executed on the entire working tree.

git add -u !== git add -u .

git add -u executed on entire working tree from the root.

 

In Git version 2.x,


git add <path> :

New, modified and deleted files are staged in the specified path.

In the earlier version this command didn't support staging removals.

From 2.x onwards to omit removals when staging use the --ignore-removal flag.

>> git add --ignore-removal <path>


Refer to the Git 2.0.0 release notes for further details


Usage of "git add -A" and "git add -u" commands didn't change.

But now if the path is not specified with the -A and -u flags, in the code level ":/" is added to the path and commands are executed from the top-level on the entire working tree.

Refer to the Git 2.0.0 release notes for further details :


From Git 2.x, "git add -A" and "git add -u" commands without the path execute on the entire working tree. Earlier version 1.x, the commands without the path were execute in the current working directory.


So, now,

"git add -A" !== "git add -A ."

"git add -u" !== "git add -u ."


Refer to the Git commit :


Git 2.7 (Nov. 2015) will allow you to add a folder named ":".


In summary,


Git version 2.x has improved the "git add" to stage file removals as well.

To omit removals --ignore-removals flag need to be passed.


Git 2.x changed the "git add -A" and "git add -u" commands to execute on the entire working tree if no path specified.


Git 2.x supports adding everything inside a directory named ":" to the staging area by

>> git --literal-pathspecs add -u :/


If we just specify the directory like

>> git add -u :/

It will consider it as everything under the top-level from the root and stage changes in the entire working tree.

To avoid this we pass the "--literal-pathspecs" flag to disable the magic with

--literal-pathspecs.


Refer to the documentation for more information on literal-pathspecs magic.


Thank you for reading!!!

Please feel free to drop your constructive comments.


13 views0 comments
Post: Blog2_Post
bottom of page