I did search. tried to read Git pull after forced update, git refusing to fetch into current branch, How can I recover a lost commit in Git? but still cannot understand my situation.
Both local and remote on main
branch currently (by git status
). git log
on both confirmed local is ahead of remote by 2 commits. Why git fetch apparently tries to update HEAD?
git fetch
fatal: Refusing to fetch into current branch refs/heads/master of non-bare repository
fatal: The remote end hung up unexpectedly
git pull
did force update and my main
"lost" recent commits. From linked post I've read to do git reflog
and rest --hard
to last commit and I'm back to square one.
Also currently I face another issue (as initially seeing local is ahead I wanted to push) I've just posted as names match, but: "The upstream branch of your current branch does not match the name of your current branch", git pull writed "forced update"":
git push
outputs "The upstream branch of your current branch does not match the name of your current branch".
What happened to the repo? Why git pull
does force update by default w/out asking? Why git push
does not find remote branch?
Added:
I continue see some more interesting things, still w/out understanding why git does not see match of main
branches, I did git push origin HEAD:refs/remotes/origin/main
and saw push succeeded:
Total x (delta y)
To remote_url
ref_of_last_remote_commit...ref_of_last_local_commit HEAD -> origin/head
But: git reflog
on remote did not output those new commits!
CodePudding user response:
It’s hard to exactly understand what has gone wrong without knowing the context of your problem (i.e. the status of your git repository), but you said “ git push
outputs ‘The upstream branch of your current branch does not match the name of your current branch’.” and that could be a clue.
Assuming your local branch is named main
and your remote repository is named origin
(you can verify this by git remote -v
), the common setup is that your local branch main
should be tracking remote branch origin/main
. Check by typing git branch -vv
. If they don’t match, try using git push -u origin main
to make your remote branch name match with that of your local’s.
Or if origin/main
already exists, you can try rename your local branch to some unique name (git checkout -b <the_unique_name>
after you have committed everything locally) and do git push -u origin <the_unique_name>
.
CodePudding user response:
Sometime before noticing the issue I did git-branch --set-upstream-to=origin/main main
Now found out:
git version 2.25.1:
above sets main to track remote ref refs/remotes/origin/main
(--set-upstream-to=main
sets to track remote branch main from origin)
git version 2.34.1
above sets to track remote branch main from origin (--set-upstream-to=main
sets to track local branch main)
I think one needs to look attentively to output of the command to know the result.
tl;dr
Recently I did git-branch --set-upstream-to=origin/main main
, where as before I did git-branch --set-upstream origin main
. I used unfamiliar command w/out checking the effect. When torubleshooting I noticed in .git/config
entry for main
was different, extra remotes
.
Apparently it set main to track main on remote's remote (further down the chain). On fetch' fatal I guess was due to call to this 3rd repo from 2nd to fetch info. I wonder in what cases it works.
Now I see git-branch --set-upstream-to=main main
produces effect of setting main
branch to track origin's main.
Why git pull
does force update w/out confirmation I don't know, in my case it resulted in undesired result (latest commits lost from the branch).