Aquarius Power Aquarius Power - 25 days ago 6x
Git Question

Git GUI + GitHub, amend last commit is problematic, how to quickly overcome it?

The problem is on

git gui
VERSUS github (when I try to push an ammended commit created with
git gui

Many times, instead of promptly pushing, I just code more.

When I go to commit and push using
git gui
, some times I select "amend last commit" just to reuse the commit message at
git gui

When I remember that this interaction (
git gui
vs GitHub) will mess things up, I copy the comment at "ammend last commit" option, and select "new commit" back again, and paste the comment on it.

But right now I forgot and I am on such mess again... GitHub will not allow me to push the ammended commit, it wants me to merge things unnecessarily (because the ammend commit should work, and simply does not).
So now, my source files are messed (requiring merge, with that merge tags all over preventing compilation), and there are like 20 files messed...

I have not a previous backup.

So I could painfully get my previous commit in some way (using some git command line)... but I guess there is some straight forward way to overwrite the files modified to allow manual merging with my latest proper changes.

I am the only one committing files, so this is a
git gui
versus GitHub bug or limitation for sure.


1) do commit, commit, commit...

2) do ammend last commit (it may or not cause problem)

3) do
git pull
(it may create merge tags messing the files, uncompilable, requiring needles conflict resolve)

From @VonC answer
git log --graph --oneline --decorate --all
, the fork seems very clear:

* d8f3505 (HEAD, master) improving savable helper
| * 772a6c0 (origin/master, origin/HEAD) improving savable helper
* 15630f3 improving savable helper


* fdc9ae1 (HEAD, master) improving savable helper
| * 772a6c0 (origin/master, origin/HEAD) improving savable helper
* 15630f3 improving savable helper

so, my latest changes are at d8f3505 or fdc9ae1 (I recovered backups to retry, thats why such number changed).

this command keeps the merge tags, therefore uncompilable:
git reset origin/master

this command
git reset --hard HEAD@{1}
restores the changes at 772a6c0, therefore I lose the latest ones (I still have the backup).

So, how to make the latest (d8f3505 or fdc9ae1 or another new one as soon I recover the backup again), be the one that will win/overwrite the HEAD?


git reset --hard 12eb50c
(the new id) let me recover the latest changes without merge tags, great!

but now, after
git rebase origin/master
(that messes the files again, but now I had time to backup the files, in a non test case situation...), I get this message from
git gui
enter image description here

after I accept it, the push from within
git gui
stopped working, but command line
git push
gives me this message:

! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to ''
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. If you did not intend to push that branch, you may want to
hint: specify branches to push or set the 'push.default' configuration variable
hint: to 'simple', 'current' or 'upstream' to push only the current branch.

unable to push even after
git reset --hard 15630f3
... if I could prune whatever is after it and just overwrite with my backuped sources...


By selecting amend the last commit, you have actually rewritten your HEAD instead of creating a new commit (which would have been pushed to GitHub without issue)

 --o--o (origin/master)
     -O (HEAD: your amended commit)

Since the history is now different, GitHub (or any remote Git repo) wants you to pull and merge first, then push.

In order to fix this, since the content in O is good for you, but the commit itself is not, you can:

  • reset HEAD to origin/master, but keeping your current working tree

    git reset origin/master
  • add and commit from there: this should add your work as a new commit.

From there, you can push your new commit without any issue (fast-forward)


Find out what is your last changes with:
git log --graph --oneline --decorate --all (it will be the top one, not the forked one)

Then do this ex.: reset --hard fdc9ae1 and backup your restored last sources changes.

Then git checkout master and git reset --hard HEAD@{1}

It will require git pull before git push, do it, the merge tags will appear again...

Replace with your backup, and now you can push properly again.