# Git add on deleted file

3 messages from 2012-08-13 to 2012-08-13. Participants: Angus Hammond, Junio C Hamano, Ralf Thielow.
Thread: https://gitlist.dev/t/31244

## Angus Hammond, 2012-08-13 17:34

Subject: Git add on deleted file
Message-ID: <CAOBOgRZRSk7+jxMg+v=GWcn3F9ZfDTGC89YhJ1s7o9=HaOx4Bg@mail.gmail.com>
URL: https://gitlist.dev/e/CAOBOgRZRSk7%2BjxMg%2Bv%3DGWcn3F9ZfDTGC89YhJ1s7o9%3DHaOx4Bg%40mail.gmail.com

```
I was messing around with git add and how it interacts with deleted
files earlier today and came across this odd behavior.

$git init
Initialized empty Git repository in /tmp/test/.git/
$touch foo
$git add foo
$git commit -m"initial commit"
[master (root-commit) 0b5a193] initial commit
 0 files changed
 create mode 100644 foo
$rm foo
$git status
# 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)
#
#       deleted:    foo
#
no changes added to commit (use "git add" and/or "git commit -a")
$git add foo
$git status
# 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)
#
#       deleted:    foo
#
no changes added to commit (use "git add" and/or "git commit -a")

Notice that the two outputs from git status are identical, so git add
doesn't appear to have changed anything. Personally I'd like to see
"git add foo" here be equivalent "git rm --cached foo", but I can
understand how others might prefer git add not to be destructive like
that. Either way, I don't think it's right that the command exits
without any output (which would normally indicate success) yet has
failed to do anything. Perhaps it should act more like git add does
when the file doesn't exist in the working directory or the index:

$git add bar
fatal: pathspec 'bar' did not match any files

Thanks
Angus

```

## Junio C Hamano, 2012-08-13 17:54

Subject: Re: Git add on deleted file
Message-ID: <7vipcmekzh.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vipcmekzh.fsf%40alter.siamese.dyndns.org
In-Reply-To: <CAOBOgRZRSk7+jxMg+v=GWcn3F9ZfDTGC89YhJ1s7o9=HaOx4Bg@mail.gmail.com>

```
Angus Hammond <angusgh@gmail.com> writes:

> ... Personally I'd like to see
> "git add foo" here be equivalent "git rm --cached foo", but I can
> understand how others might prefer git add not to be destructive like
> that.

Funny that you bring it up this week.  As I wrote in

  http://git-blame.blogspot.com/2012/08/leftover-bits.html

I think the following topic should be revisited:

  http://thread.gmane.org/gmane.comp.version-control.git/171811/focus=171841

-- >8 --
From: Junio C Hamano <gitster@pobox.com>
Date: Tue, 19 Apr 2011 12:18:20 -0700
Subject: [PATCH] git add: notice removal of tracked paths by default

When run without "-u" or "-A" option,

    $ edit subdir/x
    $ create subdir/y
    $ rm subdir/z
    $ git add subdir/

does not notice removal of paths (e.g. subdir/z) from the working tree.
Make "git add" to pretend as if "-A" is given when there is a pathspec on
the command line.  "git add" without any argument continues to be a no-op.

When resolving a conflict to remove a path, the current code tells you to
"git rm $path", but now with this patch you can say "git add $path".  Of
course you can do "git add -A $path" without this patch.

In either case, the operation "git add" is about "adding the state of the
path in the working tree to the index".  The state may happen to be "path
removed", not "path has an updated content".

The semantic change can be seen by a breakage in t2200, test #15.  There,
a merge has conflicts in path4 and path6 (which are removed from the
working tree), and test checks "git add path4" to resolve it must fail,
and makes sure "add -u" needs to be used.  We do not have to do this
anymore.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/add.c         | 3 +++
 t/t2200-add-update.sh | 4 ----
 2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/builtin/add.c b/builtin/add.c
index 89dce56..4eae028 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -389,6 +389,9 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 
 	if (addremove && take_worktree_changes)
 		die(_("-A and -u are mutually incompatible"));
+	/* default "git add pathspec..." to "git add -A pathspec..." */
+	if (!take_worktree_changes && argc)
+		addremove = 1;
 	if (!show_only && ignore_missing)
 		die(_("Option --ignore-missing can only be used together with --dry-run"));
 	if ((addremove || take_worktree_changes) && !argc) {
diff --git a/t/t2200-add-update.sh b/t/t2200-add-update.sh
index 4cdebda..b2fcd01 100755
--- a/t/t2200-add-update.sh
+++ b/t/t2200-add-update.sh
@@ -150,10 +150,6 @@ test_expect_success 'add -u resolves unmerged paths' '
 	echo 2 >path3 &&
 	echo 2 >path5 &&
 
-	# Explicit resolving by adding removed paths should fail
-	test_must_fail git add path4 &&
-	test_must_fail git add path6 &&
-
 	# "add -u" should notice removals no matter what stages
 	# the index entries are in.
 	git add -u &&
-- 
1.7.12.rc2.85.g1de7134

```

## Ralf Thielow, 2012-08-13 18:18

Subject: Re: Git add on deleted file
Message-ID: <CAN0XMO+42uZ-D3Fz47G+gYr37wgZOqADB3Yvf5DyRB+ptaXkSQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAN0XMO%2B42uZ-D3Fz47G%2BgYr37wgZOqADB3Yvf5DyRB%2BptaXkSQ%40mail.gmail.com
In-Reply-To: <7vipcmekzh.fsf@alter.siamese.dyndns.org>

```
I think the message

no changes added to commit (use "git add" and/or "git commit -a")

is not clear enough since it lacks on the "git rm" command which
is shown above.
#   (use "git add/rm <file>..." to update what will be committed)

Of course, applying this topic would solve this problem.
Alternatively we could adjust the message.

On Mon, Aug 13, 2012 at 7:54 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Angus Hammond <angusgh@gmail.com> writes:
>
>> ... Personally I'd like to see
>> "git add foo" here be equivalent "git rm --cached foo", but I can
>> understand how others might prefer git add not to be destructive like
>> that.
>
> Funny that you bring it up this week.  As I wrote in
>
>   http://git-blame.blogspot.com/2012/08/leftover-bits.html
>
> I think the following topic should be revisited:
>
>   http://thread.gmane.org/gmane.comp.version-control.git/171811/focus=171841
>
> -- >8 --
> From: Junio C Hamano <gitster@pobox.com>
> Date: Tue, 19 Apr 2011 12:18:20 -0700
> Subject: [PATCH] git add: notice removal of tracked paths by default
>
> When run without "-u" or "-A" option,
>
>     $ edit subdir/x
>     $ create subdir/y
>     $ rm subdir/z
>     $ git add subdir/
>
> does not notice removal of paths (e.g. subdir/z) from the working tree.
> Make "git add" to pretend as if "-A" is given when there is a pathspec on
> the command line.  "git add" without any argument continues to be a no-op.
>
> When resolving a conflict to remove a path, the current code tells you to
> "git rm $path", but now with this patch you can say "git add $path".  Of
> course you can do "git add -A $path" without this patch.
>
> In either case, the operation "git add" is about "adding the state of the
> path in the working tree to the index".  The state may happen to be "path
> removed", not "path has an updated content".
>
> The semantic change can be seen by a breakage in t2200, test #15.  There,
> a merge has conflicts in path4 and path6 (which are removed from the
> working tree), and test checks "git add path4" to resolve it must fail,
> and makes sure "add -u" needs to be used.  We do not have to do this
> anymore.
>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
>  builtin/add.c         | 3 +++
>  t/t2200-add-update.sh | 4 ----
>  2 files changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/builtin/add.c b/builtin/add.c
> index 89dce56..4eae028 100644
> --- a/builtin/add.c
> +++ b/builtin/add.c
> @@ -389,6 +389,9 @@ int cmd_add(int argc, const char **argv, const char *prefix)
>
>         if (addremove && take_worktree_changes)
>                 die(_("-A and -u are mutually incompatible"));
> +       /* default "git add pathspec..." to "git add -A pathspec..." */
> +       if (!take_worktree_changes && argc)
> +               addremove = 1;
>         if (!show_only && ignore_missing)
>                 die(_("Option --ignore-missing can only be used together with --dry-run"));
>         if ((addremove || take_worktree_changes) && !argc) {
> diff --git a/t/t2200-add-update.sh b/t/t2200-add-update.sh
> index 4cdebda..b2fcd01 100755
> --- a/t/t2200-add-update.sh
> +++ b/t/t2200-add-update.sh
> @@ -150,10 +150,6 @@ test_expect_success 'add -u resolves unmerged paths' '
>         echo 2 >path3 &&
>         echo 2 >path5 &&
>
> -       # Explicit resolving by adding removed paths should fail
> -       test_must_fail git add path4 &&
> -       test_must_fail git add path6 &&
> -
>         # "add -u" should notice removals no matter what stages
>         # the index entries are in.
>         git add -u &&
> --
> 1.7.12.rc2.85.g1de7134
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

```
