JavaEar 专注于收集分享传播有价值的技术资料

如何撤消Git中的最后提交? (How to undo the last commits in Git?)



I committed by accident the wrong files into Git, but I haven't pushed the commit to the server yet.

How can I undo those commits?


我偶然将错误的文件提交到 Git ,但我还没有将提交推送到服务器


I committed by accident the wrong files into Git, but I haven't pushed the commit to the server yet.

How can I undo those commits?

我偶然将错误的文件提交到 Git ,但我还没有将提交推送到服务器



  1. Undo last commit:-

    git reset --soft HEAD^ or git reset --soft HEAD~

    This will undo the last commit.

    Here --soft means reset into staging.

    HEAD~ or HEAD^ means to move to commit before HEAD.

    Replace last commit to new commit:

    git commit --amend -m "message"

    It will replace the last commit with the new commit.

  2. 参考答案2
  3. Use SourceTree (graphical tool for Git) to see your commits and tree. You can manually reset it directly by right clicking it.

  4. 参考答案3
  5. Undoing a commit is a little scary if you don't know how it works. But it's actually amazingly easy if you do understand.

    Say you have this, where C is your HEAD and (F) is the state of your files.


    You want to nuke commit C and never see it again. You do this:

    git reset --hard HEAD~1

    The result is:


    Now B is the HEAD. Because you used --hard, your files are reset to their state at commit B.

    Ah, but suppose commit C wasn't a disaster, but just a bit off. You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Starting again from here, with C as your HEAD:


    You can do this, leaving off the --hard:

    git reset HEAD~1

    In this case the result is:


    In both cases, HEAD is just a pointer to the latest commit. When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. But (unless you use --hard) you leave your files as they were. So now git status shows the changes you had checked into C. You haven't lost a thing!

    For the lightest touch, you can even undo your commit but leave your files and your index:

    git reset --soft HEAD~1

    This not only leaves your files alone, it even leaves your index alone. When you do git status, you'll see that the same files are in the index as before. In fact, right after this command, you could do git commit and you'd be redoing the same commit you just had.

    One more thing: Suppose you destroy a commit as in the first example, but then discover you needed it after all? Tough luck, right?

    Nope, there's still a way to get it back. Type git reflog and you'll see a list of (partial) commit shas that you've moved around in. Find the commit you destroyed, and do this:

    git checkout -b someNewBranchName shaYouDestroyed

    You've now resurrected that commit. Commits don't actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn't mean to get rid of.

  6. 参考答案4
  7. $ git reset --soft HEAD~1  

    Reset will rewind your current HEAD branch to the specified revision. In our example above, we'd like to return to the one before the current revision - effectively making our last commit undone.
    Note the --soft flag: this makes sure that the changes in undone revisions are preserved. After running the command, you'll find the changes as uncommitted local modifications in your working copy.

    $ git reset --hard HEAD~1  

    If you don't want to keep these changes, simply use the --hard flag. Be sure to only do this when you're sure you don't need these changes anymore.

  8. 参考答案5
  9. Simple, run this in your command line:

    git reset --soft HEAD~ 
  10. 参考答案6
  11. To reset to the previous revision, permanently deleting all uncommitted changes:

    git reset --hard HEAD~1
  12. 参考答案7
  13. enter image description here

    Assuming you're working in Visual Studio, if you go in to you branch history and look at all of your commits, simply select the event prior to the commit you want to undo, right-click it, and select Revert. Easy as that.

  14. 参考答案8
  15. 参考答案9
  16. git rm yourfiles/*.class
    git commit -a -m "deleted all class files in folder 'yourfiles'"


    git reset --hard HEAD~1

    Warning: The above command will permanently remove the modifications to the .java files (and any other files) that you wanted to commit.

    The hard reset to HEAD-1 will set your working copy to the state of the commit before your wrong commit.

  17. 参考答案10
  18. You can undo your git commits in two ways:- First is you can use git revert, if you want to keep your commit history.

    git revert HEAD~3
    git revert <hashcode of commit>

    Second is you can use git reset, which would delete all your commit history and bring your head to commit where you want it.

    git reset <hashcode of commit>
    git reset HEAD~3

    You can also use --hard keyword if any of it start behaving otherwise. But , I would recommend until it's extremely necessary.

  19. 参考答案11
  20. There are many ways to do it:

    Git command to undo the last commit/ previous commits:

    Basic command to revert the commit in Git is:

    $ git reset --hard <COMMIT -ID>


    $ git reset --hard HEAD~<n>

    COMMIT-ID: ID for the commit

    n: is number of last commits you want to revert

    You can get the commit id as shown below:

    $ **git log --oneline**
    d81d3f1 function to subtract two numbers
    be20eb8 function to add two numbers
    bedgfgg function to mulitply two numbers

    where d81d3f1 and be20eb8 are commit id.

    Now let's see some cases:

    Suppose you want to revert the last commit 'd81d3f1'. Below are the ways:

    $ git reset --hard d81d3f1


    $ git reset --hard HEAD~1

    Suppose you want to revert the commit 'be20eb8'. Below are the way:

    $ git reset --hard be20eb8

    For more detailed information you can refer and try out some other commands too for resetting head to a specified state:

    $ git reset --help
  21. 参考答案12
  22. For a local commit

    git reset --soft HEAD~1

    or if you do not remember exactly in which commit it is, you might use

    git rm --cached <file>

    For a pushed commit

    The proper way of removing files from the repository history is using git filter-branch. That is,

    git filter-branch --index-filter 'git rm --cached <file>' HEAD

    But I recomnend you use this command with care. Read more at git-filter-branch(1) Manual Page.

  23. 参考答案13
  24. If you want remove files in the last commit, you can use this:

    git reset --hard HEAD~1

    And if need files in the last commit, you can use this:

    git reset --soft HEAD~1
  25. 参考答案14
  26. I have found this site which describes how to undo things that you have committed into the repository.

    Some commands:

    git commit --amend        # Change last commit
    git reset HEAD~ --soft    # Undo last commit
  27. 参考答案15
  28. Find the last commit hash code by seeing the log by :

    git log


    git reset <the previous co>
  29. 参考答案16
  30. In IntelliJ IDEA you can just open Git Repository Log by pressing Alt+9, then right mouse button click at some tag from commits list and select: "Reset Current Branch to Here..."

  31. 参考答案17
  32. For the sake of completeness, I will give the one glaringly obvious method that was overlooked by the previous answers.

    Since the commit was not pushed, the remote was unchanged, so:

    1. Delete the local repo.
    2. Clone the remote repo.

    This is sometimes necessary if your fancy git client goes bye-bye.
    Don't forget to re-commit your saved changes since the last push.

  33. 参考答案18
  34. To change the last commit

    Replace the files in the index:

    git rm --cached *.class
    git add *.java

    Then, if it's a private branch, amend the commit:

    git commit --amend

    Or, if it's a shared branch, make a new commit:

    git commit -m 'Replace .class files with .java files'

    (to change a previous commit, use the awesome interactive rebase)

    ProTip™:   Add *.class to a gitignore to stop this happening again.

    To revert a commit

    Amending a commit is the ideal solution if you need to change the last commit, but a more general solution is reset.

    You can reset git to any commit with:

    git reset @~N

    Where N is the number of commits before HEAD, and @~ resets to the previous commit.

    So, instead of amending the commit, you could use:

    git reset @~
    git add *.java
    git commit -m "Add .java files"

    Check out git help reset, specifically the sections on --soft --mixed and --hard, for a better understanding of what this does.


    If you mess up, you can always use the reflog to find dropped commits:

    $ git reset @~
    $ git reflog
    c4f708b HEAD@{0}: reset: moving to @~
    2c52489 HEAD@{1}: commit: added some .class files
    $ git reset 2c52489
    ... and you're back where you started

  35. 参考答案19
  36. Usually, you want to undo a commit because you made a mistake and you want to fix it - essentially what the OP did when he asked the question. So really, you actually want to redo a commit.

    Most of the answers here focus on the command line. While the command line is the best way to use Git when you're comfortable with it, its probably a bit alien to those coming from other version control systems to Git.

    Here's how to do it using a GUI. If you have Git installed, you already have everything you need to follow these instructions.

    NOTE: I will assume here that you realised the commit was wrong before you pushed it. If you don't know what pushing is, you probably haven't pushed, so carry on with the instructions. If you have pushed the faulty commit, the least risky way is just to follow up the faulty commit with a new commit that fixes things, the way you would do it in a version control system that does not allow you to rewrite history.

    That said, here's how to fix your most recent fault commit using a GUI:

    1. Navigate to your repository on the command line and start the gui with git gui
    2. Choose "Amend last commit". You will see your last commit message, the files you staged and the files you didn't.
    3. Now change things to how you want them to look and click Commit.
  37. 参考答案20
  38. step 1) get git commit-id by :- git log

    step 2) use git reset:- git reset --hard commit-id

    step 3) in case you already pushed your changes to the remote you can use git push the reverted change to the remote:- git push origin branch-name -f

  39. 参考答案21
  40. You can use:

    git reset HEAD@{1}

    This command will delete your wrong commit without a Git log.

  41. 参考答案22
  42. Use git revert commit-id

    To get the commit ID, just use git log

  43. 参考答案23
  44. WHAT TO USE, reset --soft or reset --hard?

    I am just adding two cents for @Kyralessa's answer:

    If you are unsure what to use go for --soft (I used this convention to remember it --soft for safe).

    Why ?

    If you choose --hard by mistake you will LOSE your changes as it wasn't before. If you choose --soft by mistake you can achieve the same results of --hard by applying additional commands

    git reset HEAD file.html
    git checkout -- file.html

    Full example

    echo "some changes..." > file.html
    git add file.html
    git commit -m "wrong commit"
    # I need to reset
    git reset --hard HEAD~1 (cancel changes)
    # OR
    git reset --soft HEAD~1 # Back to staging
    git reset HEAD file.html # back to working directory
    git checkout -- file.html # cancel changes

    Credits goes to @Kyralessa.

  45. 参考答案24
  46. Just undo the last commit:

    git reset --soft HEAD~

    Or undo the time before last time commit:

    git reset --soft HEAD~2

    Or 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

    More details can be found in the docs: git-reset

  47. 参考答案25
  48. If you are working with SourceTree, this will help you.

    Right click on the commit then select "Reset (current branch)/master to this commit" and last select "Soft" reset.

    Enter image description here

  49. 参考答案26
  50. To undo your local commit you use git reset <file>. Also that tutorial is very helpful to show you how it works.

    Alternatively, you can use git revert <commit>: reverting should be used when you want to add another commit that rolls back the changes (but keeps them in the project history).

  51. 参考答案27
  52. If you are planning undoing a local commit entirely, whatever you changes you did on the commit, and if you don't worry anything about that, just do the following command.

    git reset --hard HEAD^1

    (This command will ignore your entire commit and your changes will be lost completely from your local working tree). If you want to undo your commit, but you want your changes in the staging area (before commit just like after git add) then do the following command.

    git reset --soft HEAD^1

    Now your committed files comes into the staging area. Suppose if you want to unstage the files, because you need to edit some wrong conent, then do the following command

    git reset HEAD

    Now committed files come from the staged area into the unstaged area. Now files are ready to edit, so whatever you changes, you want go edit and added it and make a fresh/new commit.


  53. 参考答案28
  54. If you have Git Extras installed, you can run git undo to undo the latest commit. git undo 3 will undo the last 3 commits.

  55. 参考答案29
  56. A Typical Git Cycle

    In speaking of Git-related commands in the previous answers, I would like to share my typical Git cycles with all readers which may helpful. Here is how I work with Git,

    1. Cloning the first time from the remote server

      git clone $project

    2. Pulling from remote (when I don't have a pending local commit to push)

      git pull

    3. Adding a new local file1 into $to_be_committed_list (just imagine $to_be_committed_list means staged area)

      git add $file1

    4. Removing mistakenly added file2 from $to_be_committed_list (assume that file2 is added like step 3, which I didn't want)

      git reset $file2

    5. Committing file1 which is in $to_be_committed_list

      git commit -m "commit message description"

    6. Syncing local commit with remote repository before pushing

      git pull --rebase

    7. Resolving when conflict occurs prerequisite configure mergetool

      git mergetool #resolve merging here, also can manually merge

    8. Adding conflict-resolved files, let's say file1:

      git add $file1

    9. Continuing my previous rebase command

      git rebase --continue

    10. Pushing ready and already synced last local commit

      git push origin head:refs/for/$branch # branch = master, dev, etc.

  57. 参考答案30
  58. I wanted to undo the lastest 5 commits in our shared repository. I looked up the revision id that I wanted to rollback to. Then I typed in the following.

    prompt> git reset --hard 5a7404742c85
    HEAD is now at 5a74047 Added one more page to catalogue
    prompt> git push origin master --force
    Total 0 (delta 0), reused 0 (delta 0)
    remote: bb/acl: neoneye is allowed. accepted payload.
     + 09a6480...5a74047 master -> master (forced update)
  59. 参考答案31
  60. I prefer to use git rebase -i for this job, because a nice list pops up where I can choose the commits to get rid of. It might not be as direct as some other answers here, but it just feels right.

    Choose how many commits you want to list, then invoke like this (to enlist last three)

    git rebase -i HEAD~3

    Sample list

    pick aa28ba7 Sanity check for RtmpSrv port
    pick c26c541 RtmpSrv version option
    pick 58d6909 Better URL decoding support

    Then git will remove commits for any line that you remove.

  61. 参考答案32
  62. VISUAL STUDIO USERS (2015, etc.)

    If you cannot synchronise in Visual Studio as you are not allowed to push to a branch like "development" then as much as I tried, in Visual Studio NEITHER the REVERT NOR the RESET (hard or soft) would work.

    Per the answer with TONS OF VOTES:

    Use this at the command prompt of root of your project to nuke anything that will attempt to get pushed:

    git reset --hard HEAD~1

    Backup or zip your files just in case you don't wish to lose any work, etc...

  63. 参考答案33
  64. How to fix the previous local commit

    Use git-gui (or similar) to perform a git commit --amend. From the GUI you can add or remove individual files from the commit. You can also modify the commit message.

    How to undo the previous local commit

    Just reset your branch to the previous location (for example, using gitk or git rebase). Then reapply your changes from a saved copy. After garbage collection in your local repository, it will be like the unwanted commit never happened. To do all of that in a single command, use git reset HEAD~1.

    Word of warning: Careless use of git reset is a good way to get your working copy into a confusing state. I recommend that Git novices avoid this if they can.

    How to undo a public commit

    Perform a reverse cherry pick (git-revert) to undo the changes.

    If you haven't yet pulled other changes onto your branch, you can simply do...

    git revert --no-edit HEAD

    Then push your updated branch to the shared repository.

  65. 参考答案34
  66. You need to do the easy and fast

        git commit --amend

    if it's a private branch or

        git commit -m 'Replace .class files with .java files'

    if it's a shared or public branch.

  67. 参考答案35
  68. git reset --hard your_last_good_commit_hash_here
    git push --force origin master
  69. 参考答案36
  70. HEAD:

    Before reset commit we should know about HEAD... HEAD is nothing but your current state in your working directory.. it is represented by commit number...

    git commmit:

    Each changes assigned under commit which is represented by unique tag.. Commits can't be deleted. so if you want your last commit you can simply dive into it using git reset

    you can dive into last commit using 2 method,

    Method 1: ( if you dont know commit number but want to move onto very first)

    git reset HEAD~1  #it will move your head to last commit

    Method 2: ( if you know commit you simply reset onto your known commit)

    git reset 0xab3 # commit number

    Note: if you want to know recent commit try this, git log -p -1

    here is the graphical representation,

    enter image description here

  71. 参考答案37
  72. Think we have code.txt file. We make some changes on it and commit. We can undo this commit in three ways, but first you should know what is the staged file... An staged file is a file that ready to commit and if you run git status this file will be shown with green color and if this is not staged for commit will be shown with red color:

    enter image description here

    It means if you commit your change, your changes on this file is not saved. You can add this file in your stage with git add code.txt and then commit your change:

    enter image description here

    Undo last commit:

    1. Now if we want just undo commit without any other changes, we can use

      git reset --soft HEAD^

      enter image description here

    2. If we want undo commit and its changes (THIS IS DANGEROUS, because your change will lost), we can use

      git reset --hard HEAD^

      enter image description here

    3. And if we want undo commit and remove changes from stage, we can use

      git reset --mixed HEAD^ or in a short form git reset HEAD^

      enter image description here

  73. 参考答案38
  74. I got the commit ID from bitbucket and then did:

    git checkout commitID .


    git checkout 7991072 .

    And it reverted it back up to that working copy of that commit.

  75. 参考答案39
  76. If you have committed junk but not pushed,

    git reset --soft HEAD~1

    HEAD~1 is a shorthand for the commit before head. Alternatively you can refer to the SHA-1 of the hash if you want to reset to. --soft option will delete the commit but it will leave all your changed files "Changes to be committed", as git status would put it.

    If you want to get rid of any changes to tracked files in the working tree since the commit before head use "--hard" instead.


    If you already pushed and someone pulled which is usually my case, you can't use git reset. You can however do a git revert,

    git revert HEAD

    This will create a new commit that reverses everything introduced by the accidental commit.

  77. 参考答案40
  78. If you want to permanently undo it and you have cloned some repository

    The commit id can be seen by

    git log 

    Then you can do -

    git reset --hard <commit_id>
    git push origin <branch_name> -f
  79. 参考答案41
  80. Use this command

    git checkout -b old-state 0d1d7fc32
  81. 参考答案42
  82. In my case I committed and pushed to the wrong branch, so what I wanted was to have all my changes back so I can commit them to a new correct branch, so I did this:

    On the same branch that you committed and pushed, if you type "git status" you wont't see anything new because you committed and pushed, now type:

    git reset --soft HEAD~1

    This will get all your changes(files) back in the stage area, now to get them back in the working directory(unstage) you just type:

    git reset FILE

    Where "File" is the file that you want to commit again. Now this FILE should be in the working directory(unstaged) with all the changes that you did. Now you can change to whatever branch that you want and commit the changes in that branch. Hope this helps other people that made the same mistake I did. Of course the initial branch that you committed is still there with all changes, but in my case that was ok, if it is not for you you can look for ways to revert that commit in that branch.

  83. 参考答案43
  84. You have several options to undo your last commit. Here are some of your options summarize into a single answer with code snippets

    First of all you need to figure which are the "wrong" commits which you wish to discard. We will use git reflog to find it.

    git reflog

    You can always use the reflog as well.
    git reflog will display any change which updated the HEAD and checking out the desired reflog entry will set the HEAD back to this commit.

    Every time the HEAD is modified there will be a new entry in the reflog.
    The reflog is similar to unix history command and is kept locally on your machine.

    git reflog
    git checkout HEAD@{...}
    # or
    git checkout <sha-1>

    Using the checkout you can go back to any desired commit and you can create branch or any other options that git checkout will allow you to do.

    enter image description here

    git reset HEAD --hard <commit_id>

    "Move" your head back to the desired commit.
    Git reset will checkout the desired commit content to your staging area and/or to your working directory based upon the value --hard/--soft/--mixed you choose to pick. --hard will update both stage area and working directory with the given content and will "detach" any other commits beyond this point on your local branch.

    If those commits are not part of any other branch they will become "dangle".
    "dangle" content means that there is a un-reachable content in your local repository which is not part of any other branch and can be removed or will be removed by the gc.

    # This will destroy any local modifications.
    # Don't do it if you have uncommitted work you want to keep.
    git reset --hard 0d1d7fc32
    # Alternatively, if there's work to keep:
    git stash
    git reset --hard 0d1d7fc32
    git stash pop
    # This saves the modifications, then reapplies that patch after resetting.
    # You could get merge conflicts, if you've modified things which were
    # changed since the commit you reset to.

    This schema illustrate which command does what.
    As you can see there reset && checkout modify the HEAD.

    enter image description here

  85. 参考答案44
  86. Remove a wrong commit that is already pushed to Github

    git push origin +(previous good commit id):(branch name)

    Please specify the last good commit id you would like to reset back in Github.

    For example. If latest commit id is wrong then specify the previous commit id in above git command with branch name.

    You can get previous commit id using git log

  87. 参考答案45
  88. You can always do a git checkout (SHA code) of the previous version and then commit again with the new code.

  89. 参考答案46
  90. Use this command:

    git checkout -b old-state number_commit
  91. 参考答案47
  92. On SourceTree (GUI for GitHub), you may right-click the commit and do a 'Reverse Commit'. This should undo your changes.

    On the terminal:

    You may alternatively use:

    git revert


    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 your changes.
  93. 参考答案48
  94. A single command:

    git reset --soft 'HEAD^' 

    It works great to undo the last local commit!

  95. 参考答案49
  96. In order to get rid of (all the changes in) last commit, last 2 commits and last n commits:

    git reset --hard HEAD~1
    git reset --hard HEAD~2
    git reset --hard HEAD~n

    And, to get rid of anything after a specific commit:

    git reset --hard <commit sha>


    git reset --hard 0d12345

    1- Be careful, because of "hard" option, it deletes the local changes in your repo as well and reverts to the previous mentioned commit. You should run this if you are sure you messed up in your last commit(s) and would like to go back in time.

    2- Usually 7 letters of "commit sha" is enough, but in bigger projects, you may need up to 12 letters to guarantee be unique. You also can mention the whole 40 letters sha.

    3- The above commands work in Github for Windows as well.

  97. 参考答案50
  98. Suppose you made a wrong commit locally and pushed it to remote repo. You can undo the mess with these two commands.

    First we need to correct our local repo by going back to the commit that we desire:

    git reset --hard <previous good commit id where you want local repo to go>

    Now we forcefully push this good commit on the remote repo by using this command:

    git push --force-with-lease

    The 'with-lease' version of the force option will prevent accidental deletion of new commits you do not know about (i.e. coming from another source since your last pull).

  99. 参考答案51
  100. Use:

    git reset HEAD~1 --soft

    Make changes, add, and commit the changes.

  101. 参考答案52
  102. How to undo the last Git commit?

    To restore everything back to the way it was prior to the last commit, we need to reset to the commit before HEAD.

    1. If you don't want to keep your changes that you made:

      git reset --hard HEAD^
    2. If you want to keep your changes:

      git reset --soft HEAD^

    Now check your git log. It will show that our last commit has been removed.

  103. 参考答案53
  104. Undo a commit and redo

    $ git commit -m "Something terribly misguided"              (1)
    $ git reset HEAD~                                           (2)
    << edit files as necessary >>                               (3)
    $ git add ...                                               (4)
    $ git commit -c ORIG_HEAD                                   (5)
    1. This is what you want to undo
    2. This leaves your working tree (the state of your files on disk) unchanged but undoes the commit and leaves the changes you committed unstaged (so they'll appear as "Changes not staged for commit" in git status, and you'll need to add them again before committing). If you only want to add more changes to the previous commit, or change the commit message1, you could use git reset --soft HEAD~ instead, which is like git reset HEAD~ but leaves your existing changes staged.
    3. Make corrections to working tree files.
    4. git add anything that you want to include in your new commit.
    5. 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.

    1 Note, however, that you don't need to reset to an earlier commit if you just made a mistake in your commit message. The easier option is to git reset (to upstage any changes you've made since) and then git commit --amend, which will open your default commit message editor pre-populated with the last commit message.

    Beware however that if you have added any new changes to the index, using commit --amend will add them to your previous commit.

  105. 参考答案54
  106. You just have to use a single command:

    git reset --soft 'HEAD^' 

    It works to undo the last local commit to your Git repository.

  107. 参考答案55
  108. Add/remove files to get things the way you want:

    git rm classdir
    git add sourcedir

    Then amend the commit:

    git commit --amend

    The previous, erroneous commit will be edited to reflect the new index state - in other words, it'll be like you never made the mistake in the first place.

    Note that you should only do this if you haven't pushed yet. If you have pushed, then you'll just have to commit a fix normally.

  109. 参考答案56
  110. Just reset it doing the command below using git:

    git reset --soft HEAD~1

    Explain: what git reset does, it's basically reset to any commit you'd like to go back to, then if you combine it with --soft key, it will go back, but keep the changes in your file(s), so you get back to the stage which the file was just added, HEAD is the head of the branch and if you combine with ~1 (in this case you also use HEAD^), it will go back only one commit which what you want...

    I create the steps in the image below in more details for you, including all steps that may happens in real situations and committing the code:

    How to undo the last commits in Git?

  111. 参考答案57
  112. Just use git reset --hard <last good SHA> to reset your changes and give new commit. You can also use git checkout -- <bad filename>.

  113. 参考答案58
  114. Reference: How to undo last commit in Git?

    If you have Git Extensions installed you can easily undo/revert any commit (you can download git extensions from here).

    Open Git Extensions, right click on the commit you want to revert then select "Revert commit".

    Git Extensions screen shot

    A popup will be opened (see the screenshot below)

    Revert commit popup

    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.

  115. 参考答案59
  116. This took me a while to figure out, so maybe this will help someone...

    There are two ways to "undo" your last commit, depending on whether or not you have already made your commit public (pushed to your remote repository):

    How to undo a local commit

    Lets say I committed locally, but now want to remove that commit.

    git log
        commit 101: bad commit    # latest commit, this would be called 'HEAD'
        commit 100: good commit   # second to last commit, this is the one we want

    To restore everything back to the way it was prior to the last commit, we need to reset to the commit before HEAD:

    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

    Now git log will show that our last commit has been removed.

    How to undo a public commit

    If you have already made your commits public, you will want to create a new commit which will "revert" the changes you made in your previous commit (current HEAD).

    git revert HEAD

    Your changes will now be reverted and ready for you to commit:

    git commit -m 'restoring the file I removed by accident'
    git log
        commit 102: restoring the file I removed by accident
        commit 101: removing a file we dont need
        commit 100: adding a file that we need

    For more info, check out Git Basics - Undoing Things

  117. 参考答案60
  118. Use reflog to find a correct state

    git reflog

    reflog before REFLOG BEFORE RESET

    Select the correct reflog (f3cb6e2 in my case) and type

    git reset --hard f3cb6e2

    After that the repo HEAD will be reset to that HEADid reset effect LOG AFTER RESET

    Finally the reflog looks like the picture below

    reflog after REFLOG FINAL

  119. 参考答案61
  120. First run:

    git reflog

    It will show you all the possible actions you have performed on your repository, for example, commit, merge, pull, etc.

    Then do:

    git reset --hard ActionIdFromRefLog
  121. 参考答案62
  122. "Reset the working tree to the last commit"

    git reset --hard HEAD^ 

    "Clean unknown files from the working tree"

    git clean    

    see - Git Quick Reference

    NOTE: This command will delete your previous commit, so use with caution! git reset --hard is safer –

  123. 参考答案63
  124. From undo last commit, you mean you want to go back to old commit.

    Go to terminal and Type following commands:

    git reset --hard hash-to-reverted
    git push origin HEAD --force

    You need to have the hash for that.

  125. 参考答案64
  126. Another way:

    Checkout the branch you want to revert, then reset your local working copy back to the commit that you want to be the latest one on the remote server (everything after it will go bye-bye). To do this, in SourceTree I right-clicked on the and selected "Reset BRANCHNAME to this commit".

    Then navigate to your repository's local directory and run this command:

    git -c diff.mnemonicprefix=false -c core.quotepath=false push -v -f --tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME

    This will erase all commits after the current one in your local repository but only for that one branch.

  127. 参考答案65
  128. Type git log and find the last commit hash code and then enter:

    git reset <the previous co>
  129. 参考答案66
  130. In my case I accidentally committed some files I did not want to. So I did the following and it worked:

    git reset --soft HEAD^
    git rm --cached [files you do not need]
    git add [files you need]
    git commit -c ORIG_HEAD

    Verify the results with gitk or git log --stat

  131. 参考答案67
  132. There are two main scenarios

    You haven't pushed the commit yet

    If the problem was extra files you commited (and you don't want those on repository), you can remove them using git rm and then commiting with --amend

    git rm <pathToFile>

    You can also remove entire directories with -r, or even combine with other Bash commands

    git rm -r <pathToDirectory>
    git rm $(find -name '*.class')

    After removing the files, you can commit, with --amend option

    git commit --amend -C HEAD # the -C option is to use the same commit message

    This will rewrite your recent local commit removing the extra files, so, these files will never be sent on push and also will be removed from your local .git repository by GC.

    You already pushed the commit

    You can apply the same solution of the other scenario and then doing git push with the -f option, but it is not recommended since it overwrites the remote history with a divergent change (it can mess your repository).

    Instead, you have to do the commit without --amend (remember this about -amend`: That option rewrites the history on the last commit).

  133. 参考答案68
  134. Undo the Last Commit

    There are tons of situations where you really want to undo that last commit into your code. E.g. because you'd like to restructure it extensively - or even discard it altogether!

    In these cases, the "reset" command is your best friend:

    $ git reset --soft HEAD~1

    Above command(Reset) will rewind your current HEAD branch to the specified revision. In our example above, we'd like to return to the one before the current revision - effectively making our last commit undone.

    Note the --soft flag: this makes sure that the changes in undone revisions are preserved. After running the command, you'll find the changes as uncommitted local modifications in your working copy.

    If you don't want to keep these changes, simply use the --hard flag. Be sure to only do this when you're sure you don't need these changes anymore.

    $ git reset --hard HEAD~1

    enter image description here

  135. 参考答案69
  136. As you have committed some stuff in your branch, it's commit id can be found if you type:

    $git log

    Note the previous commit id of your commit and do a soft reset.

    Suppose your commit id is 'C2' and the previous commit id is 'C1' then do $git reset C1

    Nor HEAD is pointing to C1. C2 is now unstaged.

    If you want to delete your changes from the branch along with unstaging, then do $git reset --hard C1

  137. 参考答案70
  138. Visual Studio Code Git options

    I'm using VS Code and there is a handy git integration which let me undo the last commit by a single click.

  139. 参考答案71
  140. I would just add another commit removing the unwanted files. I always squash commits before creating pull requests - so the effect would be the same as if the offending commit had never happened.