Languages
[Edit]
EN

Git - rollback commit (using git reset --hard)

4 points
Created by:
Root-ssh
177650

In this quick article we would like to show how to rollback pushed commmit with git with git reset --hard command.

Quick solution:

git reset --hard MY_COMMIT_HASH
git push --force

Note: --hard parameter is optional.

 

Full example

In this section you can see full example how to rollback commit with git reset --hard.

Note: using this approach, it is good to notify other people in team about rolling back to previous version.

Simple steps:

  1. find commit hash you want to recover
    $ git log --graph
    
    * commit 205c851ba0507d04e5e3f00ee48fb2ac01f4fd80 (HEAD -> 2020_03_15_ui, origin/2020_03_15_ui)
    | Author: john <john@mail.com>
    | Date:   Thu Jul 16 18:50:28 2020 +0200
    |
    |     UI: updated REST API for new backend
    |
    * commit 2ec125f7eba3238bcfe2e22b9c44b7c6e22e7052
    | Author: john <john@mail.com>
    | Date:   Tue Jul 14 19:43:49 2020 +0200
    |
    |     UI: common footer component for all service
    |
    * commit c9b6a24575c9cdf1f43da3d45cde00148e7c75a7
    | Author: john <john@mail.com>
    | Date:   Tue Jul 14 17:22:19 2020 +0200
    |
    |     UI: removed unused code
    |
    * commit 465755bead3d3afe386e702540644427c250bcb0
    | Author: john <john@mail.com>
    | Date:   Tue Jul 14 16:29:04 2020 +0200
    |
    |     UI: new routing rules
    |
     
  2. reset changed to proper commit
    (e.g. we want to recover source code to 465755bead3d3afe386e702540644427c250bcb0)
    $ git reset --hard 465755bead3d3afe386e702540644427c250bcb0
    
    HEAD is now at 465755be UI: new routing rules
    
    $ git log --graph
    
    * commit 465755bead3d3afe386e702540644427c250bcb0 (HEAD -> 2020_03_15_ui, origin/2020_03_15_ui)
    | Author: john <john@mail.com>
    | Date:   Tue Jul 14 16:29:04 2020 +0200
    |
    |     UI: new routing rules
    |
    * commit 401d25b3a48c96c08a453def29f9e1df5ec3f25a
    | Author: john <john@mail.com>
    | Date:   Tue Jul 14 14:56:42 2020 +0200
    |
    |     UI: improved left page menu - expand/collapse for submenus
    |
    *   commit b29eca1e52bc0bd2eb7d211158d060338605a9ca
    |\  Merge: c189dc14 f3e85ee7
    | | Author: john <john@mail.com>
    | | Date:   Tue Jul 14 02:48:02 2020 +0200
    | |
    | |     Merge branch '2020_03_15_ui' of https://bitbucket.org/great-team/shop into 2020_03_15_ui
    | |
    | * commit f3e85ee7049dc8d89a7a309d6fe2ea9d3c64a63d
    | | Author: john <john@mail.com>
    | | Date:   Tue Jul 14 00:31:57 2020 +0200
    | |
    | |     UI: fix for image orientation on mobile devices
    | |
     
  3. pushing changes with --force
    Note: notify other team members that you push your changes to remote repository.
    $ git push --force
    
    Total 0 (delta 0), reused 0 (delta 0)
    remote:
    remote: Create pull request for 2020_03_15_ui:
    remote:   https://bitbucket.org/great-team/shop/pull-requests/new?source=2020_03_15_ui&t=1
    remote:
    To https://bitbucket.org/great-team/shop.git
     + 205c851b...465755be 2020_03_15_ui -> 2020_03_15_ui (forced update)

 

Alternative titles

  1. Git - revert commit (using git reset --hard)
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Git

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join