As a student using a course's GitHub Repo, should I clone or fork a repository to persist personal studies?

Viewed 234

I use several books for coding studies. Many of them have coding examples that link back to a GitHub, for example: https://github.com/ehmatthes/pcc_2e/

If I want to make changes to the code, for example add comments, experiment with the code some more etc. for personal use, should I fork or clone the repository in order to keep my changes in the cloud?

If I clone, it seems I can only keep changes in GitHub if I commit and push to a branch, which I don't want to do since I'm not fixing or enhancing anything.

However if I fork, I believe forking implies I also want to contribute back, so I'm not sure if that is right. I'm aware I can commit and push to my fork without doing a pull request back to the original, however I'm not sure if conceptually that is the correct way, since Forks again are meant to be potential code contributions to the original author.

TLDR: I want to get the latest course code from a GitHub repo, play with the code in some files, and have my notes\code changes on GitHub. How do I meet those needs in GitHub?

3 Answers

To clone is simply to download it locally and use it. The content of the repo can be enhanced anytime. And this will not be reflected in your Cloned Directory.

So, I think you should prefer to fork the course repository. This will also help you to see any new changes that are made to that repository and stay updated. Here are the steps to sync your repo with the original one, without loosing out on local changes. Later on if you wish to contribute, you can do that too.

Github makes it kind of confusing, but the reality is that they are the same thing: a fork is a clone. Git itself doesn't know anything about forks, which are a concept invented by Github. Forks help facilitate the centralized workflows that Github is designed to use, but which are not built into git itself.

From the perspective of a clone on your local system, then only real difference is going to be the assignment of the remote 'Origin', ie. the repo that you cloned from. It will determine what happens when you use git push, git pull git fetch etc... without explicitly naming a remote URL.

If you directly clone the ehmattes/ppc_2e repo on your local system, git will assign that as Origin. When you git pull, it will fetch from ehmattes/ppc_2e. If you git push, it will try to write your updates to ehmattes/ppc_2e (which will most likely be rejected).

If you make a fork in Github (milesMorales/ppc_2e) and then clone your fork locally, the fork becomes the remote 'Origin' instead. pull and push operations will use your fork. But the fork as it exists on Github is really just a clone of the original repo, so your local copy is actually a clone of a clone. The advantage is that you can make any changes you like locally, and then push them back to the cloud (github) where they are written to your forked copy. The downside is that if ehmattes makes changes to the course content, you won't see it unless you update your fork.

Hypothetically, if you wanted the changes you made to milesMorales/ppc_2e to be merged back into ehmattes/ppc_2e, you would use GitHub to raise a pull request from milesMorales/ppc_2e into ehmattes/ppc_2e1.

There are a few ways to set things up to get around the disadvantages of either option, mainly involving the use of more than 1 remote configured on your local copy. But that is probably outside the scope of the original question.

I want to get the latest course code from a GitHub repo, play with the code in some files, and have my notes\code changes on GitHub. How do I meet those needs in GitHub?

You want to make a fork on Github, and then clone your fork on your local system and do your work there. git push will write your changes to your forked copy. Just be aware that you won't see any updates made by the author unless you manually update your fork.


1: pull requests are another GitHub concept. Whereas 'fork' is a wrapper around git clone, a pull requests is a wrapper around git merge (or git rebase).

If you want to have a repo with write access on GitHub : you need to fork the original repo.

You can then add the original repo as an additional remote :

git remote add upstream https://GitHub.com/the/repo

# after that, you can run :
git fetch upstream
# to update your local repo with its content
Related