2

So I'm not sure if this was the cause of my problem, but I accidentally did:

git push -u origin master

instead of:

git push -u origin facebook

when I was on my facebook branch. It responded:

Branch master set up to track remote branch master from origin.

Now when I try to push:

To git@git.url.com:url.git
 ! [rejected]        facebook -> facebook (non-fast-forward)
error: failed to push some refs to 'git@git.url.com:url.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.

I can't pull either:

You asked me to pull without telling me which branch you
want to merge with, and 'branch.facebook.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:

    [branch "facebook"]
    remote = <nickname>
    merge = <remote-ref>

    [remote "<nickname>"]
    url = <url>
    fetch = <refspec>

See git-config(1) for details.
dtgee
  • 1,272
  • 2
  • 15
  • 30

3 Answers3

2

Well this is when you try to push and you have made changes in your file that conflicts with the lastest version in Git.

Try this:

git pull origin facebook

If there is an error:

git merge
git commit -a -m 'message'
git push origin facebook
Gidrek
  • 750
  • 9
  • 23
1

You can use this command

git pull origin master --allow-unrelated-histories
ireshika piyumalie
  • 2,226
  • 22
  • 22
0

You need to fix 2 issues

1.) Revert the incorrect "facebook" commits that you pushed to the "master" branch of the upstream repo.

The easiest way to do that might be to clone the repo from upstream once more into a new directory. Use e.g. gitk to see what are the wrong commits in the master branch, revert them and push them. Now your colleagues are happy because the wrong stuff in the repo are corrected.

Don't use push -f unless you and all your colleages understand fully what you are doing. You might lose data. Reverting might look uglier but it is safe even if you get it wrong on the first attempt.

2.) Fix your own repo, so that branches are no longer confused. The easiest would actually be to forget about the old repo and just continue with the new clone. However, if there are important stashes or other branches that have not been pushed to any upstream, then you need to repair the setup of your old repo. The easiest might be to edit .git/config directly. Take a clean repo that did not have such accidents as a sample if unsure what it should look like.

Personally I often prefer git fetch over git pull. I don't think git fetch can fail. Git pull does a fetch and a merge in one command, sometimes it is just better doing the one be one In the remote branches remotes/origin/branchname you have exact snapshots of what is in the remote repo at the time of the last fetch. They are read-only, so you can never screw them up. (They are called remote, but they are not stored remotely, that is often misunderstood) Your local branches are read/write. If one is screwed up you can even delete it, and check it out again. Git will create a new local branch from the remote branch. (If course you don't want to delete a branch if it contains important commits that have never been pushed)

Before doing tricky operations to your repo, which you don't fully understand, a backup might be a good idea, especially if you have something that has never been pushed.

Myself I only use git push (without any arguments), so such mix-up cannot happen. (Except the first time I push a new branch.) Additionally I only use git fetch instead of git pull so it always succeeds and I can check what others have done. (but then I need to merge the remote branches changed if I maintain a local one for them, there a similar mix-up could happen, but still it would only be local and could be deleted without disturbing others)

Addition: The accepted answer in Git Pull Doesn't Do A Git Fetch gives further details about push/fetch/pull and how (not) to use them.

Community
  • 1
  • 1
Uwe Geuder
  • 2,236
  • 1
  • 15
  • 21