Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify git add to handle deleted files?

Tags:

git

I removed a few files from my git repo and now, upon status see

# Changes not staged for commit: # ... #   deleted:    project/war/favicon.ico #   deleted:    project/war/index.html 

Usually, i would stage them by issuing git add . command, but doing so does not affect git status. Files are still not staged for commit.

Now .. i know i can git rm file to take care of this.

The question is ... can i modify git add . somehow to also stage deleted files as well? I thought add "." takes care of everything (deleted files included)

like image 926
James Raitsev Avatar asked Oct 10 '12 16:10

James Raitsev


People also ask

How do I add a commit to a deleted file?

To add a single file to the commit that you've deleted, you can do git add what/the/path/to/the/file/used/to/be . This is helpful when you have one or two deletions to add, but doesn't add a batch of deletions in one command.

What to do when git deleted files?

Recovering Deleted Files with the Command Line Git provides ways to recover a deleted file at any point in this life cycle of changes. If you have not staged the deletion yet, simply run `git restore <filename>` and the file will be restored from the index.

Does git add only tracked files?

It does not add any new files, it only stages changes to already tracked files. git add -A is a handy shortcut for doing both of those.


1 Answers

git add . will add new and modified files to the index. git add -u will delete files from the index when they are deleted on-disk and update modified files, but will not add new files. You need a combination of the two:

git add . && git add -u . 

Addendum: It appears that the -A switch will catch all three: added, modified, and deleted files.

git add -A . 

Note the extra '.' on git add -A and git add -u


Warning, starting git 2.0 (mid 2013), git add -A|u (not extra dot) will always stage files on the all working tree.
If you want to stage file only under your current path with that working tree, then you need to use

$ git add -A . 

See "Difference of “git add -A” and “git add .”".

like image 91
cdhowie Avatar answered Oct 04 '22 01:10

cdhowie