Fork a github repo and push to my private repo - github

I am taking a class which has a classwide github repo to publish labs, docs, etc. I want to fork this, do my own work on the labs, and push to my private git repo. However, I still want to be able to pull changes from the class github. Is there a way to do this?
Thanks

set up two remote branches, one for your private and one to the class
git remote add classwide sshblah
git remote add private sshblah
then you can
git fetch classwide
to grab your class stuff and
git merge localbranch
to work on it, then you can
git push localbranch private
to put it into your private repo

Yes, for sure, the key is in your repo remotes, so your fork have a remote called "origin" and for the original repo create a remote called upstream.
create the upstream remote with following command
git remote add upstream https://github.com/user/original.git
Then validate your remotes
git remote -v
origin https://github.com/your-user/fork.git (fetch)
origin https://github.com/your-user/fork.git (push)
upstream https://github.com/user/original.git (fetch)
upstream https://github.com/user/original.git (push)
So now you can push and pull from the original repo
git pull upstream branch
git push upstream branch
In the other way you can create a pull request directly in Github

Related

GitHub: how to fork public repo to enterprise, edit enterprise version but still update with public fork

This is a follow up to the question Is it possible to fork a public GitHub repo into an enterprise repository?
I want to fork the public project github.com/foo.git and work on it on my enterprise server, under the name github.enterprise.com/bar.git. However, I want to keep them linked so I can pull any changes on the master. In other words, I am going to create some new codes and edits in my enterprise bar.git, but whenever the authors of foo.git update their master repo, I want to pull those changes. So I followed the following steps.
Create an empty repository on github.enterprise.com/bar.git
Fork github.com/foo.git on my public GitHub account github.com/MyPublic/foo.git
Rename my public repo as to bar.git so it's github.com/MyPublic/bar.git
Create a bare clone of my new fork on my computer
git clone --bare https://github.com/MyPlublic/bar.git
Change directories into the bare clones folder:
cd bar.git/
Push the repository with the --mirror flag to my enterprise GitHub
git push --mirror https://github.enterprise.com/bar.git
git remote set-url --push origin https://github.enterprise.com/bar.git
Now I have the bar.git/ directory on my computer, which is BARE:master and not an editable version of the repo. I have bar.git repos on my personal public GitHub.com account and on my Enterprise server. The next things I need to do are (1) clone the enterprise repo onto my computer and edit it; (2) commit and push those changes to the enterprise repo; and (3) I also want to pull changes from the master foo.git repo when they happen.
I'm assuming I just do a standard clone, commit, and push for (1) and (2), i.e.
git clone GitHub.enterprise.com/bar.git
git commit
git push
Is it actually possible to do #3? I'm thinking that in my computer's bar.git/ directory, I execute
git fetch -p origin
git push --mirror
and then in the bar/ directory, I execute a standard git pull?

How to remove Github Fork Workflow & point the same local clone on my system to main repository

I forked from main repository and cloned the forked repository on my system. I am not liking the the Github forked workflow and want to move back to normal Github Workflow.
I do not want to delete my forked repository from system and clone from Main Repository.
Is there any way to point my local setup(from forked repo) to Main repo(from which i initially forked) without cloning(fresh setup) from Main Repository?
Yes, simply replace your remote origin by your original main repo URL, using git remote:
git remote set-url origin /url/main/repo
git remote remove upstream
git for-each-ref --format='%(refname:short)' 'refs/remotes/upstream/*' |
xargs git branch -D
Then you can git fetch/git push directly from/to that main repo, and no longer to your fork.
That is provided that you own the main repo as well, and have the right to push back to it.
Don't forget that you don't have to clone a repo to make pull requests: you can make pull requests between branches of your main repo directly (share repository model).

github fork confusion

I followed this https://help.github.com/articles/fork-a-repo post to clone a repository locally. After doing that another developer created a branch to the main repository and added some features to that branch. My question is
How do I get that branch into my fork.
Can I get that missing branch again to my local using git pull upstream/missing_branch command?
Thank you
You need to add a remote repo 'upstream' in the local repo (which has for origin your fork)
(git remote man page)
git remote add upstream url://upstream/repo
The OP opensourcelover mentions seeing this:
git remote -v,
origin git#github.com:username/project.git (fetch)
origin git#github.com:username/project.git (push)
upstream git#github.com:username/project.git (fetch)
upstream git#github.com:username/project.git (push)
If your origin is the same as your upstream remote repo, you can replace that url by the https one for that upstream:
git remote set-url upstream https://github.com/originalDevName/originalRepoName
That way, you can git fetch upstream and get the new branch.
If you need to work on that new branch, you can now declare it:
git branch -u upstream/foo foo
See "How do you make an existing Git branch track a remote branch?".

I've fork a repo in github. How could I fetch all changes from the original repo?

In this case, I've fork grocery-CRUD repo from github.
I make some changes, and submit a pull request.
The author accept it, and do some other modification.
How could I fetch the newest modification to my grocery-CRUD repo?
Thanks
You add his repo as a remote (git remote add <his repo> <repo spec>).
Then, you pull changes from his repo (git pull <his repo>)
Then, you push the changes to your repo (git push origin)
git pull git://github.com/scoumbourdis/grocery-crud.git
This will fetch the changes and merge them with your current branch in your local repository.
If you want to get it on your remote you need to push
git push origin

Having a private branch of a public repo on GitHub?

I have a public PHP project in a GitHub repo, which contains just one branch (master).
I want to have a separate branch/fork that is private for me (I have paid for private GitHub repos). I would like to be able to merge changes from the private branch/fork to the public repo, and vice versa.
With that in mind, here are my questions:
Can I have a private branch on a public repo?
Can I fork my own public repo into my own private branch/fork?
If both of the above are possible, which is the best way forward? If neither, how should I proceed?
Duplicate your repo.
Make the duplicated repo a private one on GitHub.
Clone the private repo to your machine
Add a remote to your public repo (git remote add public git#github.com:...)
Push branches with commits intended for your public repo to that new public remote. (make sure you don't accidentally commit private-only code)
You can bring in changes to your public repo using 'git fetch public' and then merge them locally and push to your private repo (origin remote).
Is it possible to have a private branch on a public repo?
On GitHub, your repository is either public or private; you cannot selectively "privatize" just a branch.
Can I fork my own public repo into my own private branch/fork?
You can clone your public repo to your local machine, branch as needed, and simply not push your "private" branches upstream (by specifying which branch to push to origin: git push origin master or git push origin branch-i-want-to-be-public:master).
Which is the best way forward/how should I proceed?
In order to take advantage of GitHub for both your public and private development, I would suggest forking your public branch within GitHub, changing the settings of the new fork to "Private", and then cloning the private version down to your local machine. When you're ready to make changes public, push everything up to your private fork on GitHub and then use pull requests to selectively copy branches to the public repo.
To make a repository private on GitHub, you must have an upgraded (paid) account. If you're only rocking the free account, you can still use the first process I suggested — clone public to local machine, branch, and push specific "public" branches to origin — without needing a private repo.
If you have a paid GitHub account, or are using another service that offers public and private forks and pull requests (such as BitBucket), then you can use either of the above approaches to make your code public.
There is another solution which I find better as it doesn't result in duplicate repos on the same machine.
Make a branch with the stuff you want private.
Make a new repo on GitHub, set it to private.
Add new GitHub repo as a second remote to your repo on your machine.
Push private branch to second remote.
End result is 1 repository with 2 remotes. 1 public, 1 private.
Just need to be careful about which you push to so name accordingly.
1.) Is it possible to have a private branch on a public repo
From what I know, no.
2.) Can I fork my own public repo into my own private branch
No, you can't fork a full repo (1-n branches) into a single branch. Well actually you could, if you just fork the one branch of the full repo. Just add it as a remote or start from a clone.
You might also be interested in Sparse checkouts.
3.) If both the above the are possible which is the best way forward
n/a
4.) If neither are possible how should I proceed?
n/a
To create private branch (downstream) of a public repository (upstream):
Initialize repository
$ git init private-repo
$ cd private-repo
Add remotes
$ git remote add upstream git#github.com:<username>/public-repo.git
$ git remote add origin git#github.com:<username>/private-repo.git
$ git remote --verbose
Initial commit
$ git commit --allow-empty --message "Initial commit"
$ git push --set-upstream origin master
Create development branch
$ git checkout -b develop
$ git push --set-upstream origin develop
$ git branch --all
Merge upstream into development branch
$ git fetch upstream master
$ git merge --allow-unrelated-histories upstream/master
$ git push
Some changes on repository...
# Do some changes...
$ git add .
$ git commit -m "Some changes"
$ git push
Apply upstream changes...
$ git fetch upstream master
$ git log --all --graph --oneline
$ git merge upstream/master
$ git push
Merge development branch to master
$ git switch master
$ git merge develop
$ git push
$ git log --all --graph --oneline
For next clones:
Clone repository
$ git clone git#github.com:<username>/private-repo.git
$ cd private-repo
Add upstream remote
$ git remote add upstream git#github.com:<username>/public-repo.git
$ git remote --verbose
Switch to development branch and get upstream changes
$ git switch develop
$ git fetch upstream master
$ git log --all --graph --oneline
$ git merge upstream/master
https://gist.github.com/amir-saniyan/a3c76b7e545e3d84f82f27e6c6579245