git - Is it possible to exclude file from `git push`, but keep them in the local repository?

前端 未结 5 1283
悲&欢浪女
悲&欢浪女 2020-12-13 09:44

In my home directory I have files in a local git repository, because I want track them all under version control.

Most of these files I want to push to a remote repo

相关标签:
5条回答
  • 2020-12-13 09:55

    Here git-update-index - Register file contents in the working tree to the index.

    git update-index --assume-unchanged <PATH_OF_THE_FILE>
    

    Example:-

    git update-index --assume-unchanged somelocation/pom.xml
    

    for more details.

    0 讨论(0)
  • 2020-12-13 09:56

    This is not possible. If you have committed a file, then it will be pushed. The push action only acts on commits, not on a file basis.

    But you could use a submodule for your sensitive data and keep this submodule on your local machine, while you push the regular git repository to the remote machine.

    0 讨论(0)
  • 2020-12-13 10:07

    The way I eventually got around the issue is the following: Put the sensitive information in a sub directory with its own git repository and symlink the file(s) back to the old location.

    E.g. in your home folder (~) lives the file .creds which you do not want in the public repository. Move this file in a sub folder called, say protected and create a symlink from ~ to protected/.creds. Of course, do not include this folder in your ~ repository , but create a new repository in the folder protected just to keep track of .creds. If you do not push this repository publicly at all, you are set.

    I know this solution is kind of a cop out: My questions states that the file resides in the same directory, but the symlinking works for me.

    0 讨论(0)
  • 2020-12-13 10:10

    You can go ahead and actually track these files (sans the sensitive info), but then use:

    git update-index --assume-unchanged <file>

    on each file. Then you can go ahead and add the sensitive info to each file, but Git will not see the file as changed, and not try to commit (and thus push) that sensitive info.

    To get Git to update the info again, you'd use:

    git update-index --no-assume-unchanged <file>

    0 讨论(0)
  • 2020-12-13 10:10

    Just don't commit the files you want to exclude.

    For example, if you have two files a and b that are in the local repo, but you only want to commit one of them, use: git add a to add one of the files. Then commit with git commit (don't include the -a flag)

    0 讨论(0)
提交回复
热议问题