0

what is the proper way to remove a file from a git repository? I mean the file is already in the repository in the main branch.

Should I cd into the directory and use git rm filename ? or just rm filename straight?

$ cd myproject
$ git rm filename
$ git add .
$ git commit -m "filename has been removed"
$ git push
Zoltan King
  • 1,964
  • 4
  • 18
  • 38
  • Ok thanks but if `git rm filename` does the `git add filename` automatically do I still need to `git commit` and `git push` manually? – Zoltan King Jan 22 '21 at 13:18
  • 1
    You still have to add & commit & push afterwards. The removal won't add/commit automatically. – costaparas Jan 22 '21 at 13:23
  • 1
    @costaparas: not true, you don't have to do `git add` manually after `git rm` – Arkadiusz Drabczyk Jan 22 '21 at 14:02
  • @arkadiusz oh oops, you're right (thanks). Only commit/push would be needed if using `git rm`. – costaparas Jan 22 '21 at 14:04
  • @costaparas Thank you. For moving files, if I want to move a file from a folder to a different folder, do I need to use `git mv filename` or just `mv filename`? – Zoltan King Jan 22 '21 at 17:34
  • 1
    That's the sample principle as the `rm`. Either is fine. But if you `git mv`, you don't have to `git add`. If you do regular `mv`, then `git add` would be necessary – costaparas Jan 22 '21 at 23:58
  • In fact, this is also specified in the docs: "*The index is updated after successful completion, but the change must still be committed.*" – costaparas Jan 23 '21 at 00:07

0 Answers0