Page 1 of 1

How to push only a single file of a commit to different repository using git?

Posted: Sat Apr 23, 2022 9:54 am
by user1234

For example I have a directory like this-

dir
|---- subdir1
| |------- file1
|---- file2

I used git to add all files and folders, and committed them all, and then pushed it to a local github repo that I want to keep for myself to work over different devices. Now I made a program, say file2, that I want to show at my school, and obviously there I can't login to my account to look over the local repo, nor do I want to change my repo from local to public as it contains few waste files as well, for example- HelloWorld, few books, etc. For this I want to create a public repo and push only file2 to the public repo. How can this be done? Or would I have to create a different dir sibling to the parent dir and move file2 there, and then do git stuff?


Re: How to push only a single file of a commit to different repository using git?

Posted: Sat Apr 23, 2022 8:44 pm
by Phoenix

You can cherry-pick commits and push those. Via git cherry-pick <hash>.
But this means that the said commit must only contain said file and nothing else, otherwise you'll need to make a commit to remove extraneous files and push that.