70

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.

Community
  • 1
  • 1
Mo.
  • 14,027
  • 13
  • 42
  • 57

15 Answers15

75

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>
wxs
  • 4,927
  • 5
  • 31
  • 46
34

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.

  • 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 '10 at 16:33
  • Does that prevent SVN from deleting everyone else's local copy also? – ᆼᆺᆼ Mar 19 '12 at 13:10
  • 2
    Answer: no it does not! Damn! – ᆼᆺᆼ Mar 19 '12 at 13:28
  • 15
    In **TortoiseSVN** this option is hidden in the "extended context menu": shift-right-click the file. – Jeroen Sep 11 '12 at 12:28
  • in my experience, that wouldn't keep the file in other developers' machines. – Ashkan Kh. Nazary Apr 12 '13 at 08:05
  • unless you version your svn properties file ? – Emmanuel BRUNET Feb 13 '16 at 09:46
  • What do you expect? If you order SVN to remove the file from the working copy you do not want to have it in the working copy any more. That means: It will not be present if someone performs a fresh checkout (or synchronizes an existing working copy). This is the fundamental concept of a version control system: Either the file is "official", then it is in all working copies. If it is to be excluded it automatically is "not official" = local only = not in working copies. My advice: Move the file to some other folder under version control if you still want to keep it in your project. – Regis May Aug 17 '17 at 12:30
9

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

Glenn Slaven
  • 31,855
  • 25
  • 107
  • 163
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.

Kai
  • 4,970
  • 4
  • 25
  • 34
  • You'll need to pass the --cached switch to rm like so `git rm --cached ` so that it's only removed from the index and not the file system. – ghickman Jan 28 '11 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.

Trumpi
  • 7,160
  • 5
  • 37
  • 50
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.

Greg Hewgill
  • 828,234
  • 170
  • 1,097
  • 1,237
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)

dbr
  • 153,498
  • 65
  • 266
  • 333
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.

Sergio Acosta
  • 11,109
  • 12
  • 58
  • 89
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.

Magnus Westin
  • 879
  • 7
  • 12
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.

Manu Manjunath
  • 5,540
  • 1
  • 29
  • 30
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... ;)

FantaMango77
  • 2,399
  • 4
  • 23
  • 27
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

paan
  • 6,868
  • 7
  • 33
  • 41
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
PerfectGamesOnline.com
  • 1,652
  • 1
  • 18
  • 26
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.

Community
  • 1
  • 1
Damien Diederen
  • 2,464
  • 1
  • 17
  • 7
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.

live-love
  • 34,372
  • 16
  • 163
  • 152