Git: how to add a file but not track it

This question already has an answer here:

  • Git ignore files being tracked WITHOUT DELETING THEM

    5 answers

  • How to add Stored Procedures to Version Control
  • Check out a Git repository with chef?
  • examining history of deleted file
  • Get changes from master into branch in Git
  • Moving multiple files in TFS Source Control
  • Testing and Managing database versions against code versions
  • git: splitting merge into a few commits
  • Create a git tree from working tree without touching the index?
  • 2 Solutions collect form web for “Git: how to add a file but not track it”

    [Edit]
    The duplicate identified contains a more robust solution (git submodules) to this question. This answer is valid, but after further consideration it is not appropriate if you want a team to get the same behaviour in each clone.
    [End Edit]

    Git supports this with the --assume-unchanged option for git update-index. This will let you commit the production version of database.php, and have developers update local versions of this file to point to their local resources.

    git add database.php
    git commit
    git push
    git update-index --assume-unchanged database.php
    

    When/if you need to make changes to the production version of database.php:

    git update-index --no-assume-unchanged database.php
    git commit -a
    git update-index --assume-unchanged database.php
    

    Also each developer will need to run git update-index --assume-unchanged database.php if the want this too as this command only affects the local repository.

    You will want to ensure by some other mechanism that database.php fails for developers fails on non-production machines – don’t want developers pointing to production obviously 🙂

    Note you will need to remove the database.php file from your ignore list before you can add it.

    It is a really bad idea to add a file to a repository under version control and then asking git not to track any changes to the file. Files which are added under version control are meant to be versioned and tracked, and if you do not want to track any changes to a file, the rule of the thumb is DO NOT add it in the first place.

    Agreed that people have their own preferences, and git supports these workflows to some extent as well, but you would rarely see people using it, and would favor much cleaner and straight-forward solutions instead. In fact the use case which the OP is requesting (adding a file under version control and make git to stop tracking any changes to the file, and make this possible for all future clones/fetches/pulls), is not directly feasible in git at all.

    .gitignore is used for excluding files from accidentally going under version control and also for git not listing any changes to it in commands like git status. But if a file already exists under version control, git does not care if that file is part of .gitignore and starts tracking changes to it.

    Instead I would recommend that you add a template file called database-template.php and make every user create a copy of it as database.php and input his/her changes into it. You could also probably write a simple script which does that.

    Along with that, add database.php to your .gitignore and you’re good to go, so that the user’s local copy does not get into version control.

    Alternative Solution:
    There is another way to do this, but each developer should do it his local repo, and this is really a bad approach IMO:

    # Ask git to stop tracking any changes to database.php
    git update-index --assume-unchanged database.php
    
    # If you want to start tracking changes again to database.php
    git update-index --no-assume-unchanged database.php
    
    Git Baby is a git and github fan, let's start git clone.