美文网首页
undo last commit: git reset & gi

undo last commit: git reset & gi

作者: Time_Notes | 来源:发表于2021-11-17 20:32 被阅读0次

    https://devconnected.com/how-to-undo-last-git-commit/#:~:text=Commit%20with%20revert-,Undo%20Last%20Git%20Commit%20with%20reset,removed%20from%20your%20Git%20history.

    Undo Last Git Commit with reset

    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. You have to specify the commit to undo which is “HEAD~1” in this case.

    The “git reset” command can be seen as the opposite of the “git add” command, essentially adding files to the Git index.

    When specifying the “–soft” option, Git is instructed not to modify the files in the working directory or in the index at all.

    git reset --soft

    As you can see, by undoing the last commit, the file is still in the index (changes to be committed) but the commit was removed.


    Hard Reset Git commit

    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.

    In some cases, you simply want to get rid of the commit and the changes done to the files.

    This is the purpose of the “–hard” option.

    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”).

    $ git reset --hard HEAD~1

    Be careful when using “–hard” : changes will be removed from the working directory and from the index, you will lose all modifications.

    git reset --hard

    As you can see, the file was completely removed from the Git repository (index + working directory)


    Mixed reset Git 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. Next to this command, simply append “HEAD~1” for the last commit.

    $ git reset --mixed HEAD~1

    As an example, let’s say that we have added a file named “file1” in a commit that we need to undo.

    $ git log --oneline --graph

    * b734307 (HEAD -> master) Added a new file named "file1"

    * 90f8bb1 Second commit

    * 7083e29 Initial repository commit

    To undo the last commit, we simply execute the “git reset” command with the “–mixed” option.

    $ git reset --mixed HEAD~1

    When specifying the “–mixed” option, the file will be removed from the Git index but not from the working directory.

    As a consequence, the “–mixed” is a “mix” between the soft and the hard reset, hence its name.

    $ git status

    On branch master

    Your branch is ahead of 'origin/master' by 1 commit.

      (use "git push" to publish your local commits)

    Untracked files:

      (use "git add <file>..." to include in what will be committed)

            file1

    nothing added to commit but untracked files present (use "git add" to track)

    Great! You found another way to revert the last commit while preserving changes done to files.

    In the next section, we are going to see another way to revert the last commit using the git revert command.


    Undo Last Commit with revert

    In order to revert the last Git commit, use the “git revert” and specify the commit to be reverted which is “HEAD” for the last commit of your history.

    $ git revert HEAD

    The “git revert” command is slightly different from the “git reset” command because it will record a new commit with the changes introducted by reverting the last commit.

    Note also that with “git reset” you specified “HEAD~1” because the reset command sets a new HEAD position while reverting actually reverts the commit specified.

    As a consequence, you will have to commit the changes again for the files to be reverted and for the commit to be undone.

    As a consequence, let’s say that you have committed a new file to your Git repository but you want to revert this commit.

    git revert

    相关文章

      网友评论

          本文标题:undo last commit: git reset & gi

          本文链接:https://www.haomeiwen.com/subject/bgkrtrtx.html