2

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?

Madison320
  • 247
  • 3
  • 11
  • Does this answer your question? [Github "Updates were rejected because the remote contains work that you do not have locally."](https://stackoverflow.com/questions/18328800/github-updates-were-rejected-because-the-remote-contains-work-that-you-do-not-h). BTW, as you entered your question's title, SO would have responded with quite a few suggestions thus saving you the need to post –  May 09 '21 at 00:24
  • I just tried renaming the project on github and I'm still getting the error. How can the remote contain work that does not exist? – Madison320 May 09 '21 at 00:24
  • This error doesn't just happen. You've already mentioned that you have been performing operations GitHub-side, are you sure that you didn't say edit a README via the GitHub GUI? That is a common practice that will lead to the above error. This and more is explained in the above link. Renaming the project is kinda `extreme` and doesn't solve the underlying problem. I'm curious why you didn't just perform a `git pull` (even if you are the only author) out of curiosity –  May 09 '21 at 00:26
  • No I've never touched the readme. I've committed hundreds of project and this has never happened before. – Madison320 May 09 '21 at 00:28
  • The only operation I performed on the github side was to rename the project. I thought by doing that I could push my project to github since it is now a project that in theory does not exist on github. Is there some sort of underlying identifier that I'm not aware of besides the project name? – Madison320 May 09 '21 at 00:31
  • I didn't do a git pull for 2 reasons. One is that I've never done one and this is a bad time for me to be testing out new stuff. Two is that I'm 100% sure that the code on my pc is correct and I don't want to be junking it up with something on github. – Madison320 May 09 '21 at 00:34
  • Can I do a pull and just tell it to keep all of my existing code? – Madison320 May 09 '21 at 00:40
  • 1
    _"Can I do a pull and just tell it to keep all of my existing code?"_ - YES! :) If there is an issue with the `pull` you can [undo it](https://stackoverflow.com/questions/1223354/undo-git-pull-how-to-bring-repos-to-old-state). [Or this](https://stackoverflow.com/questions/5815448/how-to-undo-a-git-pull/5815626). Probably easier in the UI. VS 2019 Preview might have something different. Don't forget you can always perform a `git clone` again on your PC in a **different folder** and then compare the directory contents in order to test GitHub –  May 09 '21 at 00:41
  • Tried to do a pull. Told it to keep the local version. It succeeded but my project is not on github. I swear github is harder than writing code. Probably harder than nuclear fusion. – Madison320 May 09 '21 at 00:48
  • _"I swear github is harder than writing code"_ - haha you sir just said a mouthfull. ;) –  May 09 '21 at 00:59
  • One more thing before I go, when you are about to start a **new project** and you plan on storing your code in a hosted repo like _GitHub_ or _DevOps_, it's soooo much easier to create the repo **first in GitHub/DevOps** then `clone` to your computer. In doing so you get an exact copy of the remote; the Git remote is auto configured in your local Git config file and subsequent `push`s are pain-free. Doing the reverse can lead to the problems during `push` and/or `merge` because if you are not careful, Git will essentially think your local repo is completely different –  May 09 '21 at 06:54

2 Answers2

0

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.

Shidouuu
  • 368
  • 4
  • 14
  • 1
    I didn't want to do either of those because the local version I'm working on IS the correct version. It makes no sense to me to try to merge or clone anything on github with my known good project. – Madison320 May 09 '21 at 01:09
  • 1
    As far as I can tell github does not use the project name to identify the project. There must be an underlying identifier. So assume my inital project was named Proj. I finally was able to fix the problem by renaming the project on github to Proj-2021-05-08 and then creating a new project named Proj and copying my code into the new project. Github treated this project as a new project. – Madison320 May 09 '21 at 01:16
  • @Madison320 it's actually nothing to do with GitHub but rather Git. I don't know if a _repo_ has an ID but each commit will. Commits in any repo will have a unique ID ([SHA-1 hash of your commit](https://stackoverflow.com/a/29107504/585968)). If you try to `push` to a remote that is ahead of yours you will need to `pull`. If however the ID of your first local commit has an ID different to the remote's first commit, that's a good sign you did not `git clone` first and any pull/merge will fail. What you have now is an orphaned repo –  May 09 '21 at 07:49
  • @Madison320 Clearly it is not the correct version because git says so. Changing certain things on github such as the README will create and push a new git commit, changing the history. – Shidouuu May 09 '21 at 22:42
  • @MickyD No, it does have to do a lot with GitHub actually. Assuming that they are the only one working on the GitHub repo, then the only reason that explains why their local repo is not up to date with the remote is changing something on GitHub. – Shidouuu May 09 '21 at 22:46
  • @Shidouuu we are in agreement there but I was actually talking about the OP's concern whether there is some secret ID that GitHub uses to identity projects, hence the _"it's actually nothing to do with GitHub but rather Git"_ –  May 10 '21 at 01:45
0

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.

  • _"you need to Fetch and Pull"_ - _["...`git pull` is shorthand for `git fetch` followed by `git merge` ...](https://git-scm.com/docs/git-pull)_ so there is no need to perform an explicit `fetch` (https://git-scm.com/docs/git-pull) –  May 09 '21 at 06:44
  • @MickyD In theory... but in the real world sometimes I need to fetch & pull, or pull & fetch. I admit that I am new to git and that I have trouble understanding the difference between pull & fetch, especially with what I just said, especially since sometimes it makes me fetch/pull and/or pull/fetch again to get correct result, I say sometimes. But it must either come from my ignorance, TortoiseGit or whatever. Answering to OP with what works for me, I re-read the doc and it tells that pull is a special fetch+merge, so if I understand, there is a little difference, but I still don't understand. –  May 09 '21 at 06:51
  • If your intent is to merge in recent remote changes into your local branch then just do `git pull`. `git fetch` as the name implies just downloads but as far as your working copy is concerned, nothing will appear to have changed. However, an explicit `git fetch` is useful if you want to refresh the list of "remote branches". e.g. if you perform say a pull-request and your changes merged and feature branch deleted on the server, the branch still shows as a ghost in say Visual Studio. Performing a `fetch` with the `prune` option will make that branch go away. VS has a GUI option :) –  May 09 '21 at 07:25
  • It doesn't hurt to do _fetch-pull_, _pull-fetch_ or a simple _pull_. The only difference is an additional tiny traffic to the remote and Git's smart enough not to double-up on downloads if it doesn't need to. Modern UIs make all this easier too –  May 09 '21 at 07:36