How to delete files from my remote and local HEAD from my git but not delete from my disk?

Asked

Viewed 526 times

1

I have some files that I uploaded to my local remote Git HEAD, but now I need to delete these files but need to leave them in my local development environment. These files cannot go to the remote because they are private files.

  • You’ve already committed these files?

  • Yes, I don’t mind having those files in the history, but I don’t want them showing up in the next commits

  • Then you’ll need to rewrite the history

  • I don’t mind having these files in the history, I just want to delete them so that in the next commits, they don’t appear in the repository

  • git always keeps history of everything commited. If you’ve committed once in your life private data, they’ll be present in the commits tree forever

  • Dude, I don’t know if you understand, but I don’t mind them being in the tree, I just want to delete it so it doesn’t show up in the next commit I give, I just need them on my pc, I don’t need them when I give a next git pull

  • It would be better to take it out of the tree then, or put in the gitignore

Show 2 more comments

1 answer

2


Using git rm --cached

Because you don’t care that the file continues in previous commits everything gets easier:

  1. First, include in your . gitignore the name of the files you intends to delete, as indicated by @Bacco in the comments of question.

    echo arquivo_que_nao_vou_mais_querer >> . gitignore

  2. Now use git rm --cached to delete the archive from the repository local, but not disc:

    git rm --cached arquivo_que_nao_vou_mais_querer

So the file was removed from git, but not from the directory itself:

nunks@yokoi:~/test$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    arquivo_que_nao_vou_mais_querer

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   .gitignore
  1. Now you can commit and push smoothly.
  • Thanks man, that was exactly what I was looking for. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.