profile

Hi! I'm Moncef Belyamani

GitHub 101: Cloning and Forking

Published about 3 years ago • 2 min read

Hi Reader, how's your weekend so far? Here is this week's guide.

GitHub 101: Cloning and Forking

Common questions asked by people who are new to GitHub include:

  • "Should I fork or clone when working with other people's repos?"
  • "After cloning and making changes, why do I get a permission denied error when trying to push the changes back up to GitHub?"

Cloning and forking are two ways to make a copy of a repo, but they behave differently in terms of who can make changes and where the changes can be made.

Cloning

When you clone someone else's repo, you are making a copy of it on your computer. You can play around with the code, and even make changes to it on your own computer, but you can't push any changes back up to the repo on GitHub, because it still points to someone else's repo under their own GitHub account. When a repo exists in multiple locations besides your computer, those other locations are called remotes. To see which remotes exist, run this command:

git remote -v

When you clone a GitHub repo, it will include a remote called origin that points to the repo on GitHub. For example, if you clone the Rails repo, it will look like this:

origin  https://github.com/rails/rails.git (fetch)
origin  https://github.com/rails/rails.git (push)

Unless you have direct access to a GitHub repo (because you are the owner, or someone added you as a collaborator), you cannot push directly to it. However, you can create a new remote that points to a location that you have access to, and push your changes there. For example, you could create a new repo called rails under your own GitHub account, and then create a new remote called myrails. Then, to push the changes, you would use git push myrails instead of git push origin. But this is tedious. The easier way is to fork!

Forking

At the top right of any GitHub repo, there is a Fork button. If you hover over it, it will say Fork your own copy of [repo name] to your account. With a couple clicks, you can have a copy of a repo on your own GitHub account, which means you can push any changes to it, without affecting the original repo. Once you fork a repo, you will then want to clone it on your computer, and if you run git remote -v, you will see that the origin remote now points to your fork, which means you can push changes directly to it.

Another reason to fork is when you want to contribute to someone else's repo. For example, to fix a typo or bug, or to improve the project in some other way. Since you have direct access to your own fork, you can push the changes to it, and then you can submit a Pull Request to the original repo, which lets them know you are proposing changes, and they can decide whether or not they accept your changes, or work with you until the changes are acceptable. This is a more advanced topic that I will go into in a future guide.

For now, I wanted to give you a quick intro to the difference between forking and cloning, and when you would want to use one or the other. I've also created a visual decision tree that might help. Let me know what you think!

Thanks for reading, and have a wonderful day!
Moncef

Hi! I'm Moncef Belyamani

Every week, I send out an automation tutorial that will save you time and make you more productive. I also write about being a solopreneur, and building helpful things with Ruby. Join 2853 others who value their time.

Read more from Hi! I'm Moncef Belyamani

Hi Reader 👋🏼 Happy Sunday! I hope you and your loved ones are doing well. Earlier this week, I found a great use case for the 1Password CLI that hadn't occurred to me before. I'm gonna use it a lot more often whenever I can! If this email doesn't look right, or if you prefer reading on my site, you can click the title link below. Automate GitHub API Calls With Ruby, Keyboard Maestro, and 1Password CLI One of the perks of the “Ultimate” version of Ruby on Mac is access to the private GitHub...

over 1 year ago • 5 min read

Hi Reader! This week's automation guide is about a free but powerful Mac app called Bunch. I had heard of it years ago but never took the time to explore it in detail. Until now, and it has proven very useful so far. I'm not sure how the code samples will look like in your email, so you might prefer to read this on my site by clicking the title below. Automate Context Switching With Bunch You sit down to work on a feature, and wake up your Mac. Oh hey, Slack is open. You decide to check it...

over 1 year ago • 12 min read
PopClip default extensions

Hi Reader! This week's automation guide is about a little-known app called PopClip. PopClip was originally released in 2011, but I didn’t hear about it until four years ago, and I’m sure there are still a lot of people who don’t know about it. It’s one of the many useful apps you can discover and quickly install with the “Ultimate” version of Ruby on Mac. You can pick and choose from hundreds of Mac apps, fonts, and dev tools in the included Brewfile, and Ruby on Mac will install them all at...

almost 2 years ago • 2 min read
Share this post