Skip to content

Instantly share code, notes, and snippets.

@yashpatel2202
Forked from jbgo/git-recover-branch.md
Created May 10, 2023 04:01
Show Gist options
  • Save yashpatel2202/4977aadde812cdffcda4adffeee3e19d to your computer and use it in GitHub Desktop.
Save yashpatel2202/4977aadde812cdffcda4adffeee3e19d to your computer and use it in GitHub Desktop.

Revisions

  1. @jbgo jbgo revised this gist Aug 19, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-recover-branch.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ### UPDATE August 2015 - A better way!
    ### UPDATE: A better way! (August 2015)

    As pointed out by @johntyree in the comments, using [git reflog](http://git-scm.com/docs/git-reflog) is easier and more reliable. Thanks for the suggestion!

  2. @jbgo jbgo revised this gist Aug 19, 2015. 1 changed file with 31 additions and 0 deletions.
    31 changes: 31 additions & 0 deletions git-recover-branch.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,34 @@
    ### UPDATE August 2015 - A better way!

    As pointed out by @johntyree in the comments, using [git reflog](http://git-scm.com/docs/git-reflog) is easier and more reliable. Thanks for the suggestion!

    ```
    $ git reflog
    1ed7510 HEAD@{1}: checkout: moving from develop to 1ed7510
    3970d09 HEAD@{2}: checkout: moving from b-fix-build to develop
    1ed7510 HEAD@{3}: commit: got everything working the way I want
    70b3696 HEAD@{4}: commit: upgrade rails, do some refactoring
    98f2fc2 HEAD@{5}: commit: a couple code cleanups
    d09f35e HEAD@{6}: commit: remove test method - it served it's purpose and now it must go
    d586a93 HEAD@{10}: commit: aha! that is why I'm so fail
    4644046 HEAD@{11}: commit: cleaning up the initial migration for dev/test environments
    323df37 HEAD@{15}: commit: bump ruby version
    eab861c HEAD@{16}: commit: bundle update EVERYTHING
    2b544c4 HEAD@{17}: commit: fixing what few tests actually exist - a.k.a., wow! does this app even work?
    3970d09 HEAD@{18}: checkout: moving from develop to b-fix-build
    3970d09 HEAD@{19}: pull: Fast-forward
    ```

    Once you find the commit you're looking for, create a new branch from it and you're done!

    ```sh
    $ git checkout -b branch-name 70b3696
    Switched to a new branch 'branch-name'
    ```


    ### Original post

    Oops! I accidentally deleted a local git branch, and I haven't pushed it to a remote server yet. The branch has several important commits, and it hasn't been merged with any other branches yet. How do I find the missing branch?

    ### 1. Create a list of all dangling or unreachable commits.
  3. @jbgo jbgo revised this gist Feb 29, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-recover-branch.md
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ cb3d67e1aa2226ab9d816fc541f36ff698bfda41 WIP on master: 40a4453 Use #website_url
    def0a251bd29b7fc54a5622e364711f60097b826 Example tabs for export show page (no styles)w
    ```
    ### 3. Find your missing commit through the process of manual inspection (a.k.a reading).
    ### 3. Find your missing commit through the process of manual inspection (i.e. reading).
    If you need more information on a commit, you can always use a more detailed log command, such as `git log -p --stat --color 9ae38fc`.
  4. @jbgo jbgo renamed this gist Feb 29, 2012. 1 changed file with 2 additions and 8 deletions.
    10 changes: 2 additions & 8 deletions git-recover.md → git-recover-branch.md
    Original file line number Diff line number Diff line change
    @@ -16,24 +16,18 @@ These commits are copied into `.git/lost-found/commit/`, and non-commit objects

    ### 2. Print a list of commit messages for all commits in the lost and found.

    ```sh
    $ ls -1 .git/lost-found/commit/ | xargs -n 1 git log -n 1 --pretty=oneline
    11d4daacabc388eca5f3dafdac2b93172200aded WIP on master: 40a4453 Use #website_url instead of #template_url or #url
    63b65d3784b16f92bb370ad6a2c1091a05824ecc Call #to_s on value before calling some string methods, like gsub
    6ed99e63db69ca04f0cc78081a1fd471289551b2 On master: search and reset page
    74a961a76b67b142a94d1d9ce86baf4f21f785c7 On master: RV Search export
    9273d9f5d33b5a70c2270c6c19e5a4d13024b361 WIP on xml-dsl: 67473e8 Now XML builder actually uses options given in the spec
    927f26c372296ebca07629ab402f5bce00d27685 On master: FtpTransport#deliver_all
    973d9be3e2cefcd0c5801ad9cd1b2e18774b4bee Rename decorator proxy to decorator context
    9ae38fc6b0548cab08ccee1178db0ba0edeafdb2 foo
    9e994ca0c0c4785ab45bf64b367fdacccc4575a9 foo [#12345]
    9efa6b28b3b0a89c312484f28cf589385d613dfd On master: mysql db config
    a2a50727a0bc1e77b92008937b199c2122e96d14 WIP on export_spec_dsl: beb1223 added support for collections in export DSL
    bb23ec415a874e0d23557ce412a8312465ba5188 Get rid of the ExportService#wone_file_per_dealer column
    c57a67c7e1c21fa0c32f152e73d8c3376cad19a0 bar
    cb3d67e1aa2226ab9d816fc541f36ff698bfda41 WIP on master: 40a4453 Use #website_url instead of #template_url or #url
    d8fd8b73e538ea3310f17c776524a08a4fdb0536 WIP on master: 40a4453 Use #website_url instead of #template_url or #url
    def0a251bd29b7fc54a5622e364711f60097b826 Example tabs for export show page (no styles)w
    e70fd56f37c1c7c1988f43665a0eb3a41e9bea20 Recent jobs API
    ```
    ### 3. Find your missing commit through the process of manual inspection (a.k.a reading).
  5. @jbgo jbgo created this gist Feb 29, 2012.
    47 changes: 47 additions & 0 deletions git-recover.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    Oops! I accidentally deleted a local git branch, and I haven't pushed it to a remote server yet. The branch has several important commits, and it hasn't been merged with any other branches yet. How do I find the missing branch?

    ### 1. Create a list of all dangling or unreachable commits.

    ```sh
    $ git fsck --full --no-reflogs --unreachable --lost-found
    unreachable tree 4a407b1b09e0d8a16be70aa1547332432a698e18
    unreachable tree 5040d8cf08c78119e66b9a3f8c4b61a240229259
    unreachable tree 60c0ce61b040f5e604850f747f525e88043dae12
    unreachable tree f080522d06b9853a2f18eeeb898724da4af7aed9
    unreachable blob bf01f514add2ada00a7ae5c666493d30d639018c
    ...
    ```

    These commits are copied into `.git/lost-found/commit/`, and non-commit objects are copied into `.git/lost-found/other/`.

    ### 2. Print a list of commit messages for all commits in the lost and found.

    $ ls -1 .git/lost-found/commit/ | xargs -n 1 git log -n 1 --pretty=oneline
    11d4daacabc388eca5f3dafdac2b93172200aded WIP on master: 40a4453 Use #website_url instead of #template_url or #url
    63b65d3784b16f92bb370ad6a2c1091a05824ecc Call #to_s on value before calling some string methods, like gsub
    6ed99e63db69ca04f0cc78081a1fd471289551b2 On master: search and reset page
    74a961a76b67b142a94d1d9ce86baf4f21f785c7 On master: RV Search export
    9273d9f5d33b5a70c2270c6c19e5a4d13024b361 WIP on xml-dsl: 67473e8 Now XML builder actually uses options given in the spec
    927f26c372296ebca07629ab402f5bce00d27685 On master: FtpTransport#deliver_all
    973d9be3e2cefcd0c5801ad9cd1b2e18774b4bee Rename decorator proxy to decorator context
    9ae38fc6b0548cab08ccee1178db0ba0edeafdb2 foo
    9e994ca0c0c4785ab45bf64b367fdacccc4575a9 foo [#12345]
    9efa6b28b3b0a89c312484f28cf589385d613dfd On master: mysql db config
    a2a50727a0bc1e77b92008937b199c2122e96d14 WIP on export_spec_dsl: beb1223 added support for collections in export DSL
    bb23ec415a874e0d23557ce412a8312465ba5188 Get rid of the ExportService#wone_file_per_dealer column
    c57a67c7e1c21fa0c32f152e73d8c3376cad19a0 bar
    cb3d67e1aa2226ab9d816fc541f36ff698bfda41 WIP on master: 40a4453 Use #website_url instead of #template_url or #url
    d8fd8b73e538ea3310f17c776524a08a4fdb0536 WIP on master: 40a4453 Use #website_url instead of #template_url or #url
    def0a251bd29b7fc54a5622e364711f60097b826 Example tabs for export show page (no styles)w
    e70fd56f37c1c7c1988f43665a0eb3a41e9bea20 Recent jobs API

    ### 3. Find your missing commit through the process of manual inspection (a.k.a reading).

    If you need more information on a commit, you can always use a more detailed log command, such as `git log -p --stat --color 9ae38fc`.

    ### 4. Create a new branch with the missing commit as the branch head.

    ```sh
    $ git checkout -b branch-name 9ae38fc
    Switched to a new branch 'branch-name'
    ```