How to remove deleted files from showing up in my local git status?

42,989

Solution 1

Since they've already been committed to the repository it's a bit more work, but still doable.

The best walkthrough that I've found on this procedure is Github's guide on Removing Sensitive Data. Since you want to keep the files, you have a step to do before and after the guide.

Before you start this process, copy the files out of your working directory. Once you have finished purging the repo of them, copy them back in and add them to your .gitignore.

Solution 2

I find this to be the easiest way of doing the task.

$> git add -u <directory-path>   (or you may use --update)

Original State :

$> git status .

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
deleted:    .ipynb_checkpoints/Untitled-checkpoint.ipynb
deleted:    CVPR 1 hr tute-Backup.ipynb
deleted:    cifar10-test.t7
deleted:    cifar10-train.t7
deleted:    cifar10torchsmall.zip
deleted:    create_advr.lua
deleted:    criterion_cifar10_lr_0.001_epoch_13.t7
deleted:    train_cifar10.lua
deleted:    trained_net_cifar10_lr_0.001_epoch_13.t7

So, to remove all deleted files from my current directory, I use.

$> git add -u .
$> git commit -m "removing deleted files from tracking"
$> git push origin master

Final State :

$> git status .

On branch master
nothing to commit, working directory clean

Hope this helps :)

Solution 3

You probably need to git rm the deleted files. From Pro Git:

Removing Files

To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it from your staging area) and then commit. The git rm command does that and also removes the file from your working directory so you don’t see it as an untracked file next time around.

If you simply remove the file from your working directory, it shows up under the “Changed but not updated” (that is, unstaged) area of your git status output:

$ rm grit.gemspec
$ git status
# On branch master
#
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#
#       deleted:    grit.gemspec
#

Then, if you run git rm, it stages the file’s removal:

$ git rm grit.gemspec
rm 'grit.gemspec'
$ git status
# On branch master
#
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    grit.gemspec
#

The next time you commit, the file will be gone and no longer tracked. If you modified the file and added it to the index already, you must force the removal with the -f option. This is a safety feature to prevent accidental removal of data that hasn’t yet been recorded in a snapshot and that can’t be recovered from Git.

Solution 4

Updated after your comments.

If you want to keep the images on the server, but remove the files from git then you need to add the --cached flag to git rm.

cd images
git rm --cached *
cd ..
echo "images" > .gitignore
git add .
git commit -m "removed image files from git only"
git push

if you want people to create the images directory when they pull (but have no files), then add a images/.gitignore with the single line !.gitignore

Note, this won't remove them from the history. For that, i would look at this SO answer

Share:
42,989
uwe
Author by

uwe

Updated on November 10, 2020

Comments

  • uwe
    uwe over 3 years

    A developer added an image directory on the server without adding that folder to the .gitignore file. When I did a git pull it pulled those files (hundreds of files). I added the folder to the .gitignore on the server but not my local system. I then deleted the files locally (permanently). When I do a git status all those deleted files still show up. How can I suppress them from showing up?

    Update: thanks for all the great help. To make sure there is no misunderstanding: I do want to keep the files on the server; I just want to remove them from git.

  • uwe
    uwe over 12 years
    thanks! So when you commit the changes it won't remove the file from the remote repository, right?
  • Andy
    Andy over 12 years
    @mototribe Look at my answer for what you will need to do to permanently remove the files from the history of the repository.
  • uwe
    uwe over 12 years
    sorry, I wasn't clear, I want to keep the files on the server, just remove them from the git tracking
  • uwe
    uwe over 12 years
    couldn't I just copy my directory with all the files, then do a git rm images/*, git commit, git push? Then I copy the directory back.
  • Andy
    Andy over 12 years
    If you do that, images/* will remain in your history. You will need to go back and edit the commits that introduced them. Otherwise the repository will still have the files, they just won't be in version after you deleted them.
  • uwe
    uwe over 12 years
    "history" as in the git log? all I want to do is prevent the files from showing up in the "Changes not staged for commit:" list when I do a "git status". Thanks for all the help!
  • Andy
    Andy over 12 years
    By history, I meant that everyone who clones that repo is going to clone all of those files, if they are not removed from the history. Although they are not currently in your working copy, you, or anyone else with a clone, can still access all of those files by checking out the commit that introduced them.
  • ocean800
    ocean800 almost 7 years
    what is the difference between this method and using git rm?
  • NightFury13
    NightFury13 almost 7 years
    I don't see any difference in the output so I think you may use what ever suits you best (someone please correct me if I'm wrong). git add also adds the deleted files to tracking, just like git rm.
  • mmai
    mmai over 3 years
    @ocean800 @NightFury13 Using git add -u . you don't have to specify the names of all the files you want to delete.