Monday, July 10, 2017

Git Reset to the Certain Commit

Git Reset to the Certain Commit

“git revert”

If I need to delete the change after the last commit, however, it has been committed and pushed to the remote repository. How can I do ?

If you just want to revert to the last commit, the command git revert can recover the change.

git revert HEAD

But in this way, the log would record the revert history in the log as follows:

$ git log
commit 7bcf5e3b6fc47e875ec226ce2b13a53df73cf626
Author: yourname <yourname@yourmail.com>
Date:   Wed Jul 8 15:46:28 2017 +0900

    Revert "a certain change"

    This reverts commit 0d4a808c26908cd5fe4b6294a00150342d1a58be.

commit 0d4a808c26908cd5fe4b6294a00150342d1a58be
Author: yourname <yourname@yourmail.com>
Date:   Mon Jul 6 23:19:26 2017 +0900

    a certain change

Back to a certain commit

If I need to move back to a certain commit ( for example 4 commits ), I can do as follows:

First, git reset to the commit with its SHA value. It would move back to that commit. For example:

git reset a8b5a0afea1e1f5faccda4a698c0002bdcc7bf892

The commit is back to that point, but not the content. The commandgit status would let yo know the files changed during this period of time.

And then:

git checkout -f

The contents will be moved back to the status of that commit.

Finally,

git reset origin/master

Point the commit to the latest position and push. The remote repository will move back to that commit and the change history (commit log) remains.

No comments:

Post a Comment