Shanthi
Shanthi

Reputation: 677

Regarding GIT command usage

I have a directory tree in my repo. I do a git add -A first time.

Now I remove a directory with files by mistake and do a:

git add -A

I lose the entire directory in my repo too. Or is there a way to retrieve my lost directory?

Also suppose I do a git add . I wouldn't have lost my directory, am I correct? So I take it there is no way to track deletion of files using git?

For example I deleted 3 files when I did an commit yesterday and if I want to go back to yesterday's version of all files and directories would I see the deleted files?

Upvotes: 0

Views: 77

Answers (1)

simont
simont

Reputation: 72527

My understanding of your question is this:

  1. You have a git repository with some files and directories.
  2. You've added everything initially (git add -A). Presumably you've also committed.
  3. You delete a directory from your repository (rm -rf dir), then add the changes (git add -A). I assume you then commit.

If this is the case, then yes, you've removed the directory from the repository. From the git-add man page:


-A, --all
   Like -u, but match  against files in the working tree in addition
   to the index. That means that it will find new files as well as staging modified
   content and removing files that are no longer in the working tree. 

git add -A will add all changes to the repository - additions, removals, the lot.

So I take it there is no way to track deletion of files using git?

I'm unsure what you mean by this. You can track deletion of files from git. Read the man-page for git-rm. Or, delete a file, then run git add -A: git will add the deleted file to the index, allowing you to commit the deletion. So the deletion is tracked.

Also suppose I do a git add . I wouldn't have lost my directory, am I correct?

You wouldn't have staged the deletion of the directory to the git index, correct. The directory is still gone from the git working directory, though, because you deleted it.

For example I deleted 3 files when I did an commit yesterday and if I want to go back to yesterday's version of all files and directories would I see the deleted files?

You add a file to your repository. git add file; git commit -m "added file". Let's call this commit A.
Next you delete that file, and commit the change to the repository: rm file; git add -A; git commit -m "Deleted file" (another way is git rm file). Let's call this commit B.

If you checkout commit B, the files are not there. You deleted them, and the commit reflects this.
If you checkout commit A, the files are there. That commit contains the files.

To answer your question:

For example I deleted 3 files when I did an commit yesterday and if I want to go back to yesterday's version of all files and directories would I see the deleted files?

No. You won't see the files in yesterdays commit, because you've committed the deletion. If you checkout the commit before yesterdays commit, though, the files are there.

Upvotes: 2

Related Questions