I have a local branch named as source_report_overview_Approach3 and there is a remote counterpart of it as origin/source_report_overview_Approach3 on github.
At a point , both point at same commit, as shown in the below image.
Now I make another commit(git commit
) to my local branch with message "test commit". This moves my local branch a commit forward while keeping the remote at same place as shown below:
Now I push this commit to remote (git push origin source_report_details_Approach3
). This as I understand git should move remotes/origin/source_report_details_Approach3
to the later commit i.e. test commit
and again my local and remote branch should be in sync and should point to same test commit
, But somehow it does something like shown below:
Now if i do git status
, it gives me the following message:
I believe this should not happen. Shouldn't the
remotes/origin/source_report_details_Approach3
be updated to point to source_report_details_Approach3
automatically.
I made another commit(git commit
) by making changes at the same place in the same file as I had made in commit test commit
with message test commit 2
. It makes the history as follows:
Now i try to push this commit(test commit 2
), then i get the following error:
How may I solve this problem. By whatever I know about git, this is weird to happen.
If I try to pull the branch using git pull origin source_report_details_Approach3
, then it gives merge conflicts. This is because test commit and test commit 2
both make changes at same place in same file. Git try to merge test commit
from remote to my local branch. Since my local file has test commit 2
, it gives a conflict.
Also I am the only person working on the branch. Still because of the problem, I have to solve merge conflicts almost every time I have to push anything.
So with some help I was able to figure out the root cause of the problem.
I had a pre-push hook written which did something as follows:
If that message has a specific text in it, then git commit --amend -m "new message"
and then push the commit.
Logically, #2 should push the amended commit to remote. But the logic is defied here.
What happens is , push
command actually figures out what to push before invoking the hook. so in my case, what went to remote was the non amended commit while my local had the amended commit as the latest. This clearly diverged my local from remote and hence I had this weird thing happening.
Link worth reading and helped me to find that push
determines what to push before invoking the hook: don't commit in pre push hook
Also gitk HEAD @{u}
proved really helpful.