oneloop oneloop - 4 months ago 15
Git Question

Git: undoing an unknown mistake

I have some really messed up git repos and I'd like to get something straight.

In my local machine, on bitbucket, and in all but one production machines I have the following history:

(local and bitbucket)

* 1a84374 (HEAD, master) message
*
*
|\
| *
| *
* |
* | ea2d82c message
|/
* ccd87f7 message
*
*


Now recently an issue arose where I had to ssh into one of my production machines and quickly code on it. Imagine extremely time critical type of thing. I guess because I was hasty, I must've broken something in that repo. I remember doing some rebases, I'm guessing that's where the mistake was. In any case, I saved my new code into a new branch. In this production machine the history now suddenly looks like

* db19412 (HEAD, quickfix) message
* 57f8865 (origin/master, master) Merge branch 'master' of https://bitbucket.org etc etc
|\
| * 1a84374 message
| *
| *
| |\
| | *
| | *
| * |
| * | ea2d82c message
| |/
* | 39052e5 Merge branch 'master' of https://bitbucket.org etc etc
|\ \
| |/
| * ccd87f7 message
* |


You'll notice there's now an extra line connecting the new commits with some much older commits. I think I must've done some mistake with a rebase. I want to get rid of it, but I still want to keep the new code that I added (in branch quickfix) and eventually push it to bitbucket. Namely, the commit
db19412
should connect directly to
1a84374
.

So how do I get rid of the extra line that's just making the repo overly complicated?

EDIT: Because this question revolves around undoing something, I was making attempts in that vein. One of things I tried was the classic "undo last commit". I placed myself on
57f8865
and used the command
git reset --hard HEAD~1
in hope that as a first step I could at least bring
master
back to
1a84374
. This is the resulting history:

* db19412 (quickfix) message
* 57f8865 (origin/master, master) Merge branch 'master' of https://bitbucket.org etc etc
|\
| * 1a84374 message
| *
| *
| |\
| | *
| | *
| * |
| * | ea2d82c message
| |/
* | 39052e5 (HEAD, master) Merge branch 'master' of https://bitbucket.org etc etc
|\ \
| |/
| * ccd87f7 message
* |


I'm completely lost.

EDIT2: I guess one of the things I could do was to copy past the changes in the quickfix into a new branch in my local machine, push that to bitbucket, and then nuke the screwed up repo on that production machine, and they clone anew from bitbucket. But then I wouldn't have learned anything :D

Answer
git checkout 1a84374
git cherry-pick db19412

This addes your quickfix to the top of the commit you wanted. Now this history can be merged with other work that came from 1a84374.

Next:

  • Add this change to a local branch: git checkout -b branch_name, or
  • Push this change to a remote branch: git push HEAD:remote_branch