Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local branch and remote branch have diverged after amending commit

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.

like image 606
joana Avatar asked Nov 24 '25 17:11

joana


1 Answers

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.

like image 141
torek Avatar answered Nov 26 '25 10:11

torek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!