Tuesday, October 11, 2011

Adding deleted files in git

When you type:

git commit -a

Git will add the new, modified and deleted files automatically and commit them

However, there are cases when you want to 'git add' deleted files (that got deleted in your working directory).

I tried:

git add *

But this didn't workout. It only added the modified and created files.

I wanted a way to add the deleted files, I thought of issuing a 'git rm' command:

git rm *

BUT WAIT. THIS WILL DELETE EVERYTHING.

So, what is the solution.

The solution is to use:

git add -u

The -u (show for --update) tells git to compare to the tracked files in git rather than the working directory.

According to the GIT manual:
'git add' man page says about the -u option...
-u
--update
Only match <filepattern> against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed.
If no <filepattern> is given, default to "."; in other words, update all tracked files in the current directory and its subdirectories.

No comments: