简体   繁体   中英

Git remote tracking branch diverged from local branch with identical commits with different SHAs

Here is my git repo's current state (as visualised in GitX).

分散的git历史

The '34e...' and 'c3d...' commits (the 'Implemented a global...' commented ones) are IDENTICAL. I confirmed this with git diff, and they even have identical commit times! The only difference is their SHAs.

I have no idea how I got my repo into this state. While I am no git pro, I have been using it for a while now and have become very comfortable with all the basics. This has occurred out of the blue, and I wasn't experimenting with any git features or workflows that I hadn't used before, so I am rather confused.

No one else has committed anything to the remote, so I can change history there, but a solution not involving that would be better.

I could just do the normal merge or rebase of master and origin/master, but I am uncomfortable with that as the history will then show 2 identical commits.

Is it possible to checkout origin/master and then rebase all the commits starting at 'a4a...' onto origin/master, and then switch master to this new HEAD? (basically leaving the 'c3d...' commit hanging by itself, which doesnt matter as its the dupe)

1) So whats the preferred way to fix this?
2) Any ideas how this happened? Anyone else seen it before?

EDIT:
git diff c3db784817 34e1ab666a : outputted nothing.

git reflog master :

Bender:mt-d-styles tyson$ git reflog master
9579294 master@{0}: commit: Added debug only console printing to test image view
1155228 master@{1}: commit: Added new marker interface that custom cells can imp
a4ab788 master@{2}: commit: Added a new StyledRootElement that automatically app
c3db784 master@{3}: commit: Implemented a global technique for styling all exist
34e1ab6 master@{4}: commit: Implemented a global technique for styling all exist
8519fb1 master@{5}: commit: Extended the remove method to give callers access to
30aeee6 master@{6}: commit: Added a new factory method based Side Swipe View con

git reflog origin/master :

Bender:mt-d-styles tyson$ git reflog origin/master
34e1ab6 refs/remotes/origin/master@{0}: update by push
8519fb1 refs/remotes/origin/master@{1}: update by push
495e0ef refs/remotes/origin/master@{2}: update by push
c5fec81 refs/remotes/origin/master@{3}: update by push
cba1e0f refs/remotes/origin/master@{4}: update by push
9ee1ffb refs/remotes/origin/master@{5}: update by push
68ee429 refs/remotes/origin/master@{6}: update by push
0e2d199 refs/remotes/origin/master@{7}: update by push
8a4de84 refs/remotes/origin/master@{8}: update by push

EDIT 2:
git log --format=raw --decorate --graph --all :

*  commit c3db7848171f396c5a595a35dd6b609c119f9e84 
| tree 998e9749546d05178798c8a462d3eff02a111f4c 
| parent 8519fb17e77b8ae865e071772ae652316df8822a 
| author Tyson <tyson> 1364529327 +0800 
| committer Tyson <tyson> 1364539365 +0800 
|  
|     Implemented a global technique for styling all existing MT.D element backg 
|  


| * commit 34e1ab666a81dde7582ee9e31bfa961420d38f55 (origin/master) 
|/  tree 38f9e0c3d936c702fdcd18d215a2f0a88280893b 
|   parent 8519fb17e77b8ae865e071772ae652316df8822a 
|   author Tyson <tyson> 1364529327 +0800 
|   committer Tyson <tyson> 1364529327 +0800 
|    
|      Implemented a global technique for styling all existing MT.D element bac 
|

The Cause : You probably did some history rewriting. If the commits where exactly identical, the SHA would automatically be the same. What you see in you UI is not the commit date, but the author date. Run git log --format=raw --decorate ̵-graph --all to get more details. I guess you will see that the commit date of your local version is a later date. That is caused by history rewriting, usually amending or rebasing.

The Solution : If you know how to go back, you could just try pulling with rebase – if the commits really are identical, git should realize this and only add one commit. If you don't know how to go back, just rebase master onto origin/master , cutting at c3db784817:

git rebase --onto origin/master c3db784817 master

@Your question in the comments:

How I know you amended from the reflog:

c3db784 master@{3}: commit: Implemented a global technique for styling all exist
34e1ab6 master@{4}: commit: Implemented a global technique for styling all exist

Your master branch was at 34e1ab6 , after you created a commit. Then you pushed this commit. Then your master branch moved to c3db784 – a commit with the same message, and your git client says the reason for this was commit . Since there was no branch head movement in between, this smells a lot like an amend. The command line client would say commit (amend) though.

And your log tells me that you did the first commit at unix time 1364529327 (2013-03-28 20:55:27) and then amended it at unix time 1364539365 (2013-03-28 23:42:45). (And that you probably live in the US ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM