How to "unversion" a file in either svn and/or git

SvnGitVersion ControlVersioning

Svn Problem Overview


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.

Svn Solutions


Solution 1 - Svn

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>

Solution 2 - Svn

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.

Solution 3 - Svn

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

Solution 4 - Svn

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.

Solution 5 - Svn

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.

Solution 6 - Svn

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.

Solution 7 - Svn

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)

Solution 8 - Svn

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.

Solution 9 - Svn

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.

Solution 10 - Svn

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.

Solution 11 - Svn

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... ;)

Solution 12 - Svn

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

Solution 13 - Svn

> 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.

Solution 14 - Svn

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

Solution 15 - Svn

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMo.View Question on Stackoverflow
Solution 1 - SvnwxsView Answer on Stackoverflow
Solution 2 - SvnbrianadkinsView Answer on Stackoverflow
Solution 3 - SvnGlenn SlavenView Answer on Stackoverflow
Solution 4 - SvnKaiView Answer on Stackoverflow
Solution 5 - SvnSir Rippov the MapleView Answer on Stackoverflow
Solution 6 - SvnGreg HewgillView Answer on Stackoverflow
Solution 7 - SvndbrView Answer on Stackoverflow
Solution 8 - SvnSergio AcostaView Answer on Stackoverflow
Solution 9 - SvnMagnus WestinView Answer on Stackoverflow
Solution 10 - SvnManu ManjunathView Answer on Stackoverflow
Solution 11 - SvnFantaMango77View Answer on Stackoverflow
Solution 12 - SvnpaanView Answer on Stackoverflow
Solution 13 - SvnDamien DiederenView Answer on Stackoverflow
Solution 14 - SvnPerfectGamesOnline.comView Answer on Stackoverflow
Solution 15 - Svnlive-loveView Answer on Stackoverflow