Git problem

Hi.

I am trying to get out a confused git situation. I can't really remember how I got there, but I want to get out and manage to add, commit and push like before.
1
2
3
4
5
6
7
8
$ git push origin main
To https://github.com/XXXXX/YYYYY.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/XXXXX/YYYYY.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.


I can't push, or pull
I've done also: git reset --hard HEAD which didn't help

I have main as HEAD, and no branches

Any suggestions?
Last edited on
So what happens if you do the "hints" of doing a "git pull" ?
A meta-search for git push/pull errors and possible solutions:

https://duckduckgo.com/?t=ffab&q=git+can%27t+push+or+pull+how+to+fix&ia=web

IMO git is not very user friendly, the remote and local copies can get out of sync all too easily.

Personally when pushing my repos I use the Windows version of git and the Git GUI, that has the feature to over-ride any difference issues. When pushing.

I have never had any pull issues since I am the sole maintainer for my repos on github using git bash or a regular Windows console.

https://gitforwindows.org/
So this issue occurs when you have two branches that are not mergable safely. e.g.:

   /---C (origin/main)
A------B (main)


Trying to push your main to origin/main will cause this failure because you are missing information on how the commit C fits into your main branch.

A common way this occurs is by not keeping your local branch up to date, missing changes.

Generally to try to fix this I would first look at my two branches via git log to figure out what is different. If the only differences are missing commits, I would then do a git rebase origin/main, which takes any differences in your branch and moves them to the end of the branch you specify, so you'd hopefully end up with:

A-----C (origin/main)------B (main)


Now, if C added changes that break B like deleting a file, you would end up a conflict, which you would have to resolve (which is a different topic).
You always "have" a branch, usually the default one (typically called "main" or "master").

Generally, you have to do a git pull before you can do the git push, because the "remote" server doesn't allow you to push your changes (commits) when they are based on an outdated version of the "remote" branch (i.e. when there have been new commits in the "remote" branch in the meantime and you didn't incorporate those new commits into your "local" branch yet). So, you need to pull in the missing commits from the "remote" branch and merge them with your "local" branch first! Once you are up-to-date again, you can push!
Last edited on
git pull origin main gives this:


$ git pull origin main
error: Pulling is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.


git merge gives:

$ git merge
error: Merging is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict


I have one main branch, and I remember that yesterday I clone the remote to another local directory, and delete the folder after couple of hours (I wanted to change remote directory name)
At this point I don't care much about losing changes, I just want to be able to push again to the repo.
How can i analyze what happened and how to solve this?
Last edited on
git merge only works automatically, if there are no conflicts. If there are conflicts, you will have to merge by hand, by manually editing the files and resolving the conflicts. Probably a git status will give you more details about the files that are in "conflicting" state...

Anyhow, what you can always do is the following, but use with care:
1
2
git fetch --all
git reset --hard origin/main

The first command will fetch the latest "remote" branch, but (unlike pull) it does not attempt to merge it with the "local" one. Then, the second command will reset your "local" branch to the specified "remote" branch. This makes your "local" branch identical to the "remote" one again, effectively discarding any local commits (or uncommitted changes) that were not yet pushed to the remote server...
Last edited on
Thanks for the replays
I kind of solved the issue
Topic archived. No new replies allowed.