


Great! You found another way to revert the last commit while preserving changes done to files. Nothing added to commit but untracked files present (use "git add" to track) When specifying the “–mixed” option, the file will be removed from the Git index but not from the working directory.Īs a consequence, the “–mixed” is a “mix” between the soft and the hard reset, hence its name. To undo the last commit, we simply execute the “git reset” command with the “–mixed” option. $ git reset -mixed HEAD~1Īs an example, let’s say that we have added a file named “file1” in a commit that we need to undo. Next to this command, simply append “HEAD~1” for the last commit. In order to undo the last Git commit, keep changes in the working directory but NOT in the index, you have to use the “git reset” command with the “–mixed” option. Your branch is up to date with origin/masterĪs you can see, the file was completely removed from the Git repository (index + working directory) Mixed reset Git commit Great, let’s now see the state of our Git repository. Now, let’s pretend that you want to undo the last commit and discard all modifications. $ git reset -hard HEAD~1īe careful when using “–hard” : changes will be removed from the working directory and from the index, you will lose all modifications.īack to the example we have detailed before, let’s say that you have committed a new file to your Git repository named “file1”. In order to undo the last commit and discard all changes in the working directory and index, execute the “git reset” command with the “–hard” option and specify the commit before HEAD (“HEAD~1”). This is the purpose of the “–hard” option. In some cases, you simply want to get rid of the commit and the changes done to the files.

In the previous section, we have seen how you can easily undo the last commit by preserving the changes done to the files in the index. " to unstage)Īs you can see, by undoing the last commit, the file is still in the index (changes to be committed) but the commit was removed.Īwesome, you have successfully undone the last Git commit on your repository. (use "git push" to publish your local commits) Your branch is ahead of 'origin/master' by 1 commit. * b734307 (HEAD -> master) Added a new file named "file1"Īs a consequence, you will use “git reset” with the “–soft” option in order to undo the last commit and perform additional modifications. When specifying the “–soft” option, Git is instructed not to modify the files in the working directory or in the index at all.Īs an example, let’s say that you have added two files in your most recent commit but you want to perform some modifications on this file. The “git reset” command can be seen as the opposite of the “git add” command, essentially adding files to the Git index. If you are not familiar with this notation, “HEAD~1” means that you want to reset the HEAD (the last commit) to one commit before in the log history. The last commit will be removed from your Git history. You have to specify the commit to undo which is “HEAD~1” in this case. The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files.
