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.