Pushing Commits to GitHub7:50 with Jay McGavren
Now we'll show you how to add a GitHub repo as a remote, and push commits there.
- After setting up a repo and making some commits on your local machine, you create a new, empty repo on the hosting service's server.
- Then you add the new repo as a remote on your local repo, and push your commits there.
- Other developers can clone this central hosted repo to their machines.
- As you make more commits on your local repo, you can push them up to the central repo.
- Other developers can then pull the commits down to their cloned repos so they're up to date.
Creating a GitHub repo
Let's create a repo on GitHub, so we can push our
medals repository code to it. To create a repo on GitHub, you'll need to create an account. Just visit https://github.com in your web browser, click the "Sign up" link [click], and follow the directions.
- Once you're signed up, we need to create a new repo. From any page on
github.com, there should be a menu with a "New repository" link. Click that.
- We'll be asked to enter a repository name. Ideally repo names are all lower-case, and if there are multiple words they'll be separated with dashes.
- We can use any name we want, but let's just use
- We'll enter a quick description of the repo: "Website for medals store."
- Now you can choose whether to make the repo public or private. Unless you have a paid account, you'll want to leave it set to "public".
- You may see a check box allowing you to "initialize this repository with a README".
- It says to "Skip this step if you're importing an existing repository", which we are, so leave it unchecked.
- There may also be options to add a
.gitignorefile or a license file; you'll want to leave those set to "None".
- When you're ready, click the button to create a repository.
Adding the GitHub repo as a remote
- You'll be taken to the web page for your new repository. This is the URL where the public will go to view your project's source code in their web browsers.
- Since no code has been pushed here yet, you'll just see a set of tips on getting started. These include shell commands that have been customized to work for your repo. [Note: If you accidentally chose to create a
READMEor license files when creating the repo, you won't see directions. If this happens, see the Troubleshooting section below.]
- There's a section for use "if you've done this kind of thing before".
- There are two protocol options here, "SSH" and "HTTPS". Notice that the URLs in all the commands change depending on which option you select.
- To get started, we'll want to select "HTTPS", although you might want to switch to "SSH" later. See below if you want more info on SSH.
- Let's go to our terminal, and run the commands they suggest in the section to "push an existing repository from the command line". If you have 2 factor authentication on, you will need to get a personal access token.
git remote add origin [URL]
- As the GitHub instructions suggest, we'll give the remote repo a name of
origin. This is the same name that would have been set up by default if we had cloned our local repo from the GitHub repo.
- Now we need the second argument, the URL of the remote repo. We'll just copy the URL from the command shown in our browser. Then we'll paste it into the terminal. [Note for Workspaces users: there is a known bug that may prevent you from pasting into the Console. If this is the case, you may have to type the URL manually.]
- Once all that's typed in, press Enter to run the command and it will add the GitHub repository as a remote repo.
The second command GitHub recommends we run is a new command,
git push -u origin master
- Whereas the
git pullcommand "pulls" changes from a remote repo into your local repo, the
git pushcommand "pushes" changes from your local repo into a remote repo.
git pullgets changes made by others,
git pushshares changes you've made with others.
- GitHub is recommending a few arguments to
git pushas well.
- First is the
-ucommand line option. The
set-upstreamoption means that you want Git to remember the repo and branch you are about to specify, and make all future
git pushcommands push to that repo and branch by default. That way, you won't have to specify a repo and branch each time you run
- The next two arguments need to actually specify that default repo and branch.
- We'll use a repo of
- And we'll specify the only branch that exists in the remote repo, the one branch that gets created by default,
- We'll use a repo of
- When we run the command, because we used an
httpsURL for the remote repo, we'll be prompted for a user name. You should enter the user name you chose when setting up your GitHub account.
- Then we'll be prompted for a password. Again, enter the password you chose when setting up your account.
- If everything works, you'll see several messages as Git uploads our commits to the remote repo.
- GitHub is recommending a few arguments to
$ git push -u origin master Username for 'https://github.com': example Password for 'https://firstname.lastname@example.org': Counting objects: 41, done. Delta compression using up to 4 threads. Compressing objects: 100% (35/35), done. Writing objects: 100% (41/41), 4.31 KiB | 0 bytes/s, done. Total 41 (delta 14), reused 0 (delta 0) remote: Resolving deltas: 100% (14/14), done. To https://github.com/example/medals.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
Using SSH with GitHub
SSH is an alternate, secure way to connect to GitHub. It's convenient because once it's properly set up, you won't have to type your username and password every time you push or pull from a GitHub repo. But setting it up does require some experience with the terminal.
Sorry, but we can't help diagnose any issues you have with SSH; you'll be on your own.
If you want to set it up, GitHub has a tutorial here.
If you accidentally created one or more files in the GitHub repo...
You'll need to pull the remote files to your repo and merge them in before you can push. We haven't covered merging in this course, but we'll tell you enough to fix the current problem.
- On GitHub, click the "Clone or download" button and note the HTTPS URL for the repo.
- In your local repo, run
git remote add origin https://github.com/YOUR_USERNAME/YOUR_REPO_NAME.gitto add the GitHub repo as a remote.
- The GitHub repo contains some commits that you don't yet have locally. If you try to run
git push -u origin masterright now, you'll get this error:
To https://github.com/jaymcgavren/medals.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/USERNAME/REPO_NAME.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first merge the remote changes (e.g., hint: 'git pull') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
- As the hint suggests, run
git pull origin master. (You need to provide the remote name and branch name because an upstream repo and branch haven't been set yet.)
- Git will pull the commits from GitHub, and merge them with your local commits (meaning it will add the commits to your local history along with all your other commits).
- This triggers a "merge commit", so Git will launch an editor to get a commit message. Just save the default message, and exit the editor to complete the commit.
- Now you should be able to run
git push -u origin master, and resume following our tutorial directions normally.
A pre-populated GitHub repo
Practice is important - we encourage you to set up a repo of your own and follow along with the demonstration in the video.
We do realize this may not be practical for everyone, though. If you just want to browse the finished result, here's a GitHub repo we've set up.
We've shown you how to make repo clones so you can actively work in, and 0:00 how to pull commits from those clones. 0:03 But that's mainly useful while you're learning how git works. 0:05 It's far more common to host a central repo on a code hosting service. 0:09 GitHub is the most popular git service. 0:13 Other alternatives that work similarly include BitBucket and GitLab. 0:16 See that teacher's notes for info on those. 0:21 After setting up a repo and making some commits on your local machine, 0:23 you create a new empty repo on a hosting services server. 0:27 Then you add the new repo as a remote on your local repo and 0:31 push your commits there. 0:34 Other developers can clone this central hosted to their machines. 0:35 As you make more commits on your local repo, you can push them up to the central 0:39 repo, other developers can then pull the commits down to their cloned repos so 0:43 they're up to date. 0:47 Let's create a repo on GitHub so we can push our medals repository code to it. 0:49 To create a repo on GitHub, you'll need to create an account. 0:54 There are two kinds of accounts on GitHub, free and paid. 0:58 The paid plans let you host private repos, where no one else can see the code. 1:01 These are appropriate for companies working on proprietary code. 1:06 The free plans can only create public repos, 1:10 where anyone can browse the code on the web or clone it using git. 1:12 But for open source projects, this is actually preferable to a private repo. 1:16 So a free plan may work just fine for your needs. 1:20 The type of account you create is up to you, either one will work for 1:23 following along with this video. 1:27 The sign up process is pretty simple, just visit github.com in your web browser and 1:29 click the sign up link. 1:34 Then follow the directions. 1:36 Website is chain, so 1:37 what you see at github.com may not exactly match what's shown here. 1:39 But the basic process should be the same. 1:42 If you encounter any trouble while setting up an account or 1:45 your first repo check the teacher's notes for help. 1:48 Once you've signed up, we need to create a new repo on GitHub. 1:51 From any page on github.com there should be a menu with a New repository link. 1:55 Click that, we'll be asked to enter a repository name. 2:01 Ideally repo names are all lowercase, and 2:05 if there are multiple words they'll be separated with dashes. 2:07 We can use any name we want, but let's just use medals, after all, 2:10 that matches the name of our local copy of the repo. 2:15 We'll also enter a quick description of the repo, website for medals store. 2:18 Now you can choose whether to make the repo public or private. 2:25 Unless you have a paid account, you'll want to leave it set to public. 2:29 You may see a check box allowing you to initialize this repository with a README. 2:33 But it says to skip this step if you're importing an existing repository, 2:38 which we are, so we'll leave that box unchecked. 2:42 There may also be options to add a git ignore file or 2:45 license file, leave those set to none. 2:48 When you're ready, click the button to create a repository. 2:51 You'll be taken to the web page for your new repository. 2:55 This is the URL where the public will go to view your project source code in their 2:58 web browsers. 3:02 There's no code to view here right now though. 3:04 Instead, you'll probably see a set of tips on getting started. 3:07 These include shell commands that been customized to work for your repo. 3:11 There's a section for use if you've done this kind of thing before. 3:15 There are two protocol options here, SSH and HTTPS. 3:19 Notice that the URLs and 3:26 all the commands change depending on which option you click. 3:27 To get started, we wants to select HTTPS. 3:31 Although you might want to switch to SSH later. 3:34 See the teacher's notes from our info on these options. 3:37 There's a section that create a new repository, we already have a repository. 3:40 So we don't need that, we're going to want the section two, 3:44 push an existing repository from the command line. 3:48 Let's go to our console and 3:52 run the command they suggest, the first is a git remote add command. 3:54 As the GitHub instructions suggest, will give the remote repo a name of origin. 4:00 This is the same name that would have been setup by default if we had cloned our 4:06 local repo from the GitHub repo. 4:09 Now we need the second argument, the URL of the remote repo. 4:12 In the previous video, we just used the path of another directory. 4:15 But here we're going to use the URL provided by GitHub, so 4:18 that git can connect to it over the Internet. 4:21 We'll just copy the URL from the command shown in our browser. 4:25 Then we'll paste it into the terminal. 4:31 Once all that's typed in, press Enter to add the command, and 4:35 it will add the GitHub repository as a remote repo. 4:37 The second command, GitHub recommends we run is a new command, git push. 4:42 Whereas the git pull command pulls changes from a remote repo into your local repo, 4:47 the git push command pushes changes from your local repo into a remote repo. 4:52 Git pull gets changes made my others, 4:58 git push shares changes you've made with others. 5:00 Because git push makes changes of the remote repo without warning, 5:04 it's generally not used with clone repos that people work in. 5:08 Instead, it's used with repos that are hosted on services like GitHub. 5:11 GitHub recommends a few arguments to git push as well. 5:16 First is the -u command line option. 5:19 The u stands for upstream as in set up stream. 5:22 Just as a source of all the water in a river lies upstream of wherever you are, 5:26 upstream is a term that refers to a remote repo that 5:31 is the source of the code in this local repo. 5:34 The -u or set upstream option, means that you want git to remember the repo and 5:37 branch you're about to specify. 5:42 And make all future git push commands push to that repo and branch by default. 5:43 That way you won't have to specify a repo and branch each time you run git push. 5:49 The next two commands need to actually specify that default repo and branch. 5:54 So, we'll use a repo of origin and we'll specify the only branch that 5:58 exists in the remote repo, the one branch that gets created by default, master. 6:04 When we run the command, because we used an HTTPS URL for the remote repo, 6:11 we'll be prompted for a user name. 6:15 So I'll enter the name that I used when signing up for an account, 6:18 this username I'm entering won't work for you, of course. 6:21 You should enter the username you chose when setting up your GitHub account. 6:27 Then we'll be prompted for a password. 6:32 Again, enter the password you chose when setting up your account. 6:33 If everything works, 6:37 you'll see several messages as git uploads our commits to the remote repo. 6:38 Now let's go back to our web browser and refresh the page. 6:43 We'll see the GitHub repo has been updated. 6:49 All of our files are here along with other information. 6:52 We'll explore more of that in the next video. 6:56 Let me show you something back on our console. 6:59 Notice the message here at the bottom, 7:02 branch master set up to track remote branch master from origin. 7:04 That's there because we passed the -u option to the git push command. 7:09 It means you won't need to specify the repo or branch names from now on. 7:13 You'll be able to just type git push by itself, 7:17 you'll still need to enter a user name. 7:20 And a password, and it will push to the origin repo's master branch by default. 7:25 In this case since we haven't made any new commits it will just say 7:32 everything up to date. 7:35 By the way, if you'd rather not type a username and 7:37 password each time you push, see the teacher's notes for some tips. 7:39 Now we know how to push our changes up to GitHub, but 7:43 what exactly does that do for us? 7:46 We'll show you in the next video. 7:48
You need to sign up for Treehouse in order to download course files.Sign up