Git - Clone, Forks, and Pull Requests

June 7, 2023

In my last post, we covered the basics of stashes in Git. In this post we're going to go through the basics of Cloning a repository, creating a Fork of an already established repository through a different GitHub account that doesn't own that repository, and creating a Pull Request.

 

These topics align nicely with the 30,000 foot view of working in teams and collaboration on software projects with multiple developers or software engineers.

 

For the purposes of this example, we're going to color code our GitHub accounts, file directories,  and files inside the directories to bring more clarity to the moving pieces.

 

Example of Working in Teams

 

For this scenario, we're going to use two different GitHub accounts which represent two different collaborators working on a project that lives inside a repository on GitHub.

 

I have created two different GitHub Accounts that align with our repositories:

  • CodingInGreen
  • CodingInBlue

 

We're starting off with the CodingInGreen GitHub user account.

 

I have created a bash script on my desktop that allows me to change GitHub accounts on my local MacOS operating system.  It allows me to delete the GitHub credential in the MacOS Access Keychain for github.com and then inserts new values for a different GitHub account.

 

How to Clone

 

The first thing we'll do is go to GitHub and login with the CodingInGreen account and create a new repository called "green". We're then going to go to the "Code" button in the GitHub repository, click it and copy the URL of the "green" repository.

 

 

We're next going to navigate to the Desktop folder, and clone the repository. The clone command allows developers to create a local copy of a repository from the code hosted on GitHub. It establishes a connection between the local and remote repositories on GitHub.

 

 

Now we see that we've created a folder on the desktop named "green", and it has pulled down the files from the GitHub repository. At this time there is just a README.md file so that's all it pulled down.

 

How to Fork

 

I've now switched to the CodingInBlue GitHub user account on my local machine and I've navigated back to the Desktop.

 

I've logged into GitHub with the CodingInBlue account, and the first thing I'm going to do is search in the search bar for the previous repository that we created and cloned, the CodingInGreen/green repository.

 

After we enter our search, we see that one result has populated. Notice in the top right corner of the screenshot that I'm in the CodingInBlue GitHub account, but the repository I'm seeing from my search is a repository owned by the CodingInGreen GitHub account.

 

Now we'll click into the green repository, and up at the top of the GitHub page, there is an button "Fork". We click the button, the menu will expand, then we click on "+ Create a new fork". Forking a repository is the process of creating a personal copy of a repository under your GitHub account.

  

Now we have the option to give a unique name to our personal repository, a identical copy of the "CodingInGreen/green" repository.

 

Let's name it "blue-forked" since we're doing this from the CodingInBlue GitHub account. And in the description we'll type "Forked repository from CodingInGreen/green". Click the"Fork" button at the bottom, and GitHub will process the request. After the request has processed, we'll see the "blue-forked"repository in the CodingInBlue account.

 

 

Now we'll navigate back to the desktop and clone the "blue-forked" repository.

 

 

And now we have two different folders on our desktop. One named "green" which is connected to the repository owned by CodingInGreen, and another named "blue-forked" which is connected to the repository forked by CodingInBlue.

  

The next step we'll take is to create a new Git branch in the local repository in the"blue-forked" folder. This step will help keep our changes separate from the original repository and makes it easier to manage the pull request that we'll cover next. We'll name our new branch "blue-experiment".

 

Now we'll use the ls command to list the blue-forked directory's contents, and we see that we have the "README.md" file that was cloned from the blue-forked repository on the CodingInBlue GitHub account. Next we'll use git branch to confirm that we're on the blue-experiment branch. Lastly we'll create a new file in this blue-forked repository blue-file.js, add it and commit the file to Git.

 

 

We'll now push the blue-experiment branch up to the blue-forked repository.

  

Pull Requests

 

A pull request is a mechanism for proposing changes to a repository when you don't own that repository. It allows you to submit your modifications to the repository owner for review and integration. Since the owner of the original repository is CodingInGreen, we'll go back to GitHub and search for the CodingInGreen/green repository, but we're doing it from the CodingInBlue GitHub account. Here's what the page looks like, we can see the pushed experiment branch.

  

Next we'll click on"Pull Requests" and here's what we see.

 

Now we see that in the CodingInBlue account the push that we did with the blue-experiment branch.We'll click "Compare and Pull Request" to take us to the next screen.

 

 

Now we can see at the top we can select our repositories and branches to compare, and we see the  commit message "Add blue-file.js" Here we'll add the description "Adding blue-file.js from the CodingInBlue GitHub Account in the blue-forked repository in the blue-experiment branch into the CodingInGreen/green repository. We'll then click "Create pull request".

 

We'll now see that the pull request is in an "Open status" for the owner of the CodingInGreen/green repository to review.

  

We'll now logout and log back in as CodingInGreen and navigate to the "green" repository and click on the Pull Request button to view our open Pull Request created by CodingInBlue.

 

 

The last step is to click on the open Pull Request, review the request, and if we agree with the changes, which we do, we'll click the "Merge pull request" button and then the "Confirm Merge" button.

 

Now we see that the pull request has been merged.

  

The GitKraken Client is helpful tool in visualizing what's happening in Git. Here is a link to the GitKraken Client, and it's free to use for public repositories:

 

https://www.gitkraken.com/git-client

 

Here's a view of the green folder on my local desktop from the GitKraken Commit Graph:

  

Here's a view of the blue-forked folder on my local desktop from the GitKraken Commit Graph:

We'll be getting into some more complicated examples soon, hope this was helpful!