2. Using git pull and push with GitHub
Part 1 illustrated how to create a new git repository on your local computer. In this part we’ll create a new repository on GitHub, clone it to our local computer, make changes, then push it back to GitHub. This is the more common way to create git repositories on GitHub.
Prerequisites: GitHub Authentication
In order to do this part you will need a means for authentication in order to push things to GitHub. We recommend one of two ways:
- Set up an ssh key pair with GitHub: This involves creating an ssh key then uploading the public key to GitHub. This method has the advantage that it will allow you to push things from a remote computer cluster if you are doing work on a cluster. However, there is some set up involved. For details on how to do this, see the ssh setup instructions.
– OR –
- Use VS Code: VS Code has in-built GitHub authentication. To use it, you will clone repositories using the “Git Clone” command in the command pallette. The first time you do this, you’ll be prompted to log into GitHub. From then on, you’ll be able to clone repositories to folders you select through VS Code. No other setup is needed. Instructions are provided below on how to clone the repository in this way.
I. Create a repository on GitHub
Go to https://github.com and sign in if you are not already. Then go to https://github.com/new to create a new repository.
In the Owner section, click the drop down “Choose an owner” and select yourself.
You’ll need to put a name in the “Repository name” field. GitHub will offer a suggestion (where it says “Need inspiration? How about …”). Use the suggested name. That should ensure that the repostiory name is unique, which will make doing Exercise 3 easier.
Under “Description” put “My test git repository.”
Make sure “Public” is selected.
Check the box for “Add a README file”. This will cause your repo to be initialized with a README.md file in it. It isn’t strictly necessary, but is good to have, as it’s what GitHub will show by default when people go to your repository.
Under “Choose a license” select “GNU General Public License v3.0”. This will cause your repository to be initialized with a LICENSE file that has the GNU Public License v3.0 in it (an open source license). Choosing a license isn’t strictly necessary, but it’s good practice.
Now click the “Create repository” green button. This will create your repository. You will have the repository webpage open.
II. Clone the repository to your local computer
We want to add things to your repository. You can do that through the web interface, but it’s easier to do it on your local computer, esepcially for code development so that you can test things. To do that we’ll need a local copy of your repository. This is what git clone does: it downloads a local copy of your remote repository (which lives on GitHub) that can track the remote repository.
How to clone the repository depends on if you chose to use an ssh key or to use VS Code in the Prerequisites section above.
If you are using an ssh key
To clone the repository, click the Green
<> Codebutton in the upper right of the repository’s webpage on GitHub, then select the “SSH” tab. We’ll want the SSH version so that we can push changes later.Copy the URL in the SSH tab. Now open a terminal (you may want to open the terminal side-by-side with your web browser; it’ll make doing the rest of this exercise easier).
cdto the directory that you want your local copy of the repository to live, then type:
git clone SSH_URLwhere SSH_URL is the URL you copied from GitHub.
This will download a copy of the reposotry to the folder you are in. The repo will be in a subfolder with the same name as the repository.
If you are using VS Code
Click the green “Code” button in the upper right of the repository’s webpage. That will open a drop-down menu. Make sure the HTTPS tab is selected, then copy the URL that’s listed there (you can just click the copy icon to the right to copy the URL).
Open VS Code.
In VS Code, open the command palette by hitting Ctrl/Command + Shift + P on the keyboard. In the command palette prompt at the top, type
gitcl, then select “Git: Clone” from the drop-down menu.
- If you have not logged into GitHub previously in VS Code, you will be prompted to log in. If so, log in. After you do, you’ll need to open the command palette again, type
gitcl, and select “Git: Clone”.
Paste the URL you copied from GitHub into the command palette then hit Enter.
You will be prompted where to store the repository. Select the folder that you would like to download the repository to. Then click the “Select as Repository Destination” button.
You will be asked whether to open or open in a new window. To make sure we’re not mixing up projects, select “Open in a New Window”. This will open a new VS Code Window with the contents on the cloned repository.
III. Add files to the repository
When git clone finishes, you will have a new directory that has the same name as your repo on your computer.
cdinto that and typels. You should see theREADME.mdfile and theLICENSEfile.This is your local copy of your repository. It’s setup to track your repo on GitHub, which is called the
originrepository. You can verify that by typing:
git remote -vThat will show you the URL of the origin. Note that this is printed twice: once for fetching (i.e., the default repo you will pull new updates from) and one for pushing (i.e., the repo to which you will push changes from your local computer).
- Now let’s add a file. In your terminal, use your favorite text editor to create a file called
mymath.Rand put the following in it:
!#/usr/bin/env R
# Some math functions
add <- function(a, b) {
return (a + b)
}This is a simple R program that contains a function to add two numbers.
- Add and commit your file to your repo:
git add mymath.R
git commit -m "add mymath file"Check your history to see the commit by typing git log.
- Now look at your copy of the repository on GitHub in your web browser. Is
mymath.Rthere? No. Try refreshing the page. Is it there? Still no. This is because you have only commited the file to your local copy of the repository. The origin repository on GitHub doesn’t know about these changes. To get the changes on to your origin repository, type:
git pushGo back to your web browser and refresh your repository page. You should now see the mymath.R file there. You can click on it to view it if you like. You can also see your commit history on GitHub (click the icon that looks like a clock just below the green <> Code button on the front page of your repository). Notice that the history on GitHub is the same as your history on your local computer. Your repos are in sync!
Let’s create a branch to do some development work. We could do this on our local computer, but let’s do it on GitHub instead. Click the drop down button that says “main” in the upper left corner of the website. Then click “View all branches”. This will bring you to another page that shows you all your branches. Currently we only have one,
main. Create a new branch by clicking the greenNew branchbutton in the upper right. In the “New branch name” field typedev1then clickCreate new branch. This will create the new branch.Now let’s make some changes on the
dev1branch on our local computer. To do that, we’ll need to update our local repo to pickup the new branch. Go to your terminal and type:
git fetchYou’ll see a message telling you about the new branch origin/dev1. You still don’t have this branch on your local repo! If you type git branch, you’ll only see * main. The fetch command just informed your local repo about the existence of the dev1 branch on GitHub. You can verify this by typing git branch -r to see the remote branches.
- Let’s checkout a local copy of the
dev1branch. Type:
git checkout -b dev1 origin/dev1Adding the extra origin/dev1 instructs git to make your local dev1 track the remote one. This means that when we git push from this branch it will by default push changes to the dev1 branch on GitHub, not the main branch. You can verify which branch you will push to by typing:
git branch -vv- Let’s modify
mymath.Rondev1. On your local computer, use your favorite text editor to openmymath.Rand add the following to it:
subtract <- function(a, b) {
return (a - b)
}Commit your changes to your local repo:
git add -u
git commit -m "add subtraction"- Again, your changes to your local
dev1are not on GitHub yet. To do that, push the changes by typing:
git pushNote that when you did this, it pushed to the dev1 branch on git hub (you’ll see a message like dev1 -> dev1).
Now look at your web browser. As soon as you typed
git pusha message popped up that said “dev1had recent pushes” with a greenCompare and pull requestbutton popping up. Click that button. This will open a “pull request” page. The pull request page will automatically have a title that is the message from your last commit. You can optionally add a longer description. Then click theCreate pull requestbutton.This will open “Pull request”. Pull requests are GitHub’s way of managing new development. This is particularly useful for collaborating with others on a project. You can use the pull request page to discuss changes before they are merged on to the main branch. You can alos have multiple pull requests open at once.
Let’s merge your pull request. There’s a green
Merge pull requestbutton at the bottom of the page. Click it, then “Confirm merge”. This will merge your changes ondev1on tomain. You now have the option to deletedev1. Feel free to click the delete dev1 branch button.Now that your
mainbranch on GitHub has the merged changes, yourmainbranch on your local copy is out of sync. To update it, go to your terminal and switch to your main branch:
git checkout mainYou can now bring it up to date with your remote repository by running git pull:
git pullOnce that’s done, you can also delete your local copy of the dev1 branch by typing:
git branch -d dev1