12

So my colleague was editing some code on a repo, however she had not pulled some recent changes and so the software would not allow her to commit her changes.

We went into the shell and pulled the commits that she had omitted and she was able to push, the only problem is that it drew those changes into her diverged branch and attributed those commits to her.

It looks like this:

A-B-C-D-E (say D and E were by someone else)

My colleague had this:

A-B-C, and wanted to add some more on.

So she pulled D and E and pushed those changes and now it looks like this:

A-B-C-D-E-F, except that the changes D, E, and F are now from her account.

Is there away to keep her changes (F) but have the timeline revert to when D and E were attributed to someone else?

Thanks!

Graphic representation of problem:

A-B-C-D-E (most recent version of master)

A-B-C (colleague has this)

A-B-C-F (colleague makes edits) (branches hath diverged)

!ERROR! (colleague tries to commit and push)

A-B-C-D-E-F (colleague merges master into her branch)

A-B-C-D-E-F (colleague pushes)

GitHub UI now does not display changes D-E, they are lumped into a "Merge branch 'master' ..."

2
  • "attributed those commits to her" - that should not have occurred under any normal circumstances. Can you provide details on repro steps? Commented Jul 13, 2016 at 23:05
  • Yes, so, there were originally three recent commits on that branch (master) and they were on July 6th, July 7th, and July 8th. My colleague had only pulled changes as recent as July 5th. However it appears she started editing based on what she had, and so the shell told her that her branch had diverged from master. So she merged the commits from master INTO her branch, and then pushed. As a consequence it seems like the July 6th, 7th, and 8th commits are now from her. On the GitHub website I see the whole history, but in the software the 6th, 7th, and 8th commits are gone. Commented Jul 13, 2016 at 23:07

2 Answers 2

9

With git log check which commit is the one before the merge. Note the sha.

Then you can reset it using: git reset --hard commit_sha

Also if you want to, using your example, remove D and E then do the following. Except it will also remove F. That is, the last 3. git reset --hard HEAD~3

Sign up to request clarification or add additional context in comments.

1 Comment

what happens exactly if you push this to the remote?
0

If you used git merge $main_branch, GitHub's UI may show some metadata making it look like you implemented the changes temporarily, but when you finally merge everything into your main branch, everything will be okay. If you are still concerned, the best option here is to simply:

  1. Checkout a new branch from master/current_branch (wherever the changes were from the "other team member(s)", i.e. D,E commits)

  2. git cherry-pick $your_commits for all 3 of your commits. The order will now be: D-E-A-B-C, or D-E-F-A-B-C if F is also on the master branch.

3 Comments

My colleague never used the git-merge command though, what she did was git-pull the D,E commits and then git-push. As a consequence the D,E commits are gone from the History in the GitHub UI and are lumped into a merge from her. The GitHub website shows the previous commits though.
@JamesYen, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.
@JamesYen To minimize confusion you're probably best off starting on a clean branch with the D through F commits already, and then cherry-picking A thru C. It sounds like there may be some weirdness due to the order of commits. Another option is to manually git fetch and then git-rebase so that all the previous (other folks') commits are added first, THEN your commits

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.