Search code examples
gitversion-controlgithooksgit-pushgit-pull

Git push diverges the remote and local branch


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.

  1. At a point , both point at same commit, as shown in the below image.

    enter image description here

  2. 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:enter image description here

  3. 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: enter image description here

  4. Now if i do git status, it gives me the following message: enter image description here 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.

  5. 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: enter image description here

  6. Now i try to push this commit(test commit 2), then i get the following error: enter image description here enter image description here

  7. How may I solve this problem. By whatever I know about git, this is weird to happen.

  8. 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.

  9. 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.


Solution

  • 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:

    1. Find the last commit message from history.
    2. If that message has a specific text in it, then git commit --amend -m "new message" and then push the commit.

    3. Logically, #2 should push the amended commit to remote. But the logic is defied here.

    4. 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.