Skip to content

Instantly share code, notes, and snippets.

@SeLub
Last active February 8, 2024 06:23
Show Gist options
  • Select an option

  • Save SeLub/f3d61ae628d500e022f28822ad611127 to your computer and use it in GitHub Desktop.

Select an option

Save SeLub/f3d61ae628d500e022f28822ad611127 to your computer and use it in GitHub Desktop.

Revisions

  1. SeLub revised this gist Feb 8, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Remove last commit from git
    # Delete last commit from git

    ## First way

  2. SeLub revised this gist Feb 8, 2024. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    ## Remove last commit from GitHub
    # Remove last commit from git

    ### First way
    ## First way

    ```jsx
    git reset --hard HEAD~1
    git push --force
    ```

    ### Another way
    ## Another way

    ```
    git reset --hard "HEAD^"
    @@ -18,9 +18,9 @@ Both ways works fine.

    ---

    ## Return removed commit to GitHub
    # Recover deleted commit to git

    ### Step 1. Find full SHA of deleted commit
    ## Step 1. Find full SHA of deleted commit

    ```jsx
    git fsck --lost-found
    @@ -34,7 +34,7 @@ dangling commit 1b0b23f462cb4c560c3adfa821bcddd4e53e6a43
    dangling blob b99c193caeccb77af13df8adb0358cee0b93eb82
    ```

    ### Step 2. Recover the dangling commit with rebase
    ## Step 2. Recover the dangling commit with rebase

    ```jsx
    git rebase 5601624a8926d4426ad42768bd0637fd82b70b80
  3. SeLub renamed this gist Feb 8, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. SeLub created this gist Feb 8, 2024.
    46 changes: 46 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    ## Remove last commit from GitHub

    ### First way

    ```jsx
    git reset --hard HEAD~1
    git push --force
    ```

    ### Another way

    ```
    git reset --hard "HEAD^"
    git push origin -f
    ```

    Both ways works fine.

    ---

    ## Return removed commit to GitHub

    ### Step 1. Find full SHA of deleted commit

    ```jsx
    git fsck --lost-found

    Example output:
    Checking object directories: 100% (256/256), done.
    Checking objects: 100% (3/3), done.
    dangling commit 5601624a8926d4426ad42768bd0637fd82b70b80
    dangling commit d5ee3e10a2c8b17b4e3cd7ee0562d6cdafacc5fa
    dangling commit 1b0b23f462cb4c560c3adfa821bcddd4e53e6a43
    dangling blob b99c193caeccb77af13df8adb0358cee0b93eb82
    ```

    ### Step 2. Recover the dangling commit with rebase

    ```jsx
    git rebase 5601624a8926d4426ad42768bd0637fd82b70b80
    git push
    ```

    ---

    Deleted in this way commit still in **git reflog**. But garbage collector should remove typically in 30 days or so.