简体   繁体   中英

Is it possible to ignore a file once it is already tracked in Git?

Is it possible to add a dummy configuration file once (so that other developers can see how the configuration file should look) and then ignore it using the .gitignore file. Then replace the dummy config details with working ones so I can continue to develop the project and keep committing changes? But the original dummy config file will remain intact on Github?

This is the approach I attempted:

  • create repository on Github
  • intitialise local repo
  • pull remote repo
  • push local repo to github with Configuration.java (containing dummy details)
  • add Configuration.java to the .gitignore file
  • push local repo to github
  • add correct details to Configuration.java
  • push local repo to github

But the change to configuration.java is tracked, so this doesn't work.

If you are just worried about ignoring changes in your local dev repo, you can use git update-index --assume-unchanged <file> to ignore any changes to the file. See http://blog.pagebakers.nl/2009/01/29/git-ignoring-changes-in-tracked-files/ for more details.

You mention :

  1. push local repo to github with Configuration.java (containing dummy details)
  2. add Configuration.java to the .gitignore file

The problem with this is, gitignore only works on files that aren't already being tracked, and since you've committed it it's too late for the gitignore to work. To quote the gitignore docs:

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected;

If you want to commit your own work to a publicly visible repo, but want to hide sensitive info such as connection strings / passwords etc, then commit a dummy file first. For example, commit something like this:

public class Configuration {
    public static final String USERNAME = "someUsername";
    public static final String PASSWORD = "passwordHere";
}

Then, tell git to stop tracking changes on that file, so you can amend the values locally but not be prompted to commit those changes.

git update-index --assume-unchanged path/to/file.txt

A couple of things to point out:

  • Always check in a dummy config file, don't leave it out entirely. If you do, when people checkout your project and the file is missing, they won't be able to compile the project! Help them by giving some sample data.
  • As git is no longer tracking that file, if you make any modifications that you DO want to commit, you'll have to manually cater to that (either do it via github.com or turn tracking back on, fix it, then disable tracking again). For this purpose, keep that file relatively light, just variables, don't put logic in there that you may need to keep updating.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM