I can't push it to Github after reset --hard

.git pull
.Conflict merge and save editor content
.git add <>
.git commit -m""
.git push

===== “all the above work, but below when I git reset --hard<>, and git push, then rejected by GitHub”=====

. git reset --hard<>
.“save the content in the editor”
. git push
“[rejected] master → master (non-fast-forward)
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.”

COULD SOME HELP? TKS.

Updates were rejected because the tip of your current branch is behind its remote counterpart.

This means there are commits on the remote branch that aren’t on the branch you want to push, by default git push won’t delete them. You have two options:

  1. If you want to keep the remote changes, merge them with your local ones, or rebase your local changes onto the remote ones. Then push.

  2. If you are 100% sure you want to discard the remote changes and lose them forever, you can add the --force option to git push to tell it you really want to delete remote changes. :warning:

Thanks for the reply. I am the kid bird for coding.

And one more question,
After “git reset --hard then git add, git commit, git push”, the remote repository reject!!!
Then git pull, the remote version cover the local version, and it doesn’t show conflict.
Why?

As described in the documentation, git pull will by default merge the remote branch into the current branch. If that works without conflicts all remote commits will be on your local branch, so the problem that pushing would delete remote commits is gone. :slightly_smiling_face:

If you don’t want git pull to merge, check the pull.ff and pull.rebase configuration options.

But after I “git reset, git add. git commit”, I could not “git push”, because the remote reject.

" ! [rejected] master → master (non-fast-forward)
error: failed to push some refs to ‘https://github.com/han186888/E_T_0.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."

So the remote is still the old version 2, not gone. Then I “git pull”, the version 2 of the remote cover my local reset version 1.

I want to push the “git reset” version 1 to remote, how can I do that?

Critical question here: Reset to what?

If you mean you want to push your local version no matter what and completely discard (lose!) anything in the GitHub repo that doesn’t fit, you can use git push --force as mentioned above. Just make sure you’re aware of the consequences. :warning:

Use git push --force, it can help me to solve my problem, thank you.

But I still don’t understand:
I “git reset version 1”, and the the remote version is 3.
After I git reset to version 1 locally,I push the version 1 to remote, the remote response rejected!
Here I still can understand, because they are conflict.

Then I git pull, the remote version 3 replace the local version 1, and the it didn’t show conflict. I mean because the remote and local version is conflict, but after I pull, the remote just replaced the local version.

It’s impossible to say for sure what’s going on without knowing your Git history, but I think there’s some confusion about the term “conflict” here.

git push by default refuses anything that cannot be fast-forwarded. That is, the commit that will become tip of the remote branch must be a descendant of the current tip. It will not merge on its own.

git pull will by default fast-forward if possible, and merge otherwise. And if the merge works without a merge conflict it completes just fine, including adding a merge commit.

Yeah, thank you.
Anyway I used the git reset --hard, and git push --force, then I got what I want.

I just want to replace remote version by the reseted local version , if I only used git push command, the remote reject it. And it hint to me to git pull from remote, and then the remote version replaced the local version on my computer. The result is not I wanted.

1 Like

Force-pushing is just the right thing if that’s what you want. :slightly_smiling_face:

I’m just careful about recommending it and always add big warnings because I’ve also seen the opposite here, people who force-pushed without understanding what exactly it did and then wondered where their remote commits went, which is not a fun situation.