In this video, we're going to talk about one of the most common problems users run into
when pushing to a new GitHub repository. First, I'll show you the problem and how to resolve
it. Then, I'll show you two ways to avoid running into that problem in the first place.
I'll be working through the examples quickly, since all of the commands in this video are
explained in previous videos. If you get lost, just watch the rest of the series and then
come back to this video.
Anyway, I'll start by demonstrating the problem. Let's create a repo on GitHub and initialize it with a README file.
Our plan is to create a local Git repo, add a file to the local repo, and then get that
repo synchronized with GitHub.
Let's create a subdirectory on our local machine with the same name as the GitHub repo, though
you can actually use any name you like. We then "change directory" to the subdirectory,
and initialize Git with "git init".
Now, we'll add a "remote" that allows you to easily reference your GitHub repo later.
You type "git remote add origin" and then the URL, which will either be an HTTPS URL,
or in my case, an SSH URL.
I created a simple text file before the video, which I'll now copy into my local repository.
Running "git status" confirms that Git has detected an untracked file. Now we'll stage
that file to be committed with "git add ." and we'll commit the file with "git commit -m" and a message.
Finally, we'll push the file using "git push origin master", which directs Git to push
the committed changes on the master branch to the origin that we defined earlier. Unfortunately,
there's an error which says that the updates were rejected because the remote contains
work that we don't have locally.
So why is this error occurring? It's a bit complicated, but the root of the problem is
that you initialized the GitHub repo with a README file. That actually added and committed
a file to your GitHub repo, which is not a file your local repo knows about. So when
you try to push your text file up to GitHub, Git is confused about why your local repo
doesn't have the README file in it. Git correctly guesses that you are probably trying to sync
these two repos, but it would rather have you fix the problem manually than guess how
to automatically fix the problem for you.
So to manually fix this problem, you simply need to update your local repo with what has
already occurred in your GitHub repo. To do this, we'll use the command "git pull origin
master", which fetches the README file from your GitHub repo and merges it into your local
repo. Now, you can once again try "git push origin master", and this time it will be successful.
We can refresh the repo on GitHub, and confirm that the text file is indeed there.
Now, let's talk about two ways that you could have avoided running into this problem in
the first place.
The first way would be to follow the exact same steps I did in this video, except don't
check the box to initialize the GitHub repo with a README. That way, your GitHub repo
will initially be empty, and you won't get an error the first time you try to push.
The second way, which I personally prefer, is to use a cloning approach. If you've watched
the rest of this video series, the cloning approach will look familiar, but I'll demonstrate
it here quickly. In this approach, you first create a GitHub repo and you must initialize
it with a README.
Then, you clone this repo to your local machine using "git clone" followed by the repo URL.
Cloning automatically downloads the GitHub repo and stores it in a subdirectory of your
working directory. It also automatically sets up your origin remote, so you don't have to
add the remote manually. And most importantly, your local repo and the GitHub repo are already
in sync.
Now, we can just copy the text file into our local repo, add it, commit it, and push. Refresh
GitHub, and you'll see that the text file is there.
To conclude, let me just quickly summarize this video. If you run into the problem where
your push is rejected, you may just need to "git pull origin master" before you "git push
origin master". If you want to avoid this problem in the first place, there are two
easy ways to avoid it. One way is to follow the first part of this video but don't check
the README box when creating your GitHub repo. The other way is to follow the cloning approach
from the end of the video, in which you do include a README on GitHub but you use "git
clone" instead of "git init" locally. Note that GitHub does not allow you to clone an
empty repo, which is why you must check the box to include a README if you're going to
clone.