Problem using Git basic commands - push

Hello everyone, my name is Billy and am a student studying BSc. business intelligence. I have just started an online course about Front-End Web Development. First week course is about setting up Git and using basic Git commands. As you see from the screenshot, I commited four changes and would like to push these changes to my remote repository. However, this command is rejected. The error message hints to use “git pull” and I tried, terminal gave me another error:

Does anybody knows how I can solve this problem, so that the changes can be pushed to the remote repository? Thanks in advance!

1 Like

The “refusing to merge unrelated histories” message means that your local and remote repositories have absolutely no commits in common. That can mean a few things:

  1. You have the wrong remote repository (seems unlikely if it’s already configured as origin).
  2. You edited the commit history back to the first commit (git rebase, git commit --amend, or maybe git filter-branch), so even if the content is the same the commits are all different.
  3. It could also be that the remote repository was initialized with a commit (e.g. a default readme), but you don’t actually care about that.

I’ll assume the second case here, but the third one can be handled the same way.

  • Do git fetch origin to have the current state of the remote repository available to compare.
  • Check the commit logs of both the local and remote branches (git log master, git log origin/master).
  • You can also use git diff origin/master..master to see what your local branch changes relative to the remote one.

Decide what you want to keep. That can be just one version (I’d guess the local one). If you also want to keep a few commits from remote you can use git cherry-pick to add them to the local branch. You could also create a new branch based on whichever branch most fits what you want to keep and then cherry pick, to have a sort of sandbox where you won’t destroy anything.

Once you have a branch that you are absolutely sure contains what you want to keep, you can force-push (git push --force) it to the remote repository. You have to be absolutely sure, because this will DESTROY EVERYTHING on the remote branch. :warning:

Theoretically you can also tell git to merge despite the unconnected histories, but that is almost always the wrong choice (which is why git refuses do to it by default).

1 Like

Thank you very much for your feedback. I checked the commit logs of my local and my remote branches. I would like to keep my local one, that also means, I would like to push the local one to the remote repository. Do I need to tell the terminal which version (in my case the local one) I will keep? Because when I did git push --force right aright, it shows the following error:

1 Like

Each branch in a git repository can have a remote “upstream” branch (also called “tracking” branch). If you do a git pull or git push (force or not) without further parameters it will pull from or push to there. The error message says that your local master branch doesn’t have a configured upstream branch.

If you want to set origin/master as the upstream branch for your local master, you can just use the command suggested by the error message, and add --force for the force-push:

git push --force --set-upstream origin master

If you don’t want to set the upstream branch leave out the --set-upstream option.

1 Like

Thanks for the explanation! I tried the git push --force --set-upstream origin master command and problem was solved immediately. Now all commits in my local repository was synchronized to my remote repository. Honestly I don’t completely understand how it worked (and did not work)… but still thank you so much!