80

It happens to me all the time. I accidentally version a file, I do not want to be versioned (i.e. developer/machine specific config-files).

If I commit this file, I will mess up the paths on all the other developer machines - they will be unhappy.

If I do delete the file from versioning, it will be deleted from the other developers machines - they will be unhappy.

If I choose to never commit the file, I always have a "dirty" checkout - I am unhappy.

Is a clean way to "unversion" a file from revision-control, that will result in no-one being unhappy?

edit: trying to clarify a bit: I have already commited the file to the repository and I want to only remove it from versioning - I specifically do not want it to be physically deleted from everyone doing a checkout. I initially wanted it to be ignored.

Answer: If I could accept a second answer, it would be this. It answers my question with respect to git - the accepted answer is about svn.

15 Answers 15

88

In Git, in order to delete it from the tree, but NOT from the working directory, which I think is what you want, you can use the --cached flag, that is:

git rm --cached <filename>
4
  • 6
    Will that delete everyone else's copy of the file when they pull changes?
    – cHao
    Sep 18, 2012 at 17:12
  • I'm stuck with RabbitVCS and your command just work as a charm for me. Thank you! @wxs
    – Nam G VU
    Jul 25, 2016 at 11:13
  • @BenLessani-Sonassi - what if they have local changes? Merge after pull fails? Sep 12, 2017 at 23:09
  • @BenLessani-Sonassi But then is there a way to unversion it for everyone without having it being deleted?
    – jeromej
    May 10, 2019 at 11:52
35

SVN version 1.5 supports removing/deleting a file from a repository with out losing the local file

taken from http://subversion.tigris.org/svn_1.5_releasenotes.html

New --keep-local option retains path after delete..

Delete (remove) now takes a --keep-local option to retain its targets locally, so paths will not be removed even if unmodified.

7
  • 2
    you can then use svn propedit with the svn:ignore property to make sure these locally stored files don't come up in future changes.
    – jorelli
    Aug 11, 2010 at 16:33
  • Does that prevent SVN from deleting everyone else's local copy also?
    – P Varga
    Mar 19, 2012 at 13:10
  • 2
    Answer: no it does not! Damn!
    – P Varga
    Mar 19, 2012 at 13:28
  • 15
    In TortoiseSVN this option is hidden in the "extended context menu": shift-right-click the file.
    – Jeroen
    Sep 11, 2012 at 12:28
  • in my experience, that wouldn't keep the file in other developers' machines. Apr 12, 2013 at 8:05
9

If you accidentally 'add' a file in svn & you haven't committed it, you can revert that file & it will remove the add.

6

Without having tried it...

In git, if your changes haven't been propagated to another repository, you should be able to git rm the affected file(s), git rebase --interactive to reorder the deletion commit to be just after the commit in which you accidentally added the offending files, and then squash those two commits together.

Of course, this won't help if someone else has pulled your changes.

1
  • You'll need to pass the --cached switch to rm like so git rm --cached <file> so that it's only removed from the index and not the file system.
    – ghickman
    Jan 28, 2011 at 10:59
4

It sounds like you have already added and committed the file to subversion (I assume that you are using Subversion). If that is the case, then there are only two ways to remove that file:

  1. Mark the file as deleted and commit.
  2. Perform an svnadmin dump, filter out the revision where you accidentally committed the file and perform an svnadmin load.

Trust me, you don't really want to do number 2. It will invalidate all working copies of the repository. The best is to do number 1, mark the file as ignored and apologise.

3

Look up svn:ignore and .gitignore - these features allow you to have extra files in your checkout that are ignored by your RCS (when doing a "status" operation or whatever).

For machine-specific config files, a good option is to check in a file named with an extra ".sample" extension, ie. config.xml.sample. Individual developers would make a copy of this file in config.xml and tweak it for their system. With svn:ignore or .gitignore you can ensure that the unversioned config.xml file doesn't show up as dirty all the time.

In response to your edit: If you remove the file from the repository now, then your developers will get a conflict next time they do an update (assuming they have all changed the file for their system). They won't lose their local changes, they will be recoverable from somewhere. If they happen not to have made any local changes, then their config file will vanish but they can just re-get the previous one out of source control and use that.

3

To remove a file entirely from a git repository (Say you commited a file with a password in it, or accidently commited temporary files)

git filter-branch --index-filter 'git update-index --remove filename' HEAD

Then I think you have to commit, and push -f if it's in remote branches (remember it might annoy people if you start changing the repository's history.. and if they have pulled from you before, they could still have the file)

2

To remove a file already in source control:

git rm <filename>

and then

git commit -m ...

You should add every file you want to ignore to the .gitignore file. I additionally always check the .gitignore file to my repository, so if someone checks out the code on his machine, and the file gets generated again, he won't 'see' it as 'dirty'.

Of course if you already committed the file and someone else got your changes on another machine, you would have to alter every local repository to modify the history. At least that's a possible solution with git. I don't think svn would let you do that.

If the file is already on the master repository (git) or in the server (svn), I don't think there is a better solution than just deleting the file in another commit.

1

For SVN you can revert files you haven't committed yet. In TortoiseSVN you just right click the file in the commit window and choose Revert...

On command line use svn revert [file]

Don't know about GIT since I've never used it.

1

Two simple steps in SVN:
1. Add this directory in parent directory's svn:ignore property:

svn propedit svn:ignore .  

2. Remove directory:

svn rm mydir

3. Commit

Please note that when other developers do a svn update, that directory will not get deleted. SVN just unversions it instead.

0

As far as I know there is no easy way to remove an added file from versioning control in svn once it is committed.

You will have to save the file somewhere else and delete it from version control. Than copy the backup back again.

It's a version control system after all... ;)

0

You can exclude files from subversion with the global-ignore setting
http://svnbook.red-bean.com/en/1.1/ch07.html#svn-ch-7-sect-1.3.2
check out the documentation for details

0

I there f I choose to never commit the file, I always have a "dirty" checkout - I am unhappy.

With regard to this particular point, you might want to .gitignore the file as other have suggested, or to use a scheme like the one described in this answer of mine.

0

You can unversion all files in current directory with this command. The sed bit reverses the order so svn can process it:

find . | sed '1!G;h;$!d'| awk '{print "svn rm --keep-local " $1}'

As already stated in other answers, single file is unversioned with this:

svn rm --keep-local yourFileNameXXX
0

In Windows, if what you are looking for is copying the folder to another location and removing it from git, so that you don't see the icons anymore, you just delete the .git folder. The .git folder is hidden, so you have to go to Organize / Folder and Search Options, Display hidden Files option, under your main folder. It should unversion it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.