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:
- committed changes locally
- made more changes then pushed to remote by amending commit
- 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.
Why this happened is simple:
git commit --amendis 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, sogit commit --amenddoesn't do that.Instead,
git commit --amendmakes 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 --forceto 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:git fetchgit statusstill says "diverged" and "1 commit" each: that's your "amended" commit vs their originaland then run
git push --forceorgit push --force-with-leasewith the remote name (origin) and branch namefeatureA.