How to Undo Recent Commits in Git
Git has a lot of greatest advantages and one of them is considered undoing recent commits. Find several ways of undoing changes and get a copy of the codes.
One of the greatest advantages of Git is that you can undo almost anything. Let’s have a look at how we can undo recent commits in Git.
How to Undo Commits with git reset
The git reset command is used to undo changes.
undo changes git
git reset --soft HEAD~xPut the corresponding number instead of <kbd class="highlighted">~x</kbd>. For example, if you specify <kbd class="highlighted">~3</kbd>, the command will affect the three latest commits.
When you use this command you just undo the last commits, but the changes you have made will be stored in your working tree, and on your index, so the git commit command will create a commit with the same changes as the commits you "undo" before. Instead of <kbd class="highlighted">HEAD~x</kbd>, you can use a commit hash.
undo changes with the last commit, create a new commit with undoed changes
git reset --soft <sha1-commit-hash>How to Undo Commits with git revert
The following command reverts the changes of the given commit and creates a new commit with the reverted changes.
undo changes with the last commit, create a new commit with undoed changes
git revert HEAD~xIt's the best method for undoing commits when working with public shared repositories.
How to Undo Commits with git amend
The git commit --amend is used to edit the latest commits. Instead of creating a completely new commit, you can run this command for combining staged changes with the previous commit. To do that, stage your changes and run the following command:
edit the latest commits
git commit --amendThis command will open your Editor and let you change the message. The new changes will be added to the amended commit.
If you want to change the latest commit message, you can run this command without any staged changes::
make changes to the commit message
git commit --amend -m "This is the correct message"If you want to add some changes beside changing the commit message, then you can stage your changes as usual and, then, commit again:
add to this last commit
git add some/changed/file.ext
git commit --amend -m "commit message"If you have pushed your commit before the last amend, then you should force push your changes with <kbd class="highlighted">--force</kbd> (suppose, the name of remote is origin, which is by default):
change pushed commit
git push origin <branch-name> --forceInstead of --force, you can use --force-with-lease, which is a safer option that will not overwrite the work done by other developers.
How to Undo Commits with git checkout
You can use the git checkout command to checkout to any previous commit with its hash or by using the <kbd class="highlighted">HEAD~x</kbd> syntax. The repository will be set in a "detached HEAD" state, and all your new commits will be orphaned when you change branches back to an established branch. "Detached HEAD" warns you that your activity is "detached" from the project’s development. If you have made several commits in the "detached HEAD" state, then, after checking out to another branch, Git will offer you to create a branch to keep those commits.
From the "detached HEAD" state, you can run:
create a new branchand switch to that statecreate a new branchand switch to that state
git checkout -b <new-branch-name>or
creating a new branch
git switch -c <new-branch-name>This will create a new branch named <kbd class="highlighted"> XFI9 </kbd> and switch to that. The branch will be on a new history timeline where your last commits don’t exist.