Git Remotes and Github Codealong
Objectives
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 repositoryUse
git remote
Use
git push
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:
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.
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
Creating a remote repository on GitHub
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.
Connecting your remote repo to a local repo
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.
Push code to GitHub
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!
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. 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.
Navigate to your remote repository on GitHub.com, e.g., https://github.com/username-here/repository-name-here.
Click on your README 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:
View Git Remotes + GitHub Code-Along on Learn.co and start learning to code for free.
Last updated