11

I have a "parameters" file in a repo, which I've added to .gitignore so it is not being tracked.

I need to push it once so that it shows in the repo, but making sure it is not tracked. This is because I'll keep modifying it (since it stores input parameters) and I only want the default version showing in the repo.

If I git add --force my_params.dat the file is pushed, but then it keeps being tracked, which I do not want.

What are the correct steps to achieve this? I tend to avoid using git update-index --assume-unchanged FILE_NAME because I feel it obscures the tracking process, but I'm not strictly opposed to using it.


If I had to use the answers in the question How to make Git "forget" about a file that was tracked but is now in .gitignore?, I'd need to:

  1. remove parameters file from .gitignore

  2. push file and changed .gitignore

  3. re-add parameters file to .gitignore and push

  4. remove all tracked files with git rm --cached -r .

  5. re-add all files with git add . and push them

This doesn't work because it deletes the parameters file from the repo when I push . That's not what I need.

Community
  • 1
  • 1
Gabriel
  • 40,504
  • 73
  • 230
  • 404
  • 4
    If you downvoted, could you please explain why? Your silent downvote is not helpful, to me or anyone else. – Gabriel Mar 03 '17 at 13:03
  • 1
    Possible duplicate of [How to make Git "forget" about a file that was tracked but is now in .gitignore?](http://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore) – Samir Aguiar Mar 03 '17 at 13:05
  • 3
    How is this a duplicate of that question? My current parameters file is not even being tracked, I don't need to make `git ` forget about it. – Gabriel Mar 03 '17 at 13:07
  • 1
    Remove it from the gitignore, add,commit and push gitignore and the file. Re-add it to gitignore and to do a `git rm --cached -- `, push again. But I actually don't see a reasonable value for this process because the file is deleted afterwards from the history and is only accessiable for this particular commit where you added it. – ckruczek Mar 03 '17 at 13:24
  • 1
    Either you track a file, or you don't. Gitignore is used during add, but if you manage to track a file, or later on add a file to gitignore that you're already tracking, then you're tracking it. You can always just *not* add changes to the file to the index before committing. The typical way to handle configuration files, however, is to commit a template, and make a copy locally that is ignored. You will then track changes to the template, but not to the actual files. You could then have it as part of build or whatever to make a copy from the template if the actual file is missing locally. – Lasse V. Karlsen Mar 03 '17 at 13:40
  • @SamirAguiar: not a duplicate – invot Mar 01 '19 at 21:35

2 Answers2

10

I don't think it is intended nor possible via git.

I would recommend to hierarchically load the paraemter files. You either load parameters.file or, if not existing, paramters.default.file. The paramters.default.file is kept in git, whereas the parameters.file is ignored.

Users who want to adapt the parameters need to create a file parameters.file for that purpose (alternatively you can automate the process during some make/setup process).

Marcus
  • 922
  • 6
  • 21
  • It is not intended but possible. – ckruczek Mar 03 '17 at 13:37
  • 1
    @ckruczek feel free to provide an answer if you feel it is possible as requested by the user. I changed my answer for now. – Marcus Mar 03 '17 at 13:45
  • I already did in my comments as well in the linked duplicate. Thats the way, it is not reasonable and intended but possible. – ckruczek Mar 03 '17 at 13:46
  • Thanks @Marcus, I'll follow your advise and create a second (tracked) default params file. – Gabriel Mar 03 '17 at 13:53
  • 2
    I don't think you did @ckruczek. I assume the question is how to have a file not tracked, but showing up on a fresh checkout of any following HEAD. That is not fullfilled with your answer, nor with the link to the "duplicate". – Marcus Mar 03 '17 at 13:54
  • Holy crap you are totally right...I didn't realised he want it on fresh checkout. So my comments are obsolete. Good work! – ckruczek Mar 03 '17 at 13:55
  • what do we mean by loading hierarchically? – Farees Hussain Apr 10 '21 at 18:18
4

It looks like there's been a new feature added to support this kind of scenario

git update-index --skip-worktree <file>

Source: https://stackoverflow.com/a/20241145/2436737
More info: Git - Difference Between 'assume-unchanged' and 'skip-worktree'

farlee2121
  • 2,959
  • 4
  • 29
  • 41
  • 2
    This is not what the feature is for. It's intended for performance uses. The [docs](https://git-scm.com/docs/git-update-index#_notes) say this is a bad idea. "Users often try to use the assume-unchanged and skip-worktree bits to tell Git to ignore changes to files that are tracked. This does not work as expected, since Git may still check working tree files against the index when performing certain operations. In general, Git does not provide a way to ignore changes to tracked files, so alternate solutions are recommended." – Curtis Blackwell Apr 07 '20 at 16:36