#Git Remotes + Github
##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 repository
- Use git remote
- Use git push
- Use git pull
##Why this is useful Github does nothing special in the git universe. It's just another git repository in the cloud. If you don't want to work with anyone else, you don't need remotes. However, this is rarely the case, and we want to work with others! So, we have to talk about remotes.
##Creating a remote repository on Github
- From your Github profile, click the "Create New Repository" button.
- After you create the repo, click the "Copy to clipboard" symbol on the right-hand side of the screen to copy the url (we'll use this in the next section)
##Connecting your remote repo to a local repo
git init
git add .
+git commit -m "initialize git"
. Add and commit the new file created by the init command in the step 1.git remote add origin your-remote-repository-URL
. This sets the remote, so you can push and pull code.
###Note on Origin
What is origin
? Origin is just the name of the repo, but it could be anything. Origin is simply a convention for the default repo name. Let's take a closer look by changing the name of the repo:
git remote -v
# View existing remotes
# origin https://github.com/OWNER/REPOSITORY.git (fetch)
# origin https://github.com/OWNER/REPOSITORY.git (push)
git remote rename origin destination
# Change remote name from 'origin' to 'destination'
git remote -v
# Verify remote's new name
# destination https://github.com/OWNER/REPOSITORY.git (fetch)
# destination https://github.com/OWNER/REPOSITORY.git (push)
For consistency's sake, let's go ahead and rename destination
back to origin
:
git remote rename destination origin
##git push + git pull Now that we added a remote repo, there are two actions. Send our latest work up and retrieve the latest work from the remote.
###git push We use this command when we want to send some code from the local repository to the associated remote repository.
git push
takes two arguments. The first is the name of the remote repo. Remember, origin
is just an alias that refers to the repository name. You don't actually have to enter the repository name. Instead, you can just use origin
. The second is the name of the remote branch you want to send code to. In the example below, we're pushing to the master branch on our remote location of our repository, referred to as origin
. To find all the branch names, run git branch -r
.
git push origin master
That is the explicit way to push. You can also implicitly push your code by running:
git push
This will push your code up the remote repo/branch you're tracking.
For more details, check out the Github guides on pushing
###git pull As we collaborate with other people, inevitably they will push some code. The only problem is that now our code on our machine (our local repo) is out of sync with the remote repo. To remedy this, we must pull down the code to our local repo. No surprises here. To do this we run:
git pull
Again, we can also do this explicitly if need be by adding the remote name and branch as arguments: git pull origin master
.
For more details, check out the Github guides on pulling
View Git Remotes + Github on Learn.co and start learning to code for free.
View Git Remotes and Github on Learn.co and start learning to code for free.