How can i undo my last Commits on Git?


I have committed some files on my Git using Visual Studio, which i want to undo now, as the code and i want to change it without pushing it to server, i haven't pushed the committed files on server yet, so how can i achieve it using Visual Studio or with some other source?

 


Asked by:- bhanu
2
: 3760 At:- 9/11/2017 2:09:35 PM
git git-commit git-undo-commit git-revert







2 Answers
profileImage Answered by:- jaya

You need to run this command using Git terminal:

git reset HEAD~

If you don't want the changes and remove everything, run this :

git reset --soft HEAD^     # Use --soft if you want to keep your changes
git reset --hard HEAD^     # Use --hard if you don't care about keeping the changes you made

OR

you can also use commit Id

Use git revert <commit-id>.

To get the commit ID, just use git log.

OR

On SourceTree (GUI for GitHub), you may right-click the commit and do a 'Reverse Commit'. This should undo your changes.

OR

You can use Graphical way to revert commit

Using Git Extensions. You can download it from here link1 or from here link2

Once this is done Git Extensions will show you all the commits. Select any specific commit and right click on it. Then select revert commit. Check the below screenshot for reference

reverse-commit-using-git-extension

Once you select the Revert Commit option, it opens a pop-up, from pop-up select "Automatically create a commit" if you want to directly commit the reverted changes or if you want to manually commit the reverted changes keep the box un-selected and click on "Revert this commit" button.

3
At:- 9/12/2017 3:08:27 PM Updated at:- 12/14/2022 5:52:27 AM
great, for me the second alternative was good, as I don't want to run commands and it looks easier also. 0
By : bhanu - at :- 9/16/2017 9:50:42 AM


profileImage Answered by:- Vinnu

You can undo the last commit:

git reset --soft HEAD~

Or you can undo the time before last time commit:

git reset --soft HEAD~2

Or you can undo any previous commit:

git reset --soft <commitID>

(you can get the commitID using git reflog)

When you undo a previous commit, remember to clean the workplace with

git clean

You can find more details in the docs: git-reset

hope this helps

1
At:- 9/14/2017 2:05:23 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use