WestMichiganRubyTraining / discussion

Create issues on this repository to open discussion threads in the Issue Tracker!
2 stars 0 forks source link

git deleted files #42

Open ThomasBush opened 10 years ago

ThomasBush commented 10 years ago

I have probably had a more difficult time comprehending git/github than anyone ever for some reason so forgive me if this is a trivial question.

I work in my local repo, in this case I manually deleted some files git add .

git commit -m 'something really insightful'

git push

Okay now these files are still in the github repo, but I deleted them and pushed successfully? Also on my local machine git status shows a listing of all these files in what I can only assume is file purgatory because they no longer exist on my machine.

How do I recover from this? I want these files out of my status and out of my repo, what do I do to accomplish this?

I know git rm filename is a command but not really how it should be used. So is this what I should have used instead of manually deleting these files? Would this have prevented the file purgatory issue? Would this have removed the files from github when I committed?

shekibobo commented 10 years ago

git rm filename will stage a removed file in your workspace.

To make it easier to stage all file removals in the workspace, I add an alias to my .gitconfig:

[alias]
  rma = "!git ls-files --deleted | xargs git rm"

This way, git rma will stage all files in the workspace that are marked as deleted so I don't have to do them one at a time.

thejbsmith commented 10 years ago

In order to recover from this, all you need to do is run git rm filename for each of the files listed as deleted but not staged for commit.

When you manually delete a file, it removes it from your system but not from Git. Git notices that the file was removed (which is why it shows you a list of deleted files when you run git status), but does not stage the removed file for commit.

Using git rm filename specifically tells Git that you want to remove the file from your repository. If you run this after manually deleting the file it will stage the deleted file for commit. If you run git rm filename before manually deleting the file, it will remove the file from your system as well as staging the file for commit in Git.

billgathen commented 10 years ago

What you did originally is also fine, but doing git add --all instead of git add . will also stage the "you removed this file" changes as well, then the commit will include them. If you do that now, you'll stage the deletions, too, then they'll go in with your next commit.

In the future, using the git rm <filename> approach is an "all in one" solution, since it deletes the file and stages it as well.

BTW, you're right about the "file purgatory": it's displaying changes it recognizes but that have not been staged. :smile:

ThomasBush commented 10 years ago

Awesome, got it all fixed! @shekibobo I actually got your .gitconfig previously from you via twitter/github, so I didn't need to add it as an alias, but that makes me think I maybe should look there for an answer first next time as the majority of the aliases are currently over my head.

@thejbsmith and @billgathen @shekibobo thanks for the clear explanations, I feel like I finally understand this, as I was having trouble finding a concise way to google the problem I was having.

Also while I have your attention, I was hoping I could ask one more potentially obvious question. Why won't github respect changes made to a .gitignore file? Is this a similar staging issue on my part? My expectation is that if I added a folder or file to the .gitignore then stage, commit, and push, that the file or folder would be removed from my repo and no longer track changes, but that it would still exist in my local file tree. Is this an incorrect assumption?

I don't want to git rm these files as they are necessary, but I also don't want to track changes to them as they are specific to that instance.

billgathen commented 10 years ago

The .gitignore only applies to files that haven't already been committed, so that may be the problem. Also, changes to your .gitignore will only be noticed by git when it's staged git add .gitignore.

Hope that helps.

shekibobo commented 10 years ago

Github (and git, for that matter) will ignore files in your .gitignore file provided you didn't add them to the repository already. You can add a file and then keep it around without tracking changes to it, though. Here's a post I found about how that works.

In certain cases, like a database.yml or api_keys.yml file that you use to store sensitive info that shouldn't be in your repository, I usually add a filename.example.yml file that I keep updated with examples of what should actually be in there, and then leave it to the user to copy it to filename.yml and update all the values as necessary.

shekibobo commented 10 years ago

@billgathen In my experience, changes to the .gitignore are respected as soon as the change is saved. I could be wrong, though.

ThomasBush commented 10 years ago

Thanks all for the help!