> For the complete documentation index, see [llms.txt](https://certil-remy.gitbook.io/learn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://certil-remy.gitbook.io/learn/git-and-github/untitled-5.md).

# Git Remotes and Github Codealong

## Objectives

1. Describe GitHub and its relationship with Git
2. Create a remote repository on GitHub
3. Use `git push` to connect your local repository of files to your remote repository
4. Use `git remote`
5. Use `git push`
6. Use `git pull`

Let's go through an example to clarify remote repositories on GitHub.

## Create a new directory and add a file

You can run this series of commands in the terminal:

1. Change into your `code` directory: `cd ~/code`
   * If your development directory is named something other than `~/code`, that's fine — `cd` into whatever yours is called.
2. Create a new directory named `my_new_directory`: `mkdir my_new_directory`
3. Change into the newly-created directory: `cd my_new_directory`
4. Create a new file named `README.md`: `touch README.md`
5. Add some text to the new file: `echo "This is my readme file" > README.md`

## Creating a remote repository on GitHub

1. While logged into GitHub, click the➕ in the menubar and select `New repository`. Alternatively, just navigate to [github.com/new](https://github.com/new).
2. Enter a name for your repository in the `Repository name` field. You can name it whatever you'd like; be creative! The default options are fine as-is — don't initialize the new repository with a README or add a `.gitignore` or license. Click the green `Create repository` button.
3. After you create the repo, you should see a "Quick setup" page. Click the "Copy to clipboard" symbol on the right-hand side of the screen ([![GitHub clipboard](https://camo.githubusercontent.com/591a8d1ebe78ac2348d39734a5d923cbe4c7a8d3/687474703a2f2f692e696d6775722e636f6d2f4d386968466f4a2e706e67)](https://camo.githubusercontent.com/591a8d1ebe78ac2348d39734a5d923cbe4c7a8d3/687474703a2f2f692e696d6775722e636f6d2f4d386968466f4a2e706e67)) to copy the clone URL. (We'll use this in the next section.)

[![github repo quick setup](https://camo.githubusercontent.com/4b63aa62a7b0e4e9f2273bd6fc99e91061987de4/68747470733a2f2f637572726963756c756d2d636f6e74656e742e73332e616d617a6f6e6177732e636f6d2f7765622d646576656c6f706d656e742f656e6f7567682d6769742d666f722d6c6561726e2d636f2f6769746875625f717569636b5f73657475702e706e67)](https://camo.githubusercontent.com/4b63aa62a7b0e4e9f2273bd6fc99e91061987de4/68747470733a2f2f637572726963756c756d2d636f6e74656e742e73332e616d617a6f6e6177732e636f6d2f7765622d646576656c6f706d656e742f656e6f7567682d6769742d666f722d6c6561726e2d636f2f6769746875625f717569636b5f73657475702e706e67)

## Connecting your remote repo to a local repo

Run these commands in the terminal:

1. `git init`.
2. `git add .` and `git commit -m "initialize git"`. Add and commit the new file we created in the `my_new_directory` directory.
3. `git remote add origin your-remote-repository-URL`. This sets the remote, so you can push and pull code.

## Push code to GitHub

1. Let's add some new content to our `README.md` file. Open the file, and add whatever text you'd like.
2. Now look at the remote repo on GitHub. Notice that the new text in your README is not there. Let's fix this by pushing our code up to GitHub.
3. If we `git push` right away, we still won't get the changes because we have not tracked and committed the changes. Let's do that: `git add .` and `git commit -m "add additional content to README"`.
4. Now we can `git push -u origin master`. We only need to apply the `-u` flag (short for `--set-upstream`) the first time we use `git push`. It tells the current local branch to track itself against the `master` branch of `origin`, the remote repo we're pushing to. After you've set the upstream link with `-u`, you can use `git push` and `git pull` without specifying any arguments (such as a target branch or repo).
5. Confirm that your changes are now visible on GitHub, and you're done!

## Pull code from GitHub

This command takes any new changes to the remote repository and "pulls" them down to your local code. Try running it in your terminal now. In our case, there's nothing to pull, so you should see a message that says `Already up-to-date.`

Sometimes the code on your remote gets ahead of your local code. This happens often when collaborating with others, but it can also happen when you edit code directly on [GitHub.com](https://github.com/). Let's give that a try.

## Editing directly on GitHub

Say you liked your README, but you noticed a minor typo. Let's fix it directly on GitHub.

1. Navigate to your remote repository on [GitHub.com](https://github.com/), e.g., <https://github.com/username-here/repository-name-here>.
2. Click on your README file.
3. At the top of the file, you'll notice a pencil icon ([![GitHub pencil](https://camo.githubusercontent.com/72374ec9bd0d3ed3eced8850e188f41b73ebfd94/687474703a2f2f692e696d6775722e636f6d2f4a3348694c684f2e706e67)](https://camo.githubusercontent.com/72374ec9bd0d3ed3eced8850e188f41b73ebfd94/687474703a2f2f692e696d6775722e636f6d2f4a3348694c684f2e706e67)). Clicking this will allow us to edit the file.
4. Make some changes to your README.
5. Commit them by clicking the "Commit changes" button at the bottom of the page.

Perfect! But now the code on our machine (in our local repo) is out of sync with the remote repo. To remedy this, we must `pull` down the code to our local repo. To do so, run:

View [Git Remotes + GitHub Code-Along](https://learn.co/lessons/git-remote-code-along) on Learn.co and start learning to code for free.
