2

I am newbie to git and I committed a big mistake. Other related thread didn't solve my problem. Here is what happened, it feels like "git rm" worked like linux rm command.

In the beginning I added development folder into git repo using

git add dir_name1

During last couple of days I created few more files in that directory and renamed a few, I think it requires manually adding new files in git, as I was committing all kind of changes after that but new files didnt save in repo.

Today after finishing the work, I decided to add those new files using

git add dir_name1

I guess with that new files will be found and added automatically, but when I saw that some temporary files (file ending with "~" in ubuntu) were also added.

In order to remove those tmp files I thought that "git rm" is the counterpart of "git add" so I could do "git rm dir_name1" followed by delete all files ending with ~ manually, and do "git add dir_name1" again then it will be ok.

so what i did was

git rm -r -f  dir_name1

My God, the entire directory is missing,restoring with "git reset" and few other tricks didnt restore those new files.

Does "git rm" removes just like linux "rm" command? is there a way to recover those files.

SOSSSSS

duckduckgo
  • 1,280
  • 1
  • 18
  • 32

2 Answers2

2

I saw that some temporary files (file ending with "~" in ubuntu) were also added

A simple *~ in a .gitignore would have prevented that

I could do "git rm dir_name1" followed by delete all files ending with ~ manually

git rm -r -f dir_name1 (or rm -r -f dir_name1) will remove the folder and its content from the disk, with or without git.

You might still have a trace of those previously added (staged) files with:

git fsck --cache --unreachable $(git for-each-ref --format="%(objectname)")

Read more in "Undo git reset --hard with uncommitted files in the staging area"

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thank you for the commnad, it returns all "unreachable blob 1685d57d4b460b43de338634b6d5932a0cecf5ad" lines – duckduckgo Oct 30 '15 at 05:29
  • @AbhishekK yes, http://stackoverflow.com/a/7376959/6309 and http://stackoverflow.com/a/15472831/6309 will show you what to do with it. – VonC Oct 30 '15 at 05:30
  • with your input I was able to recover 90% of work, funny that i used a system in a radically opposite way that is rather intended for safeguarding code :) – duckduckgo Oct 30 '15 at 06:11
1

The correct way to go about doing this would be to add the entire directory, and then remove the pesky Linux system files afterwards:

git add dir_name1
git rm -r '*.~' 

If you want to make the .~ files ignored by Git, then you can do this:

echo '*.~' >> .gitignore

Alas, if you never committed the dir_name1 directory, it might be lost in space.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360