InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Git tutorials

  • Github for beginners
  • Undo last commit
  • List git branches
  • Git checkout remote branch
  • Diff two branches
  • Show log of one user's commits
  • View diff for a given commit id
  • Setup password less git push for github
  • Git - Check if a hash is valid
  • Git - display specific length commit hash
  • Git - rename local branch
  • Git create branch
  • Git delete branch
  • Git detached head
  • Git display info of last commit (HEAD) in one line
  • Git display local HEAD location
  • Git log with file names
  • Git show details of a commit hash
 
  • Home
  • > Tutorials
  • > Git

Git – how to undo last commit

By admin on Oct 12, 2015

Sometime we need to undo last commit in git. This is fairly useful to undo a change made accidentally. Here are quick instructions to do it:

git – soft undo last commit

Undo last commit but keep the changes. This is default option. So you can also skip --soft.

$ git reset --soft HEAD~1
$ git status

In case you need to undo last two commits you can use HEAD~2 as shown below:

$ git reset --soft HEAD~2
$ git status

git – hard undo last commit

Undo last commit and discard the changes

$ git reset --hard HEAD~1
$ git status

Note that HEAD~ or HEAD~1 refers to the first parent of the tip of the current branch and HEAD~2 refers to first parent of first parent and so on.

Suggested posts:

  1. Debug javascript using node-inspector, node-debug, nodemon and Chrome
  2. WordPress – write custom php log to separate file
  3. Git – Check if a hash is valid
  4. Git display local HEAD location
  5. Git – display specific length commit hash
  6. Git log with file names
  7. AngularJS format date using date filter
  8. Php equal (==) vs identical (===)
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Git, Linux/Unix Command Line, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress