Troubleshooting: Updates Rejected When Pushing to GitHub (2023)

Introduction

This is video #11 in the Data School series, "Introduction to Git and GitHub." Relevant links and the full transcript are below. Playlist: www.youtube.com/playlist

== LINKS ==

Git quick reference for beginners: www.dataschool.io/git-quick-reference-for-beginners/

== TRANSCRIPT ==

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.

Content

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.

FAQs

Why is my push getting rejected? ›

This usually happens when you work on different local branches and push local content to different remote branches. To resolve this error, it is needed to work on the same local and remote branches. This article demonstrated the procedure to fix the Git push “[remote rejected] main” error.

How do you fix Updates were rejected because the tip of your current branch is behind its remote counterpart? ›

Un-stash and re-commit your changes

followed by git commit to add your local changes to a branch that the remote won't reject on push. Once you've added your changes, git push shouldn't get rejected.

Why is my GitHub push not working? ›

This error mainly occurs when you attempt to push your local changes to GitHub while the local repository (repo) has not yet been updated with any changes made in the remote repo. So Git is trying to tell you to update the local repo with the current changes in the remote before pushing your own changes.

How do I push updates to GitHub? ›

To push your local changes to the remote repository, in the repository bar, click Push origin. If there are commits on the remote branch that you don't have on your local branch, GitHub Desktop prompts you to fetch new commits from the remote. In the "New Commits on Remote" window, click Fetch.

What to do when push is rejected? ›

If you get a failed to push some refs to error, the main thing to do is git pull to bring your local repo up to date with the remote. Avoid employing the --force flag when using git pull and prevent other developers' accidental overwrites of committed features.

How do you solve rejected non fast forward? ›

20 Answers. It looks, that someone pushed new commits between your last git fetch and git push . In this case you need to repeat your steps and rebase my_feature_branch one more time. After the git fetch I recommend to examine situation with gitk --all .

What does this mean updates were rejected because the tip of your current branch is behind? ›

hint: See the 'Note about fast-forwards' in 'git push --help' for details.) This error message indicates that you are trying to push changes to a Git branch, but the branch has been updated by someone else in the meantime. Git is preventing you from overwriting the changes that someone else has made.

How do I force a git branch to update? ›

Use git push -f to force update the remote branch, overwriting it using the local branch's changes. This operation is necessary anytime your local and remote repository diverge.

How do I push updates to a branch? ›

Push a new Git branch to a remote repo
  1. Clone the remote Git repo locally.
  2. Create a new branch with the branch, switch or checkout commands.
  3. Perform a git push with the –set-upstream option to set the remote repo for the new branch.
  4. Continue to perform Git commits locally on the new branch.
Oct 3, 2021

What is the limit to push to GitHub? ›

GitHub limits the size of files allowed in repositories. If you attempt to add or update a file that is larger than 50 MB, you will receive a warning from Git. The changes will still successfully push to your repository, but you can consider removing the commit to minimize performance impact.

How do I force push a remote to GitHub? ›

To force a git push properly, firstly, navigate to the Git local repository, and clone the remote repository to the local repository. Fetch the updated remote repository data and run the “$ git push –force origin <branch-name>” command to push the local branch forcefully to the remote repository.

How do I reset my GitHub push? ›

For users of GitHub, this worked for me:
  1. In any branch protection rules where you wish to make the change, make sure Allow force pushes is enabled.
  2. git reset --hard <full_hash_of_commit_to_reset_to>
  3. git push --force.

Which git command is used to update the changes on GitHub? ›

git pull is the most common way to update your repository. However, you may want to use git fetch instead. One reason to do this may be that you expect conflicts. Conflicts can occur in this way if you have new local commits, and new commits on the remote.

How to push changes to remote repository git? ›

To push the commit from the local repo to your remote repositories, run git push -u remote-name branch-name where remote-name is the nickname the local repo uses for the remote repositories and branch-name is the name of the branch to push to the repository.

How do you enable updates in GitHub extension? ›

Go to Tools menu > Extensions & Update > at Updates section, check the Visual Studio Market place, you should find a new update for GitHub Extension.

Why i can't push changes to Bitbucket? ›

This happens when there's a new commit in the branch you are working on the remote repository that you don't have the copy locally. So git rejected your push because of this. The workaround is to do a git pull to ensure your local repository copy is updated with the latest changes.

How do you push to a branch? ›

To push the branch or you can say to push the changes in the branch to the Github repo you have to run this command “git push origin <the branch name>” in our case the branch name is “main”. After pushing the changes the repo will look like and this is how you can push a branch to a remotely hosted GitHub repository.

What is rejected non fast forward? ›

If someone else has also committed changes to that remote repository, we won't be able to push our changes there. We will receive a “rejected – non-fast-forward” error. This is because we have two different streams of changes that we are both trying to modify to the master branch in the same remote repository.

What is remote error denying non fast forward refs? ›

Git push rejected non-fast-forward means, this error is faced when git cannot commit your changes to the remote repository. This may happen because your commit was lost or if someone else is trying to push to the same branch as you. This is the error you face.

Top Articles
Latest Posts
Article information

Author: Lidia Grady

Last Updated: 12/12/2023

Views: 5235

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.