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.

  • Knowledge Engineer

    $36, Hourly | United States
    Structuring and formalization of information, design and programming of cognitive systems (knowledge processing systems).The specialist will need to d...
  • [Freelance] Front-End Developers

    $2,000, Fixed | United States
    We are looking for freelance front end developers who want to work in our developer community. The work is basically taking designs, cutting them into...
  • Release Manager - Git Expert - Merging - Branching - CD/CI

    $16, Hourly | United States
    We are looking to hire a Release Manager to manage all the multiple releases that our company executes in a weekly basis. We have over 15 Repos with...
  • UX/UI Facelift for Simple Angular4 app

    $1,200, Fixed | Russia
    We have an angular4+ app using { less} css that walks through a series of events (text prompts) and presents a series of design elements to go along...
  • Web Developer

    $18, Hourly | India
    Looking for a developer who loves to work on complex projects with out of the box solutions. Must be able to work remotely with others on the team. Sh...
  • 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...


People also searched for

 
Learn Web Scraping using Python
Web Development

The importance of extracting information from the web is becoming increasingly loud and clear. Every few weeks, I realize myself in a situation where we need to extract information...

Read More
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

You are freelancing Ninja?

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


What our users are discussing about Git