- Create a local repository
- Create a remote repository
- Connect the local repository and the remote repository with
git remote
- Push code up to the remote repo with
git push
You've seen how valuable remotes are for getting software. Now we can take a
look at the other side of the transaction: how we synchronize our local
repository to a remote repository using git remote
and git push
.
Once your code is on a remote, it's backed up — which is always a good
thing. Also, once you push to a remote, you can choose whether to let others
fork
or clone
and benefit from it.
You're already quite familiar with using the git push
command to push your
work on labs up to GitHub so you can submit it using CodeGrade. But what happens
when you need to push up work that doesn't already exist on GitHub?
In this lesson, we'll learn how we can set up remote repositories on GitHub for any projects we work on. Specifically, we'll learn how to get a new repo set up on our local drive, create a remote repo for it on GitHub, connect the local and remote repositories, and push our code up to the remote repo on GitHub.
Let's review the process for creating a local repository:
-
First, we want to create a folder for our repository, which we'll call
my_new_directory
. In the terminal, navigate to the~/Development/code/se-prep
directory (or wherever you are storing your code for the course) and typemkdir my_new_directory
. -
To navigate into this new folder, type
cd my_new_directory
. Your terminal should displaymy_new_directory
, indicating that you are now inside of the new folder. Open the directory in VS Code by typingcode .
. -
Next, we need to create a new file named
README.md
. You can do this in the terminal, by typingtouch README.md
, or in VS Code, by choosingFile -> New File
. -
We can directly type in content for our README file in VS Code, but we can also use our terminal skills to add content. So, in the terminal, type
echo "This is my readme file" >> README.md
. If you've got the README file open in VS Code, the new text will appear! -
Now that we've got some basic content, we can initialize our local repository. In your terminal, type
git init
. Your terminal should show that an 'empty Git repository' has been initialized. -
Type
git add README.md
(orgit add .
) to stage the newREADME.md
file so it will be tracked bygit
. -
Now, type
git commit -m "Initialize git"
. This will create the first commit for this local repository.
There are a few steps to follow to create a remote repository on GitHub.
-
Go to: github.com/new, while logged in to GitHub.
-
Enter a name for your repository in the "Repository name" field. You can name it whatever you'd like; be creative! You can leave the remaining options as they are — the default options are fine. Click the green 'Create repository' button.
-
After you create the remote repository, you should see a "Quick setup" box at the top of the page. Make sure the 'SSH' option is selected, then click the copy button next to the repo URL to copy the URL. (We'll use this in the next section.)
Behind the scenes, GitHub has essentially git init
'd a blank directory.
We learned in the previous lesson that the git remote
command will list the
remotes available to our Git repo. If you run that command now, ...nothing
happens. This is because we haven't set our remote repo as the remote for
our local repo yet. Doing so is a simple matter of assigning the URL we copied
above to a remote name.
We also learned that Git uses the remote name origin
by default when we clone
an existing repo from GitHub. We verified that by running git remote -v
, which
showed that the remote name origin
was pointing to the repo on GitHub that we
cloned. We'll stick to that convention here.
You should still have your remote Git info copied from GitHub. To set the
remote, type git remote add origin
followed by a space, then paste in the URL.
It will look something like this:
$ git remote add origin git@github.com:your-github-username/my-new-repo.git
The command above creates a remote called origin
and points it to your remote
repo on GitHub.
Let's use git remote -v
(recall that the -v
is for "verbose") to verify that
we successfully set our remote:
$ git remote -v
View existing remotes
origin git@github.com:your-github-username/my-new-repo.git (fetch)
origin git@github.com:your-github-username/my-new-repo.git (push)
We did it! We're now set up to push our code.
Now that we have added a remote repo, we need to send our latest work to it
using git push
. This command will push all the local, committed work to the
remote repository.
The git push
command takes two arguments. The first is the name of the remote
repo. Remember, origin
is just an alias or "short name" that refers to the
repository's URL. You don't actually have to enter the repository URL. Instead,
you can just use origin
. The second argument is the name of the remote
branch you want to send code to. We're going to push to our remote
repository's default branch.
If you configured Git using the instructions given earlier in Software
Engineering Prep, your default branch's name will be main
. You can verify this
by running:
$ git branch --show-current
Then, to push your code up to GitHub, run this command:
$ git push -u origin main
The first time you push code up to a newly-added remote repository, using the
-u
flag will tell Git to "save" the remote repository as the default push
destination for your current branch. What this means is that, for every
subsequent push from the main
branch, you will only need to run git push
.
Being able to add Git remotes allows you to back up your local repository to a remote server. To review, the process is:
- create a local repo and run
git init
to start tracking it - create a remote repo on GitHub
- run
git remote add origin your-remote-repository-URL
to tie your local repo to the remote repo on GitHub - use
git add
andgit commit
to save your changes - use
git push
to push the changes up to the remote repo
With these few steps, you'll be able to get your project up to GitHub in minutes!