Mercurial - How to remove a file from version control?
Asked Answered
M

5

36

So I accidentally included a config file (different for each machine) into our mercurial repositories.

How can I get Mercurial to not include it in version control? I don't want to delete the file since I still want it. And I don't want to cause the file to get deleted on other developer's working directories either.

And how do I do this in TortoiseHG?

Milquetoast answered 14/7, 2010 at 21:21 Comment(0)
L
39

Right click on the file -> TortoiseHG -> Forget Files. Click Forget. Commit and Sync.

Edit: You'll also want to add the path to your .hgignore to keep it from getting added again. You can right click on the file in the HG Commit dialog and choose to ignore it.

Libelant answered 14/7, 2010 at 21:26 Comment(5)
Need to add the steps to add the config file to .hgignore, so it doesn't get reintroduced later on.Hackathorn
I tried this but when I did a push to the central repository, and then did an update on that machine, the file was deleted. This, do not want ...Milquetoast
Greg: Since you're ignoring the file now, you'll need to copy over the config file in the right location in each repo checkout.Hackathorn
@sdolan: I was hoping to find a command that removes a file from the repository but doesn't delete the file.Milquetoast
@Greg: It will only delete it from the repository the first time you update after "forgetting" it. You can then copy it back in, and won't have to worry about it getting checked in/deleting again. There is no way that I know of to get around this on the scm level. What I do (and suggest doing) is adding a step to your deployment scripts (I use fabric) that does a soft link from the configuration file located in a separate conf directory on the server. This way if you do a new clone for whatever reason you'll always have the correct configuration in place. Hope that helps?Hackathorn
H
27

Here's the manual way of doing it through the command line:

  1. Copy the config file somewhere outside of the repository.
  2. Run hg rm path/to/config/file
  3. Add the config file path to your .hgignore.
  4. Commit the repository.
  5. Move the config file back to where you had it.
  6. Do an hg stat on your repository to double check you did everything right. (It shouldn't show up in the list of modified/added files).

Edit:

hg forget is the best way to do this.

  1. Run hg forget path/to/config/file
  2. Edit your .hgignore and add the path to the config file.
  3. hg ci to save your changes.
  4. Run hg stat to ensure everything worked according to plan.

See nates answer for how to do it TortoiseHG.

Hackathorn answered 14/7, 2010 at 21:29 Comment(5)
-1? These steps will work just fine, just in 2 more commands than forget. hg forget does the equivalent of steps 1,2 and 5 in one shot. I didn't know it existed until right now, so I guess thanks for getting my attention, as it is a much more efficient way to do it.Hackathorn
@Nate: He asked "How can I get Mercurial to not include it in version control?". That's what I answered. He included at the end "And how do I do this in TortoiseHG?".Hackathorn
Looking at it again, you're right. The question does ask how to do it AND how to do it in Tortoisehg. If you make an edit (any edit), I'll remove my -1. Sorry about that.Libelant
No worries. I've also updated the answer to include the hg forget way of doing things.Hackathorn
-1 changed to a +1. Good answer. Again, apologies for the quick trigger finger.Libelant
M
2

hg remove or hg remove -f? I think hg forget also removes it from the branch.

In both cases, files are retained in your directory.

Marchioness answered 14/7, 2010 at 21:25 Comment(2)
'hg forget' is just short for 'hg remove -Af' (#1101667)Libelant
Also... hg remove without any options will delete the file from the local directory so that would not work in this caseLibelant
R
-1

For those using SourceTree, there is an option Stop Tracking when you right click a file, it basically does the same thing as hg forget or git rm --cache, removing the file from repo but not from hard disk.

Redpoll answered 8/6, 2016 at 3:58 Comment(0)
S
-2

add it to your ignore list.

See the .hgignore file.

TortoiseHG gives you access to this config file via the "Edit Ignore Filter" menu option.

Stratosphere answered 14/7, 2010 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.