How do I undo the most recent local commits in Git?

Explanation

Undo a commit & redo

$ git commit -m "Something terribly misguided"     # (0: Your Accident) 
$ git reset HEAD~                                  # (1) 
[ edit files as necessary ]                        # (2) 
$ git add .                                        # (3) 
$ git commit -c ORIG_HEAD                          # (4)
  • The first command is used to undo. So sometimes if we don’t a proper commit so that such time we will use this command. Also, this command will have the responsibility of the undo. So this will undo your while without even disturbing the current file working.

 

  •  Next, we will use the command to make a correction. In the files which are currently working.

 

  • git add  this command is used when we want to add in our new commit.

 

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

So what if we want to change or edit the commit. So at that time, we will use commit --amend. By using this we can make changes in the commit that has been given to the server.

 It is necessary to  git push origin master --force   have. Because if we want to delete the commit from the server that is also a rewriting history.

 

Also read, Why are these datagrams reassembled to a single datagram

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *