Photo by Roman Synkevych on Unsplash
🪸Pushing to GitHub? Resolve "Non-Fast-Forward" Error with Git Pull 🪢
Today, when I was trying to push my files of Competitive Programming practice to GitHub, I faced an error...
fatal: refusing to merge unrelated histories
"This happens when you try to merge branches that don't share a recent common ancestor commit. In simpler terms, Git can't find a starting point where the histories of the two branches connect."
I started with a series of git commands and putting them here step by step :
git status
to check the statusgit add .
to stage all the filesgit status
to again check the current statusgit commit -m "Pushed all the files to github"
just a commit messagegit remote -v
to check if the remote repo connected to the local repo is correct.
All this codes run smoothly without any error. Until I done this one -
git push -u origin main
finally, pushing the files to github
now this is the point where I got the error and I became so angry as I used to face such issues a lot of time when I am doing something between VS code and github.
The error message was :
To https://github.com/HitenDewangan/Competitive-Programming.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/HitenDewangan/Competitive-Programming.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
so I tried something on my own like : step by step -
git branch
to check the current branchgit pull
to pull the changes from remote (if any)then I got this -
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> main
At this point, I can't just randomly give any command. So I asked Gemini for help.
Here is the prompt I given to Gemini and the response I got... Link
Out of the given suggestions, I tried this one -
git merge --allow-unrelated-histories origin/main
then,
git pull
Already up to date
git push
remote: Resolving deltas: 100% (75/75), done. To https://github.com/HitenDewangan/Competitive-Programming.git 572440f..4e5dadd main -> main
And in this way, I finally tackled this problem successfully...Maybe this can be small problem but a beginner or a learner like me find it so much happy that I'm able to tackle this problem on my own efforts.
Thanks for reading...
In case u want to visit my Github profile and check out the what all Competitive Programming practice files I have uploaded, I'm giving my Github repo link - here