1

In my GitBash, I edited the commits until there's only four left.

Image 1

That is because I want to like update what's on our website and get rid of those other two about me files.

Image 2

I tried to directly push it, but it it says this.

Image 3

Is there a quick way to do it?

mousetail
  • 7,009
  • 4
  • 25
  • 45
naomi
  • 13
  • 3
  • Try running `git pull` – mousetail Aug 09 '23 at 08:27
  • 2
    @mousetail No. Why would OP pull the commits they explicitly want to get rid of? – Romain Valeri Aug 09 '23 at 08:30
  • Then `git fetch`, then git will let you force push – mousetail Aug 09 '23 at 08:31
  • 1
    The push is rejected because the new history doesn't match the remote one. If local history has to replace it, the push command just lacks a `--force-with-lease` option and it will succeed. (**Be sure to double-check your new history first, though, as this cannot be undone.**) – Romain Valeri Aug 09 '23 at 08:33
  • @mousetail it just says that "Automatic merge failed; fix conflicts and then commit the result.". I can't seem to find a step by step in the internet that is related to mine. They only work on their situation. – naomi Aug 09 '23 at 08:35
  • Run `git merge --abort` – mousetail Aug 09 '23 at 08:36
  • @RomainValeri Hello. What are the necessary steps should I do? – naomi Aug 09 '23 at 08:38
  • Okay thank you, I run it and in the git status doesn't show the error anymore. @mousetail – naomi Aug 09 '23 at 08:39
  • @naomi Vincent's answer details what I suggested. Go for it, it should be fine. – Romain Valeri Aug 09 '23 at 08:39
  • https://stackoverflow.com/search?q=%5Bgit%5D+Updates+were+rejected+because+the+tip+of+your+current+branch+is+behind+its+remote+counterpart+ – phd Aug 09 '23 at 10:24

1 Answers1

1

Since you effectively rewrote history, git will not allow you to simply push the new truth. You will need what is called a "force push", described here: https://git-scm.com/docs/git-push#Documentation/git-push.txt---no-force-with-lease.

Please note that git push --force-with-lease origin master means all checks are skipped, and you may therefor lose commits. In this case though, that seems to be what you are intending to do (removing a few commits) so it's OK.

Please make double and triple sure that you're pushing exactly what you want to push. If you don't have a copy of the repository elsewhere, then this might be a tricky thing to revert.

Also, do not use --force-with-lease with every push, only when you really need to.

Vincent
  • 1,459
  • 15
  • 37
  • 1
    We do not know if OP is working alone on the repo or not, but the cautious `--force-with-lease` variant of `--force` can't do harm. Good answer here. – Romain Valeri Aug 09 '23 at 08:42
  • Yes, this worked. I first made sure that what's on my local repository commits are the ones I really wanted to overwrite. I then did git push --force origin master. It overwrites which is what I intended. 10/10 thank you @Vincent! – naomi Aug 09 '23 at 08:58