How to undo the most recent commits in Git?

If you accidentally committed wrong files to Git, but you don’t want to push the commit to the server yet. Don't worry, you can still undo those commits from the repo by following few steps.

 

$ git commit -m "Something terribly misguided"                  # (1)

$ git reset HEAD~                                                                        # (2)

<< edit files as necessary >>                                                     # (3)

$ git add ...                                                                                   # (4)

$ git commit -c ORIG_HEAD                                                     # (5)

 

1.       First step is for "what we want to undo"

2.      Second steps leaves working tree (the state of files on disk) unchanged but undo the commit and leaves the changes we committed (so they'll appear as "Changes not staged for commit" in git status, and add them again before committing). If other changes required into previous commit, or want to change the commit message, we could use git reset --soft HEAD~ instead, which is like git reset HEAD~ (where HEAD~is the same as HEAD~) but leaves existing changes staged.

3.      Third step is to make corrections to working tree files.

4.      git add adds anything that user want to include in new commit.

5.      Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows user to edit it. If we do not need to edit the message, then use the -C option.

Beware however that if you have added any new changes to the index, using commit --amend will add them to previous commit.

If the code is already pushed to a server and user have a permissions to overwrite history then:

git push origin master --force



Was this article helpful?

Top Related Jobs

Top Freelance jobs are waiting for you, View & apply to earn more.

  • I need Laravel or Symfony developer for a long term

    $14, Hourly | France
    We are a digital agency and need to develop clients' websites. We are looking for dedicated a full-stack developer with knowledge Russian. who moves t...
  • Frontend development

    $18, Hourly | Canada
    We are currently doing a redesign of our websiteThe number of pages and structure of the site will be roughly the same as at the moment. There will be...
  • Professional Front End Web Developer

    $18, Hourly | United States
    We are looking for a highly skilled Web Developer to help us build the next generation crowdsourcing platform.The ideal candidate will have experience...
  • React.js /Ruby on Rails Fullstack Developer

    $18, Hourly | France
    We are looking for a talented full-stack developer to speed up the delivery of our growing product for SEO specialists. The tool analyzes the backlink...
  • Frontend React.js Developer

    $18, Hourly | United States
    We are looking for a talented React.js developer to lead the frontend development of our growing product for SEO specialists. The tool analyzes the ba...


People also searched for

Natural Language Processing in Python
Natural Language Processing in Python
Web Development

NLP is a branch of data science that consists of systematic processes for analyzing, understanding, and deriving information from the text information in a smart and efficient mann...

Read More
How to post a Job & Hire a great WordPress developer
How to post a Job & Hire a great WordPress develop...
Web Development

WordPress is an open source Content Management System (CMS), which allows the users to build dynamic websites and blog. WordPress is the most popular blogging system on the web and...

Read More
Some Useful JavaScript Tips,Tricks and Best Practices
Some Useful JavaScript Tips,Tricks and Best Practi...
Web Development

As you know, JavaScript is the top programming language in the world, the language of the web, of mobile hybrid apps (like PhoneGap or Appcelerator), of the server side (like NodeJ...

Read More

You are freelancing Ninja?

Try Toogit Instant Connect today, The new virtual assistant for your freelancing world.


What our users are discussing about Git