0

In my project in github I made big mistake , i forgot to add .gitignore in start , and more files were added to my repo. Now I've added .gitignore where are a lot of files and directories. I've done a lot of commits before this and Now I want to delete all unnecessary files from repo with command git filter-branch --tree-filter 'rm -f passwords.txt' HEAD. This command deletes password.txt from all previous commits , but I have two questions: 1) what if I want to delete all files that are in .gitignore? What I must to write after rm?? 2) After input this command , git wrote me: "master branch and 'origin/master' have diverged. Use git pull....". I didn't use this command , I just use git push -u. Explain please what happened with my brancch? Why it is diverged? P.s. I work alone with my project and have only one branch - master

M-Misha-M
  • 33
  • 9

2 Answers2

0
  1. If your .gitignore lists only files (not directories or patterns) the command should be

    rm -f `cat .gitignore`
    

    I recommend to copy your .gitignore to a file out of Git tree to avoid Git changing the file while it filters commits. So:

    cp .gitignore ../gitignore
    git filter-branch --tree-filter 'rm -f `cat ../gitignore`'
    rm ../gitignore
    
  2. As for the second question — git filter-branch (like any other commit-editing command — amend, rebase, etc.) creates new commits, an entire new branch really, so you you cannot just push filtered branch. You have to force-push it, either with git push -f origin master or git push origin +master.

grg
  • 5,023
  • 3
  • 34
  • 50
phd
  • 82,685
  • 13
  • 120
  • 165
  • thanks for the answer but I use Visual Studio .Net project and I must to delete also directory , because there are some folders in project – M-Misha-M May 28 '17 at 19:22
  • rm -rf `cat ../gitignore`. '-r' means 'recursive'. – phd May 28 '17 at 20:09
  • thanks again , but something wrong.. your command is works but I try to delete all files and directories from previous commits and after your command I get next http://i.imgur.com/wGtYm6g.png. Also I've done a copy of .gitignore with cp command – M-Misha-M May 28 '17 at 23:01
  • Nothing was changed because ../gitignore was not found. Don't know why, sorry. – phd May 29 '17 at 15:20
0

you should use git filter branch, imagine you want delete all target/ dirs in your previous commits, you can run just 4 commands for that works:

$ git filter-branch --tree-filter 'rm -f */target' HEAD

$ rm -rf */target (you use this one to delete target/ dirs on current HEAD)

$ git commit -am 'delete all target/ dirs' --allow-empty

$ git push
Walterwhites
  • 1,287
  • 13
  • 9