Home > Enterprise >  Can't push or pull from Git in VSCode & VSCode pushing to the wrong branch by default
Can't push or pull from Git in VSCode & VSCode pushing to the wrong branch by default

Time:10-04

In VSCode I "Initialize Repository" > "Documents\Visual Studio Code\python\collatz_conjecture" > stage changes > "init" and commit > git remote add collatz_conjecture https://github.com/Frappyy/collatz_conjecture.git > Push. Pushing tells me "No configured push destination.". Okay, I cd into my local collatz conjecture folder where the python script and .git is in and do git push collatz_conjecture which gives me the following error:

To https://github.com/Frappyy/collatz_conjecture.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/Frappyy/collatz_conjecture.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Before this started happening, I ran git config --global init.defaultBranch main because after pushing, the script always ended up in the master branch instead of the main (default) branch it should be in.

Running git pull collatz_conjecture main says "fatal: refusing to merge unrelated histories". I have some basic understanding of how Git and Github work but all this is exceeding my knowledge. I've had some really annoying problems with Git on VSCode before so here I am.

CodePudding user response:

I can see you have a commit with a mostly-empty README.md file in collatz_conjecture/main: https://github.com/Frappyy/collatz_conjecture/commit/9bdf666e101a5be28bdb5cd9b43d47a43633e74e

It looks to me as if you created another history locally instead of cloning this repository, by initializing an empty local repository and creating a different initial commit.

This is why pulling fails too, because there is no common ancestor commit for the commit on GitHub that created the readme file and your local commit(s) that did other stuff.

If the commit on GitHub with the readme is not useful anyway, then force-pushing once should solve the issue:

git push -f

This will delete the history that had your README.md file and replace it with your local history.

If you want to keep your readme commit on GitHub you can instead force Git to merge the "unrelated" histories and then push:

git pull collatz_conjecture main --allow-unrelated-histories
git push collatz_conjecture

Also, consider renaming the collatz_conjecture remote to origin. This will simplify things because normally origin is the remote that points to your online repo.


To prevent these problems in the future, it's easiest to first create the repository on GitHub and then clone it, instead of creating an empty repo locally and setting the remote, because then it's ensured that both repos will add their commits to a common ancestor (the initial commit with the readme file from GitHub).

Alternatively you can also make sure that "Initialize repository with readme file" in GitHub is unchecked when creating the repo, so it will be entirely empty with no commits in it. In that case, initializing an empty repo locally, setting remote, creating an initial commit locally and pushing will work too.

  • Related