Skip to content

Instantly share code, notes, and snippets.

@diegoquintanav
Last active August 30, 2019 16:23
Show Gist options
  • Save diegoquintanav/d130ee3ac88d748f5991d99d8f82c2bd to your computer and use it in GitHub Desktop.
Save diegoquintanav/d130ee3ac88d748f5991d99d8f82c2bd to your computer and use it in GitHub Desktop.
events describing error using an interactive rebase, and going back

So I messed up with git

TL;DR

  1. Everything is under control
  2. Did an interactive rebase to move a commit to the base of my branch, forgot to include merge commits
  3. Lost my merge commits
  4. Tried to go back with git reset --hard and reflog
  5. Ended up at the base of my branch, with my unrebased commits dangling after it
  6. Want to move back to step 0, and hopefully do step 1 again, but including merge commits.

What I wanted to do

Say I have this git log

$ git log --abbrev-commit --pretty=oneline --decorate --all --graph
* 8b5f239 (HEAD -> develop) chg: dev: add Tos models to flask-admin !minor
* ce35924 chg: usr: add footer to docs template, plus minor refactoring
* fc3e4ba chg: usr: small refactoring to the dashboards main template !minor
* afd19dd new: dev: add migration script for updating tos revision
* 4b7e005 fix: usr: fix broken CSS !minor
*   1c9222e Merge branch 'feature/contact_us_form' into develop
|\
* \   a6c290c Merge branch 'feature/rewrite-frontend' into develop
|\ \
| * | 09f53e9 (feature/rewrite-frontend) chg: usr: refactor templates, add footer, and more
| * | f7c7265 fix: usr: remove social banners from footer temporarily !minor
| * | 49cd851 new: user: rewrite style of templates !minor
| * | 02b7a47 new: user: rewrite style of main index page
| * | dfbf333 new: user: Add footer to html templates
| * | 6c825f4 chg: dev: replace raw_content block in templates
|/ /
| | *   9b879d6 (refs/stash) On feature/contact_us_form: wip: changes on templates and css for dashboards and docs
| | |\
| |/ /
| | * ed1db25 index on feature/contact_us_form: c657031 new: dev: add routes and tests for a route "`/contact_us`"
| |/
| * c657031 (feature/contact_us_form) new: dev: add routes and tests for a route "`/contact_us`"
|/
*   7056ee8 Merge branch 'fix/delete-unused-blueprints' into develop
|\
| * e747671 (fix/delete-unused-blueprints) chg: usr: remove wiki blueprint and any traces of it
|/
* 3c50a8f (origin/develop, feature/fix-ux-frontend) fix: dev: fix previous tests about anon user and tos !minor

At this point, I wanted to move commit `afd19dd`

```bash
* afd19dd new: dev: add migration script for updating tos revision

to the root of the branch after origin/develop, that is, immediately after commit 3c50a8f and before merge commit 7056ee8.

What I did

First, I did a git rebase -i, once, and did nothing with it. This means that the interactive rebase dialog showed up in vim and I did :q without changing anything. Git did things, however. It rebased something.

Then, I did a git rebase -i again and moved commit to the top of the list, thus rearranging my commits.

Silly me notices I had lost all of my merge commits and I want them, as explained in this SO post. I was desperate. I googled how to undo a git rebase and this showed up.

So I did a git reset hard to the id of

git reset --hard HEAD@{30}
Checking out files: 100% (386/386), done.
HEAD is now at e747671 chg: usr: remove wiki blueprint and any traces of it

Where my reflog is like this

$ git reflog
e747671 (HEAD -> develop, fix/delete-unused-blueprints) HEAD@{0}: reset: moving to HEAD@{29}
e747671 (HEAD -> develop, fix/delete-unused-blueprints) HEAD@{1}: reset: moving to HEAD@{28}
00ec588 HEAD@{2}: rebase -i (finish): returning to refs/heads/develop
00ec588 HEAD@{3}: rebase -i (pick): chg: dev: add Tos models to flask-admin !minor
83ff903 HEAD@{4}: rebase -i (pick): chg: usr: add footer to docs template, plus minor refactoring
5397aab HEAD@{5}: rebase -i (pick): chg: usr: small refactoring to the dashboards main template !minor
45ede36 HEAD@{6}: rebase -i (pick): fix: usr: fix broken CSS !minor
783b08b HEAD@{7}: rebase -i (pick): new: dev: add routes and tests for a route "`/contact_us`"
88c2b05 HEAD@{8}: rebase -i (pick): chg: usr: refactor templates, add footer, and more
89c804c HEAD@{9}: rebase -i (pick): fix: usr: remove social banners from footer temporarily !minor
99ec1dd HEAD@{10}: rebase -i (pick): new: user: rewrite style of templates !minor
24fb1a7 HEAD@{11}: rebase -i (pick): new: user: rewrite style of main index page
19b7221 HEAD@{12}: rebase -i (pick): new: user: Add footer to html templates
0376d45 HEAD@{13}: rebase -i (pick): chg: dev: replace raw_content block in templates
f45153a HEAD@{14}: rebase -i (pick): chg: usr: remove wiki blueprint and any traces of it
b9fcea0 HEAD@{15}: rebase -i (pick): new: dev: add migration script for updating tos revision
3c50a8f (origin/develop, feature/fix-ux-frontend) HEAD@{16}: rebase -i (start): checkout refs/remotes/origin/develop
08a7070 HEAD@{17}: rebase -i (finish): returning to refs/heads/develop
08a7070 HEAD@{18}: rebase -i (pick): chg: dev: add Tos models to flask-admin !minor
542d0dc HEAD@{19}: rebase -i (pick): chg: usr: add footer to docs template, plus minor refactoring
25b5a10 HEAD@{20}: rebase -i (pick): chg: usr: small refactoring to the dashboards main template !minor
e0678b5 HEAD@{21}: rebase -i (pick): new: dev: add migration script for updating tos revision
a48b926 HEAD@{22}: rebase -i (pick): fix: usr: fix broken CSS !minor
52b670d HEAD@{23}: rebase -i (pick): new: dev: add routes and tests for a route "`/contact_us`"
8b2d74f HEAD@{24}: rebase -i (pick): chg: usr: refactor templates, add footer, and more
30ed9f6 HEAD@{25}: rebase -i (pick): fix: usr: remove social banners from footer temporarily !minor
e49dc25 HEAD@{26}: rebase -i (pick): new: user: rewrite style of templates !minor
8daa12e HEAD@{27}: rebase -i (pick): new: user: rewrite style of main index page
fe8e301 HEAD@{28}: rebase -i (pick): new: user: Add footer to html templates
8b3248a HEAD@{29}: rebase -i (pick): chg: dev: replace raw_content block in templates
e747671 (HEAD -> develop, fix/delete-unused-blueprints) HEAD@{30}: rebase -i (start): checkout refs/remotes/origin/develop
8b5f239 HEAD@{31}: reset: moving to HEAD
8b5f239 HEAD@{32}: commit: chg: dev: add Tos models to flask-admin !minor
ce35924 HEAD@{33}: commit: chg: usr: add footer to docs template, plus minor refactoring
fc3e4ba HEAD@{34}: commit: chg: usr: small refactoring to the dashboards main template !minor
afd19dd HEAD@{35}: commit: new: dev: add migration script for updating tos revision
4b7e005 HEAD@{36}: commit: fix: usr: fix broken CSS !minor
1c9222e HEAD@{37}: merge feature/contact_us_form: Merge made by the 'recursive' strategy.
a6c290c HEAD@{38}: merge feature/rewrite-frontend: Merge made by the 'recursive' strategy.
7056ee8 HEAD@{39}: checkout: moving from feature/contact_us_form to develop

I failed and I am now at the first commit after origin/develop, and I lost my commits after that.

Now my history looks like this:

git log --abbrev-commit --pretty=oneline --decorate --all --graph

*   e5f87b6 (refs/stash) On develop: before rebasing migration script  to the beginning
|\
| * 9b9e75e index on develop: 8b5f239 chg: dev: add Tos models to flask-admin !minor
|/
* 8b5f239 chg: dev: add Tos models to flask-admin !minor
* ce35924 chg: usr: add footer to docs template, plus minor refactoring
* fc3e4ba chg: usr: small refactoring to the dashboards main template !minor
* afd19dd new: dev: add migration script for updating tos revision
* 4b7e005 fix: usr: fix broken CSS !minor
*   1c9222e Merge branch 'feature/contact_us_form' into develop
|\
| * c657031 (feature/contact_us_form) new: dev: add routes and tests for a route "`/contact_us`"
* |   a6c290c Merge branch 'feature/rewrite-frontend' into develop
|\ \
| |/
|/|
| * 09f53e9 (feature/rewrite-frontend) chg: usr: refactor templates, add footer, and more
| * f7c7265 fix: usr: remove social banners from footer temporarily !minor
| * 49cd851 new: user: rewrite style of templates !minor
| * 02b7a47 new: user: rewrite style of main index page
| * dfbf333 new: user: Add footer to html templates
| * 6c825f4 chg: dev: replace raw_content block in templates
|/
*   7056ee8 Merge branch 'fix/delete-unused-blueprints' into develop
|\
| * e747671 (HEAD -> develop, fix/delete-unused-blueprints) chg: usr: remove wiki blueprint and any traces of it
|/
* 3c50a8f (origin/develop, feature/fix-ux-frontend) fix: dev: fix previous tests about anon user and tos !minor

What I want

  1. Get to the point before my first rebase
  2. Be able to perform an interactive rebase again, now with preserving merge commits

What should I do? What HEAD index should I point to if I want to git reset?

Answer

Answering my own question, thanks to the help from #git on freenode:

git log --graph --oneline develop@{1}                                                                                                                 
* 00ec588 chg: dev: add Tos models to flask-admin !minor                                                                                                                                   
* 00ec588 chg: dev: add Tos models to flask-admin !minor                                                                                                                                   
* 00ec588 chg: dev: add Tos models to flask-admin !minor                                                                                                                                   
* 83ff903 chg: usr: add footer to docs template, plus minor refactoring                                                                                                                    
* 5397aab chg: usr: small refactoring to the dashboards main template !minor                                                                                                               
* 45ede36 fix: usr: fix broken CSS !minor                                                                                                                                                  
* 783b08b new: dev: add routes and tests for a route "`/contact_us`"                                                                                                                       
* 88c2b05 chg: usr: refactor templates, add footer, and more                                                                                                                               
* 89c804c fix: usr: remove social banners from footer temporarily !minor                                                                                                                   
* 99ec1dd new: user: rewrite style of templates !minor                                                                                                                                     
* 24fb1a7 new: user: rewrite style of main index page                                                                                                                                      
* 19b7221 new: user: Add footer to html templates                                                                                                                                          
* 0376d45 chg: dev: replace raw_content block in templates                                                                                                                                 
* f45153a chg: usr: remove wiki blueprint and any traces of it                                                                                                                             
* b9fcea0 new: dev: add migration script for updating tos revision                                                                                                                         
* 3c50a8f (origin/develop, feature/fix-ux-frontend) fix: dev: fix previous tests about anon user and tos !minor

As I move the index up, I get different states of my tree, probably each one representing a rebase.

(lap-viz) diego@envy:~/Code/lap-viz$ git log --graph --oneline develop@{2}
* 08a7070 chg: dev: add Tos models to flask-admin !minor
* 542d0dc chg: usr: add footer to docs template, plus minor refactoring
* 25b5a10 chg: usr: small refactoring to the dashboards main template !minor
* e0678b5 new: dev: add migration script for updating tos revision
* a48b926 fix: usr: fix broken CSS !minor
* 52b670d new: dev: add routes and tests for a route "`/contact_us`"
* 8b2d74f chg: usr: refactor templates, add footer, and more
* 30ed9f6 fix: usr: remove social banners from footer temporarily !minor
* e49dc25 new: user: rewrite style of templates !minor
* 8daa12e new: user: rewrite style of main index page
* fe8e301 new: user: Add footer to html templates
* 8b3248a chg: dev: replace raw_content block in templates
* e747671 (HEAD -> develop, fix/delete-unused-blueprints) chg: usr: remove wiki blueprint and any traces of it
* 3c50a8f (origin/develop, feature/fix-ux-frontend) fix: dev: fix previous tests about anon user and tos !minor

Finally, develop@{3} has the tree I want, with merge commits and branches, unlike the previous trees that had them flattened because of the rebase.

(lap-viz) diego@envy:~/Code/lap-viz$ git log --graph --oneline develop@{3}
* 8b5f239 chg: dev: add Tos models to flask-admin !minor
* ce35924 chg: usr: add footer to docs template, plus minor refactoring
* fc3e4ba chg: usr: small refactoring to the dashboards main template !minor
* afd19dd new: dev: add migration script for updating tos revision
* 4b7e005 fix: usr: fix broken CSS !minor
*   1c9222e Merge branch 'feature/contact_us_form' into develop
|\
| * c657031 (feature/contact_us_form) new: dev: add routes and tests for a route "`/contact_us`"
* |   a6c290c Merge branch 'feature/rewrite-frontend' into develop
|\ \
| |/
|/|
| * 09f53e9 (feature/rewrite-frontend) chg: usr: refactor templates, add footer, and more
| * f7c7265 fix: usr: remove social banners from footer temporarily !minor
| * 49cd851 new: user: rewrite style of templates !minor
| * 02b7a47 new: user: rewrite style of main index page
| * dfbf333 new: user: Add footer to html templates
| * 6c825f4 chg: dev: replace raw_content block in templates
|/
*   7056ee8 Merge branch 'fix/delete-unused-blueprints' into develop
|\
| * e747671 (HEAD -> develop, fix/delete-unused-blueprints) chg: usr: remove wiki blueprint and any traces of it
|/
* 3c50a8f (origin/develop, feature/fix-ux-frontend) fix: dev: fix previous tests about anon user and tos !minor

So the next thing is to reset back to that symbol, with

git reset --hard develop@{3}

Now I am at step 0. Phew.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment