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

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
User avatar
user1234
Posts: 414
Joined: Sat Feb 26, 2022 5:48 am
Location: Somewhere on earth
Has thanked: 155 times
Been thanked: 88 times

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

Post 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?

PuppyLinux 🐾 gives new life to old computers ✨

User avatar
Phoenix
Posts: 341
Joined: Fri Feb 12, 2021 2:03 am
Location: Canada
Has thanked: 4 times
Been thanked: 48 times

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

Post 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.

IRC: firepup | Time to hack Puppy!

Post Reply

Return to “Programming”