- Describe GitHub and its relationship with Git
- Create a remote repository on GitHub
- Use
git push
to connect your local repository of files to your remote repository - Use
git remote
- Use
git push
- Use
git pull
Let's go through an example to clarify remote repositories on GitHub.
You can run this series of commands in the terminal:
- 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.
- If your development directory is named something other than
- Create a new directory named
my_new_directory
:mkdir my_new_directory
- Change into the newly-created directory:
cd my_new_directory
- Create a new file named
README.md
:touch README.md
- Add some text to the new file:
echo "This is my readme file" > README.md
- While logged into GitHub, click the ➕ in the menubar and select
New repository
. Alternatively, just navigate to github.com/new. - 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 greenCreate repository
button. - 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 () to copy the clone URL. (We'll use this in the next section.)
Run these commands in the terminal:
git init
.git add .
andgit commit -m "initialize git"
. Add and commit the new file we created in themy_new_directory
directory.git remote add origin your-remote-repository-URL
. This sets the remote, so you can push and pull code.
- Let's add some new content to our
README.md
file. Open the file, and add whatever text you'd like. - 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.
- 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 .
andgit commit -m "add additional content to README"
. - Now we can
git push -u origin master
. We only need to apply the-u
flag (short for--set-upstream
) the first time we usegit push
. It tells the current local branch to track itself against themaster
branch oforigin
, the remote repo we're pushing to. After you've set the upstream link with-u
, you can usegit push
andgit pull
without specifying any arguments (such as a target branch or repo). - Confirm that your changes are now visible on GitHub, and you're done!
git pull
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. Let's give that a try.
Say you liked your README, but you noticed a minor typo. Let's fix it directly on GitHub.
- Navigate to your remote repository on GitHub.com, e.g., https://github.com/username-here/repository-name-here.
- Click on your README file.
- At the top of the file, you'll notice a pencil icon (). Clicking this will allow us to edit the file.
- Make some changes to your README.
- 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:
git pull
Please open a GitHub issue or pull-request. Provide a detailed description that explains the issue you have found or the change you are proposing. Then "@" mention your instructor on the issue or pull-request, and send them a link via Connect.
PHRG Git Remotes + GitHub Code-along