Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 repository, but a few I want to keep in my local repository only (they contain mildly sensitive information).

How can I achieve this with git? Can I configure a ".gitignore-for-push" file? I cannot use the local .gitignore file, because it would exclude these files completely from being tracked.

ps: I am aware of question Is there an exclude file-equivalent..., but the answer goes down the .gitignore path which I cannot use. The other question Exclude specific files when pushing... answers only a specific case for git+heroku, not git alone.

like image 488
halloleo Avatar asked Aug 10 '12 04:08

halloleo


People also ask

How do I remove files from git remote and keep local?

Execute the following command: git rm --cached path/to/file . Git will list the files it has deleted. The --cached flag should be used if you want to keep the local copy but remove it from the repository.

How do I exclude files from a git repository?

Open the . git/info/exclude file in a text editor and add the folder to ignore. This file remains private to you.


2 Answers

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>

like image 118
redhotvengeance Avatar answered Oct 09 '22 07:10

redhotvengeance


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.

like image 35
dunni Avatar answered Oct 09 '22 09:10

dunni