GitHub error - Updates were rejected because the remote contains work that you do not have locally

Viewed 243

I'm using Visual Studio 2019 and the github updating is done thru the menu not a command line. I got an error last week when I tried to push my commits that said:

rejected Updates were rejected because the remote contains work that you do not have locally

To fix, I went to github.com and renamed the project. I was able to push several updates but now I'm getting the error again.

I am the only one using this project and the project is private so how is this possible?

2 Answers

There are multiple ways to solve this.

Option 1:

Update your local repo via git pull. This merges your local repo with the remote one, adding and deleting whatever is new on the remote. This requires that your git history be the same.

Option 2:

git clone the remote repo, making a new local repo that includes all of the new changes.

Note, changing certain things in github will cause it to create a new commit, and therefore changing the history.

This is possible because of the corruption of the repo and some bad commit/push while files have been modified online directly on GitHub (README.md for example): this causes "spaghetti".

From the root folder of the project, you need to Fetch and Pull.

Now you should be able to Push.

You can try again to Pull and Fetch in case of problems.

You can also try to backup the folder without the .git, Revert all, restore saved local files and commit/push again.

If it persists, you can try this which helps me to resolve such issue quickly:

  • Make a copy of the whole directory tree (with the .git in case of problem),
  • Delete the .git folder,
  • Recreate the local repo,
  • Configure it again to be connected to GitHub (restore the .git/config and check the HEAD file),
  • Fetch and Pull from the origin on the necessary branch (create it locally if needed),
  • Do not forget the master/main branch and all the needed branches,
  • Restore the local files from the backup folder without .git while being on the relevant branch,
  • Commit and push.

You can take a look at TortoireGit for easy, fast and advanced management with File Explorer integration having icons.

You can combine it with the TGit addon for Visual Studio.

There is also the Total Commander x64 plugin WDX GitCommander that can for example display the current branch and last commit in columns.

Related