git revert last commit

0 votes
By mistake I had committed the wrong files to the Git but that didn't push the commit to the general server yet. How should I undo these commits from the local repository as the only possible way seems to be to copy the edits in some kind of a GUI text editor and then wipe the whole local clone after which I can re-clone the repository. Finally, to re-apply the edits. However, this causes data loss which I can't afford to bear and it is very hard to do this only when an accidental git commit was run.I need help in finding out if there is a better way in doing so?
Feb 8, 2022 in Others by Rahul
• 9,680 points
730 views

1 answer to this question.

0 votes

To answer your question, you can try to do a commit and then redo as displayed below for your reference:-

$ 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 above line of code is going to help you with the undo part which will undo the last commit of yours which will also leave the state of your disk and files non touched. You will have to add them once more prior to committing them again. Follow this by making various corrections to the working tree files and then git add anything according to what you want to add to your fresh commit. By reusing your old commit message, commit the changes required and reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD. Priorily, this will include the message log from the previous commit and will ensure access for you to edit if required. The -C option can be used if thought otherwise. To ensure editorial actions for the previous commit, use this:- commit --amend which will update the changes within the current index all the way to the previous commit. Finally, in order to remove a commit which has been sent to the server, the rewriting of the history with the git push origin master --force is necessary.

answered Feb 8, 2022 by Soham
• 9,710 points

Related Questions In Others

0 votes
1 answer

Squash my last X commits together using Git

You can do this relatively easily without ...READ MORE

answered Feb 16, 2022 in Others by Aditya
• 7,680 points
511 views
0 votes
0 answers

Webinar Recording on GIT

Hi Team, I was not able to attend ...READ MORE

Jul 12, 2020 in Others by Sudhakar
• 120 points
778 views
0 votes
1 answer

'git rev-parse HEAD' is not recognized as an internal or external command

Hi@akhtar, I also faced the same problem. You ...READ MORE

answered Jul 17, 2020 in Others by MD
• 95,460 points
4,152 views
0 votes
1 answer

Git refusing to merge unrelated histories on rebase

To answer your question, the “git merge” ...READ MORE

answered Feb 9, 2022 in Others by Rahul
• 9,680 points
715 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,058 views
+2 votes
1 answer
+1 vote
1 answer

git delete branch

The following change in the code given ...READ MORE

answered Feb 8, 2022 in Others by Soham
• 9,710 points
459 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP