Git push isn't working

How can i solve this please help me

You need to use git pull and resolve the difference between your local changes and the remote changes before you can git push.

This is a new remote repo there is nothing without a readme file in that

There is still a commit in the remote branch initializing the repo that may not be in your local version. I’d still recommend pulling the remote. But if you don’t want to, you can git push -f and overwrite everything in the remote with your local branch.

when i try to pull thats when I am getting this error

The “fatal: refusing to merge unrelated histories” Git error

I’m not 100% sure, but it sounds like you initialized the repo in two different places via two different ways and now Git is confused. Like I said, if you are sure the local is what you want, you can force push.

1 Like

Thanks, Yeah your code solution just worked but why this occurs and can i avoid this? Do I always have to force push?

Finding yourself in a situation where you need to force push should be rare. Pulling and pushing should work fine now.

Is there any chance that it occurs for readme file ?
Because I initialized my remote repo with readme file while I already have one in my local repo

If you initialized the remote on GitHub with a commit that is not on your local branch, then yeah, you’ll see this sort of thing since the two versions of the repo won’t have any commits in common.

From Where I can learn these things ?

I’ve learned most of what I know about Git from trial and error. I’ve made messes of my repos and then Googled and asked questions to figure out what’s up.

And now I’m going through this mess, thanks by the way

No worries. Learning by doing is the only way it sticks for me, and I’m happy to answer questions : )

1 Like

Is it okay to crate a branch from a specific commit not from a branch ? I mean from detached head state.