3

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.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
Gaurav Kumar
  • 1,091
  • 13
  • 31
  • The images are missing. – venkatKA Jan 08 '16 at 06:51
  • It sounds unusual. But given that you're the only person working on the branch, and if you're sure that your local branch is perfect, what stops you from doing a `git push origin source_report_details_Approach3 -f`? – venkatKA Jan 08 '16 at 06:53
  • @venkatKA Images are there. Try refreshing. I can see the images in the question. – Gaurav Kumar Jan 08 '16 at 06:53
  • @venkatKA Ofcourse I can do that. But the question is why is this happening. It shouldn't. right? – Gaurav Kumar Jan 08 '16 at 06:54
  • Can anyone else see the images?! I tried with multiple browsers 0_o – venkatKA Jan 08 '16 at 06:57
  • your operations seem legit; if these are the only commands you did... This might be an issue of line endings... are both machines linux or windows; or one of each? – Chris Maes Jan 08 '16 at 07:22
  • 1
    do you have any hooks on your server? – Chris Maes Jan 08 '16 at 07:22
  • to see the diffferences after step 3 you could use this command; which might indicate the problem: `git diff origin/source_report_details_Approach3` – Chris Maes Jan 08 '16 at 07:23
  • @ChrisMaes man, you , i believe has pointed me to the root of the problem. I have a pre push hook. I looked into its code and found that it actually does a git commit --amend -m "some message". Then I have read on SO that git commit --amend does do something with the history. I am finding the solution and will update asap. Thanks – Gaurav Kumar Jan 08 '16 at 07:44
  • you're welcome; I added it as an answer so other users can see what was the problem. Good luck :) – Chris Maes Jan 08 '16 at 07:55

3 Answers3

1

It seems the problem is due to hooks on the server side. If these change the commit, then off course there will be a difference between the remote commit and the local one. You can check the difference between the local and the remote commit like this:

git diff origin/source_report_details_Approach3

note : this will only show differences in the files that changed; not differences between the commit message. I don't know any easy way to see a difference in commit message, but you can see them yourself using:

git show --stat source_report_details_Approach3 > local
git show --stat origin/source_report_details_Approach3 > remote
diff local remote
rm local remote

some explanations:

  • git show : show a complete commit with message
  • --stat : option to show only the number of lines changed in a file; makes the output way shorter
  • I create two temporary files. This is a workaround, but I didn't find a better way to do this (yet)
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • git diff origin/source_report_details_Approach3. This only shows the differences, that are there in code between the local and remote branch. The pre push hook in my case only amends the message. – Gaurav Kumar Jan 08 '16 at 08:23
  • you're right :) but I don't know how to see only differences in the commit message. I adapt my answer a little... with a workaround – Chris Maes Jan 08 '16 at 08:40
1

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.

Community
  • 1
  • 1
Gaurav Kumar
  • 1,091
  • 13
  • 31
0

Try git show-ref source_report_details_Approach3 and check what references are available along with the associated commit IDs. I have a suspicion that you might be having a similar problem to what's mentioned in this question

Community
  • 1
  • 1
Amit Joshi
  • 361
  • 1
  • 8