I don't want some files to be tracked by git and I'm excluding them with a .gitignore file. but I want to push them to the origin branch because they are necessary when someone clones the project.
Now the question is can I do this without adding and committing the excluded files. I found this answer on StackOverflow. but I guess maybe git has a better solution for this.

- 115
- 10
-
2If you add a file to the repository, then it is tracked by git, regardless of whether it is in `.gitignore` or not. There is no way to say "make this part of the repository but don't track it". – larsks Nov 23 '20 at 00:15
3 Answers
I summarized your question here:
Is there a way to push specified files in the .gitignore file to origin without adding and committing the excluded files?
Unfortunately, there is not. You can git add --force
the required file and then push it, but that's not what you asked for.
If you're trying to set up a project that requires API keys or other secrets, I recommend including instructions on how to acquire them in a README.md
file in the project.

- 317
- 2
- 9
-
Thanks, but if --force option be used then the ignored file will be added to the project and will be tracked. – Shuaib Jaff Nov 23 '20 at 11:17
-
1Correct! That's why I said there isn't a solution that fits your criteria. – Salvatore Testa Nov 23 '20 at 23:09
No, there isn't. A file is either tracked by Git, or it is not.
It sounds like you're trying to create some sort of tracked but ignored file, and that isn't possible with Git. The Git FAQ explains why, and suggests this approach for configuration files:
If your goal is to modify a configuration file, it can often be helpful to have a file checked into the repository which is a template or set of defaults which can then be copied alongside and modified as appropriate. This second, modified file is usually ignored to prevent accidentally committing it.
You can do this with a script or as part of your build process, whichever is most appropriate for your project.

- 64,793
- 6
- 84
- 100
Technically it's possible to push one or more files to the remote without committing. Taking an ignored file foo.txt
for example:
git tag xxx $(git hash-object -w foo.txt) -m "Track the content of foo.txt"
git push origin xxx
It creates a tag that points at the blob object that stores the content of foo.txt
, and pushes it to the remote repository. One can fetch the blob through the tag and read the content. However one cannot know how to use the content without extra instruction.

- 27,194
- 6
- 32
- 53
-
-
I made a directory for testing this, then I initialized it, I created two sample files: "foo.txt' and "test.txt" then created .gitifnore and excluded foo.txt. I added and committed changes then ran this command: git tag xxx $(git hash-object -w foo.txt) -m "Track the content of foo.txt" and then "git push origin master". But when I clone the origin branch there isn't any foo.txt file. – Shuaib Jaff Nov 23 '20 at 12:53
-
@ShuaibJaff the file's content (not its name) is stored in the tag `xxx`. You can clone the repository and use `git show xxx^{}` to print the content. – ElpieKay Nov 23 '20 at 13:07
-
Thanks for your answers. I cloned the repository and used `git show xxx^{}` but shows me this error: `''fatal: ambiguous argument 'xxx^{}': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: git
[ – Shuaib Jaff Nov 23 '20 at 15:54...] -- [ ...]' `. I'm not so familiar with git tags and working with them. -
@ShuaibJaff Sorry, I missed your push command. It should be `git push origin xxx` instead of `git push origin master`. However, I don't think my answer is a good solution to your needs. – ElpieKay Nov 24 '20 at 01:49
-