1

i have a file (script.js)e with this code :

alert("Hello");

i added the file to index then commited it :

git add script.js
git commit -m "say hello"

i changed the script to this

alert("Hello adam");

and then commited the changes now i want to undo last commit with this commande

$ git reset --soft HEAD~1

so the last commit was deleted but the file still contain the name "adam" why? what i want is this : when i reset a commit i wan to delet last changes on my file

phd
  • 82,685
  • 13
  • 120
  • 165
adam lolo
  • 53
  • 4
  • Its because you used a soft reset. Soft reset will un-commit the files (still have them tracked) but leave them intact. You can either do a hard reset, checkout the file, or checkout the previous commit. If you choose to do the last two, I recommend doing a git log so you can see which hash you want to checkout/reset to. – D3TXER Sep 23 '19 at 02:01
  • i commited changes and i want to undo it – adam lolo Sep 23 '19 at 02:52
  • how can i do that with code – adam lolo Sep 23 '19 at 02:52
  • 1
    Possible duplicate of [How do I undo the most recent local commits in Git?](https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git) – AElMehdi Sep 23 '19 at 09:20
  • Also see https://stackoverflow.com/q/3528245/7976758 – phd Sep 23 '19 at 09:38

3 Answers3

3

git reset has three modes, "hard", "soft", and "mixed" which describe what happens to the working tree (ie. your files) and the staging area.

  • --soft leaves both the staging area and your files alone.
  • --hard discards all changes in the staging area and files.
  • --mixed resets the index but leaves the working tree alone.

With that in mind, let's look at what you did.


Let's say your repo looks like this.

A - B [master]

You commit alert("Hello");, that becomes commit C.

A - B - C [master]

You commit alert("Hello adam");, that becomes commit D.

A - B - C - D [master]

You run git reset --soft HEAD~1. HEAD~1 is commit C with alert("Hello");. So master has moved back to commit C undoing the alert("Hello adam"); commit.

Since you used --soft both the index and files will remain the same. The files will still say alert("Hello adam"); and you will see a diff. This gives you the opportunity to redo the commit.


Instead if you want to revise the immediately previous commit, use git commit --amend. This rewrites the previous commit. In your current state with C checked out and alert("Hello adam"); uncommitted, you can git commit -a to rewrite commit C with your new change.

A - B - C1 [master]
     \        
      C

The old C will eventually be garbage collected.

In general, if you want to revise the previous commit, simply make your edits and then git commit --amend them to the previous commit. More involved rewriting of history would involve an "interactive rebase". See Rewriting History in the Pro Git book for more.


Incidentally, git --soft HEAD~1 is a form of "redo". It allows you to undo the previous commit while retaining its changes so you can edit them and commit them anew. Here's some useful aliases to make these commands easier to understand.

[alias]
    # Undo the last commit
    undo = reset --hard HEAD^

    # Undo the last commit, but leave its changes in the working copy
    # so we can redit them.
    redo = reset --soft HEAD^

    # Throw out all changes
    clear = reset --hard HEAD
Schwern
  • 153,029
  • 25
  • 195
  • 336
0

--hard discards all changes in the staging area and files. but hard work only when you add a file to stage area and you did not commit yet so to undo a commit you should add the hash

git reset --hard HashOfCommit
adam lolo
  • 53
  • 4
  • Did you commit or push? reset will remove the commit, the type of reset you do will either leave you with tracked changes or not.... – D3TXER Sep 23 '19 at 03:42
0

Basing on your comments to existing answers, you have probably already pushed your unwanted commit to the remote repository. In that case you have to do following:

git reset --hard HEAD~1
git push --force

Push with force option will overwrite state that you have on your remote repository for current branch. Note that you should not use this command on public branches (shared with other developers), because it will most likely lead to conflicts.

BugHunter
  • 119
  • 6