Good afternoon, dear, tell me, please, what commands you need to execute to remove the file from the main branch.
Suppose I accidentally added a style/mywork.scss file to git. What do I need to do in the gitBash window to remove it, without later displaying it on github?
And another question on this topic. In .gitignore I added an entry for the .style/.scss But it still added to git, what's the error?

  • 3
    If the file is already running, .gitignore will not help - vp_arth

2 answers 2

To remove a file from the repository, do the following:

  1. First, make sure you are on the master branch:

     git branch 

    It will be highlighted in green and asterisk.

  2. If you are not on the desired branch, then switch to it:

     git checkout master 
  3. Then you need to delete the file. There are two options for this, you can use any:

    • you can delete with the git command:

       git rm <путь_к_удалённому_файлу> 
    • or simply delete the file from the disk (you can simply through explorer) and add the change to the index with the following command:

       git add <путь_к_удалённому_файлу> 

    After performing one of the two options, the file will be deleted, and the changes in the index.

  4. We fix the changes:

     git commit -m "удалил файл <название_файла>" 

    The message can be different, this is as an example.

  5. Next you need to send your changes to the remote branch (on github). This can be done like this:

     git push 

    either so:

     git push origin master 

.gitignore

The .gitignore file .gitignore be added to the repository at the very beginning. If you suddenly zakomitili any files, and then added them to .gitignore , then the files that are already under the supervision of Git-a will not be deleted, but new files that fall under the rules of .gitignore will not be added.

  • one
    it is better to delete the file through git rm <имя-файла> - it will both delete the file and add it for it. After that you can commit immediately. git add . may add "extra". - KoVadim
  • @KoVadim, replaced git add . on git add <путь_к_удалённому_файлу> . I think that there will be no problems, but about git rm good point, if not against it, I can add to the answer :) - Umed
  • one
    of course supplement. - KoVadim

The method described in the Umed message will delete the file in the current state of the repository, but leave the entire contents of the file in the history. What may be undesirable if you inadvertently recorded a file with passwords / keys / tokens.

You can delete this file with the command

 git filter-branch --tree-filter 'rm -f имя_файла' master 

Attention! This command hashes the hashes of all the commits in which the specified file was found, as well as all later commits. Be careful when posting changes to the server. It may be useful for you option --force command push .

See Pro Git for details : Repair history and git-filter-branch (1)

PS: you can cancel the action of the command, for example,

 git checkout -B master refs/original/refs/heads/master 
  • one
    Attention! If you once lit your passwords, keys or tokens - they may already be found and written by someone. Instead of filter-branch will be more correct to immediately change them to new ones, making the old ones invalid. - Pavel Mayorov
  • one
    And, if you use a cloud from Amazon - look at the general list of access keys, virtual machines, and more. Perhaps your money is already running a bitcoin miner farm. There are bots that constantly monitor the appearance of Amazon keys on the github. - Pavel Mayorov