17.2 -- Git In Class
Git and GitHub Collaborative Practice¶
Clone a repo to try it out¶
Let's start by cloning a GitHub repository. This is a typical thing we would do if we wanted to try out a software tool that is publicly available. For this example we will use this class repository. First navigate to the class GitHub page at https://github.com/eaton-lab/hack-the-planet and click on the green "Code" button to get the SSH URL. Then open a terminal and clone it to a local directory on your computer.
git clone git@github.com:eaton-lab/hack-the-planet.git
A clone repo¶
We can now use the code in the repo, and we can even edit the code in this repo.
However, we do not have permissions to push changes we make to this code back
to the original repo. What if we decide that we do want to make changes and commit
them? This is where we would want to create a fork. What is a fork? It is just a
copy of the repo that is hosted under your GitHub profile, so that you have ownership
of it, and is linked to the original repo, so that you can propose changes back to it.
Once a repository exists in multiple locations, we refer to these as remotes. The
original remote is typically called origin. Other remotes can be called anything
you want. You can link multiple remotes to a single repository, such that you pull and
push code to each selectively. We can use the command git remote
to view the remotes
that are currently linked.
# view the remotes
git remote -v
origin git@github.com:eaton-lab/hack-the-planet.git (fetch)
origin git@github.com:eaton-lab/hack-the-planet.git (push)
Create a fork¶
Let's create a fork. Navigate back to the GitHub page and this time click on the "Fork" button in the upper right. This will create a fork, and navigate to a new page where your fork is located. Notice that it looks pretty much the same, but your username appears near the top. Once again, click on the green Code button to copy the git SSH URL. Now, we will add this URL as a remote to our repo.
git remote add "upstream" {pasted-url}
# view the remotes
git remote -v
origin git@github.com:eaton-lab/hack-the-planet.git (fetch)
origin git@github.com:eaton-lab/hack-the-planet.git (push)
upstream git@github.com:USERNAME/hack-the-planet.git (push)
upstream git@github.com:USERNAME/hack-the-planet.git (push)
Make edits on a branch¶
Now we can make edits to the code and push our changes to our forked repo. Let's start by editing the REAMDE.md file. Don't worry about what the changes are for now, this is just for practice. Open the README in any text editor and simply add a few characters of text to it, save, and close the file. Then let's add our changes to git. BUT, notice that when call push here we will specify the 'main' branch on the 'upstream' remote.
git add README.md
git commit -m "fixed typo in README"
git push upstream main
View the fork and origin on GitHub¶
Now that many of us have pushed our commits to a fork, let's look at what we see on GitHub. You should see on your fork that it is one commit ahead of the origin. It will also suggest that you make a pull request to the origin. Let's follow the instructions to do that.
Reviewing a pull request¶
Next, let's check what the owner of the origin repo sees. They will see all of the pull requests made by users and have the option to accept them, just like we did in our first exercise today.