Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

how do i undo the most recent commits in git

$ 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)
git reset is the command responsible for the undo. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. You'll need to add them again before you can commit them again).
Make corrections to working tree files.
git add anything that you want to include in your new commit.
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 you to edit it. If you do not need to edit the message, you could use the -C option.
Alternatively, to edit the previous commit (or just its commit message), commit --amend will add changes within the current index to the previous commit.

To remove (not revert) a commit that has been pushed to the server, rewriting history with git push origin main --force[-with-lease] is necessary. It's almost always a bad idea to use --force; prefer --force-with-lease instead, and as noted in the git manual:

You should understand the implications of rewriting history if you [rewrite history] has already been published.

Further Reading
You can use git reflog to determine the SHA-1 for the commit to which you wish to revert. Once you have this value, use the sequence of commands as explained above.

HEAD~ is the same as HEAD~1. The article What is the HEAD in git? is helpful if you want to uncommit multiple commits.
Comment

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

$ 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)
Comment

How to undo the most recent local commits in Git

$ git commit -m "Your wrong commit" 		   # (0: Your Accident)
$ git reset HEAD~                              # (1)
[ edit files as necessary ]                    # (2)
$ git add .                                    # (3)
$ git commit -c ORIG_HEAD                      # (4)
// 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
Comment

undo the most recent local commits in Git

git commit -m “this was a mistake”
Comment

undo the most recent local commits in Git

git reset HEAD~
Comment

PREVIOUS NEXT
Code Example
Shell :: find docker compose location 
Shell :: how to install software in linux terminal 
Shell :: read only in linux 
Shell :: pod reference github 
Shell :: how to ssh copy to my aws ec2 linux 
Shell :: git view entire log 
Shell :: laravel routes return not found after setting virtual host on localhost linux 
Shell :: nativescript create angular project 
Shell :: duf command ubuntu 
Shell :: shell create random mac 
Shell :: Apache2 Ubuntu Default Page 
Shell :: batch set from file 
Shell :: How to remove every other line with sed? 
Shell :: mendeley on ubuntu 
Shell :: powershell compare 
Shell :: ascii to binary in bash 
Shell :: terminator linux is not start 
Shell :: how to take a screenshot linux terminal 
Shell :: git delete branches not on remote 
Shell :: linux which command 
Shell :: git pull 
Shell :: step6 pgadmin ubuntu 20.04 
Shell :: how to setup pre commit hook on my local 
Shell :: linux print pdf from console 
Shell :: git alias variables 
Shell :: bash echo each line 
Shell :: bash list files for user 
Shell :: how to delete remote file locally on git 
Shell :: connectivity_plus flutter 
Shell :: jinja escape 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =