How can I delete a file from git repo?

2019-01-01 11:17发布

I have added a file named "file1.txt" to git repo. After that I committed it, added a couple directories called dir1 and dir2, and committed them to git repo.

Now the current repo has "file1.txt", dir1 and dir2.
How can I delete "file1.txt" without affecting others like dir1 and dir2?

标签: git git-rm
18条回答
步步皆殇っ
2楼-- · 2019-01-01 11:55

I tried a lot of the suggested options and none appeared to work (I won't list the various problems). What I ended up doing, which worked, was simple and intuitive (to me) was:

  1. move the whole local repo elsewhere
  2. clone the repo again from master to your local drive
  3. copy back the files/folder from your original copy in #1 back into the new clone from #2
  4. make sure that the problem large file is either not there or excluded in the .gitignore file
  5. do the usual git add/git commit/git push
查看更多
何处买醉
3楼-- · 2019-01-01 11:56

git rm will only remove the file on this branch from now on, but it remains in history and git will remember it.

The right way to do it is with git filter-branch, as others have mentioned here. It will rewrite every commit in the history of the branch to delete that file.

But, even after doing that, git can remember it because there can be references to it in reflog, remotes, tags and such.

If you want to completely obliterate it in one step, I recommend you to use git forget-blob

https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/

It is easy, just do git forget-blob file1.txt.

This will remove every reference, do git filter-branch, and finally run the git garbage collector git gc to completely get rid of this file in your repo.

查看更多
姐姐魅力值爆表
4楼-- · 2019-01-01 11:57

To delete a specific file

git rm filename

To clean all the untracked files from a directory recursively in single shot

git clean -fdx

查看更多
骚的不知所云
5楼-- · 2019-01-01 11:57

Note: if you want to delete file only from git use below:

git rm --cached file1.txt

If you want to delete also from hard disk:

git rm file1.txt

If you want to remove a folder(the folder may contain few files) so, you should remove using recursive command, as below:

git rm -r foldername

If you want to remove a folder inside another folder

git rm -r parentFolder/childFolder

Then, you can commit and push as usual. However, if you want to recover deleted folder, you can follow this: recover deleted files from git is possible.

From doc:

git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>…​

OPTIONS

<file>…​

Files to remove. Fileglobs (e.g. *.c) can be given to remove all matching files. If you want Git to expand file glob characters, you

may need to shell-escape them. A leading directory name (e.g. dir to remove dir/file1 and dir/file2) can be given to remove all files in the directory, and recursively all sub-directories, but this requires the -r option to be explicitly given. -f --force

Override the up-to-date check.

-n --dry-run

Don’t actually remove any file(s). Instead, just show if they exist in the index and would otherwise be removed by the command.

-r

Allow recursive removal when a leading directory name is given.

--

This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for

command-line options). --cached

Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.

--ignore-unmatch

Exit with a zero status even if no files matched.

-q --quiet

git rm normally outputs one line (in the form of an rm command) for each file removed. This option suppresses that output.

Read more on official doc.

查看更多
永恒的永恒
6楼-- · 2019-01-01 11:58

In my case I tried to remove file on github after few commits but save on computer

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch file_name_with_path' HEAD
git push --force -u origin master

and later this file was ignored

查看更多
与风俱净
7楼-- · 2019-01-01 12:00

More generally, git help will help with at least simple questions like this:

zhasper@berens:/media/Kindle/documents$ git help
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]

The most commonly used git commands are:
   add        Add file contents to the index
   :
   rm         Remove files from the working tree and from the index
查看更多
登录 后发表回答