0

I have a feature branch called featureA and I am the only developer who is and will be touching this branch until it gets merged to master branch.

My git command history is:

  1. committed changes locally
  2. made more changes then pushed to remote by amending commit
  3. made one more change (on a single line) then pushed to remote by amending commit

BUT it seems NOT pushed to the remote repository.

git status outputs:

Your branch and origin/featureA have diverged, and have 1 and 1 different commits each, respectively. (use git pull to merge the remote branch into yours)

git diff shows that:

  • local branch: the last single line change is applied
  • remote origin branch: the last single line change is not applied

My featureA branch will be merged to the master branch so I don't want to ruin history.

I went through several threads (suggesting git pul --rebase, git reset --hard, etc) but still do not have clear idea what is the best solution.

I don't mind which state I will be on with the solution. If needed, I don't mind to go back to the previous push/commit and push the new change as a new commit again because it is just a single line code change.

I appreciate your help.

joana
  • 1
  • 1
  • is `git pul --rebase` a typo? In any case, in general you just need to do `git pull` then (if that succeeds) `git push`. What happens when you try these? what is the output? Also fwiw I have no idea what you mean by *pushed to remote by amending commit*. You need to just push your commits. – topsail Dec 03 '22 at 00:18
  • @topsail Yes it was typo for `git pull--rebase`. Thank you for pointing that out. I have not tried those suggestions because I was not sure what would happen or what would be the proper solution to my case. What I did was `amend commit and push to remote` – joana Dec 03 '22 at 05:05

1 Answers1

5

Why this happened is simple: git commit --amend is a lie. It's a useful lie at most times, but it's still a lie. It's literally impossible to change any existing Git commit, so git commit --amend doesn't do that.

Instead, git commit --amend makes a new and improved replacement commit, in your own local repository, pushing the old commit "out of the way" as it were. The old (bad) commit continues to exist—for a while; eventually, if you don't reclaim it (and there's no reason you should) Git will remove it for real.

Because your local repository is a different repository from the other repository (the "remote"), and this change of "which commits we're supposed to use" has happened locally only at this point, your branch and their branch have indeed diverged.

If you have permission—Git itself always gives permission, but many hosting sites take it away (under various conditions)—you can use git push --force to send your new-and-improved commit to the other Git software that is working with the remote repository. This "force push" tells their Git software: I know this new commit discards some previous commit. Do that anyway, even though discarding some previous commit is normally a very bad idea. Since you're quite certain that you do want to discard the commit—it's the one you "amended", and you don't want it back, ever again—it's safe to tell them this. Just be very sure that that's the commit you're going to discard:

  • run git fetch
  • verify that your local git status still says "diverged" and "1 commit" each: that's your "amended" commit vs their original

and then run git push --force or git push --force-with-lease with the remote name (origin) and branch name featureA.

torek
  • 448,244
  • 59
  • 642
  • 775
  • This is an excellent explanation and answer. The only thing I would add is that I would recommend the habit of always specifying the remote and the branch as command argument when pushing to minimize the risk of inadvertently pushing the wrong thing (e.g. the current branch turns out to be different from what you expected) , *especially in the case of force push*, e.g. `git push --force origin featureA` in this case. – hlovdal Dec 03 '22 at 00:43
  • @hlovdal: I did - note that the sentence doesn't end after the verbs... – torek Dec 03 '22 at 00:59
  • @torek Thank you for your great explanation on what happened in my case. What will be alternative solution if I do not have permission for "force push"? Also, I do not understand the point of `git fetch`, how would they perform differently with vs without `git fetch` step? Or was it just to verify if Git still says the same status? – joana Dec 03 '22 at 05:17
  • If you can't force-push, you're stuck with leaving the "bad" commit in the history. (History, in Git, is the set of commits. Git finds these commits by starting at the *last* one, found by the branch name, then working backwards. The point of having `--amend` "kick the commit off the branch" is to get it out of the visible history: the end is now the replacement commit, and the previous-to-end commit is the one that was also previously the previous-to-end.) And, yes, the point of the `git fetch` is to make sure that things are still the way you left them. – torek Dec 04 '22 at 07:51