乐闻世界logo
搜索文章和话题

How do i undo the most recent local commits in git

5个答案

1
2
3
4
5

In Git, to undo the latest local commit, you can use several different methods depending on the desired outcome. Here are two common scenarios:

  1. git reset (Does not affect the working directory)

If you want to undo the commit while preserving your changes to recommit them, you can use the git reset command. For example, to undo the last commit and keep the changes, you can use:

bash
git reset --soft HEAD~1
  • The --soft option keeps changes in the staging area, allowing you to edit them or directly recommit.
  • HEAD~1 refers to the previous commit on the current branch, which is the commit to be undone.
  1. git reset (Affects the working directory)

If you want to undo the commit and discard all changes, you can use:

bash
git reset --hard HEAD~1
  • The --hard option restores the working directory files to the state of the previous commit, effectively discarding all changes.
  • Similarly, HEAD~1 refers to the previous commit on the current branch.

Important Considerations

Be cautious when using git reset, as the --hard option discards all uncommitted changes. This operation is irreversible, so ensure you don't need to keep these changes before executing.

Example:

Suppose you accidentally committed sensitive data that shouldn't be included. To resolve this, you can use git reset to undo the commit:

bash
# Preserve changes, undo commit git reset --soft HEAD~1 # Or, discard changes, undo commit git reset --hard HEAD~1

After executing the --soft option, inspect and edit the sensitive files to remove the data, then recommit:

bash
# Inspect and edit files # ... # Add changes to staging area git add . # Recommit git commit -m "Remove sensitive data and redo the commit"

This way, the original commit is undone, sensitive data is removed from history, and your desired changes are included in the new commit.

Finally, if these commits have already been pushed to the remote repository, you need to reset the local repository first and then use the --force option with git push to overwrite the remote history. However, this is risky, especially if others have already worked on these commits:

bash
git push origin <branch_name> --force

In this case, it's best to communicate with your team members and ensure they are aware of the changes you're making.

2024年6月29日 12:07 回复

This will add a new commit that deletes the added files.

bash
git rm yourfiles/*.class git commit -a -m "deleted all class files in folder 'yourfiles'"

Alternatively, you can rewrite history to undo the last commit.

Warning: This command will permanently delete modifications to the files you committed (and any other files) with the .java extension, and remove all changes from the working directory:

bash
git reset --hard HEAD~1

git reset --hard HEAD~1 will set your working copy to the state before the previous commit.

2024年6月29日 12:07 回复

Add or remove files to adjust your changes as needed:

bash
git rm classdir git add sourcedir

Then amend the commit:

bash
git commit --amend

The previous erroneous commit will be amended to reflect the new index state—essentially, as if the mistake never occurred.

Please note that this operation should only be performed if the commit has not yet been pushed. If you have already pushed, you simply commit the fix normally.

2024年6月29日 12:07 回复

There are two ways to 'undo' the last commit, depending on whether you have already pushed it (to a remote repository):

How to Reset Local Commits

Suppose I've committed locally and now I want to remove that commit.

git
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 revert to the state before the last commit, we need to reset to the previous commit HEAD:

git
git reset --hard HEAD^ # Use --hard if you don't care about keeping the changes you made``` Now `git log` will show that the last commit has been removed. How to Revert Public Commits If you have already pushed the commit, you will need to create a new commit that 'reverts' the changes you made in the previous commit (current HEAD). ```git revert HEAD``` Your changes are now reverted and ready 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 don't need commit 100: adding a file that we need``` For more information, see [Git Basics - Undoing Things](https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things).
2024年6月29日 12:07 回复

If you don't know how it works, undoing commits can be a bit intimidating. But if you truly understand it, it's actually very straightforward. I'll show you four different ways to undo commits.

Assume you have this, where C is your HEAD, and (F) represents the state of your working directory.

(F) A-B-C ↑ master

Option 1: git reset --hard

You want to discard commit C and all uncommitted changes. You do this:

bash
git reset --hard HEAD~1

Result:

(F) A-B ↑ master

Now B is HEAD. Because you used --hard, your working directory will be reset to the state of commit B.

Option 2: git reset

Perhaps commit C isn't a disaster; it's just a bit off track. You want to undo the commit but keep the changes for some editing and then commit again with improvements. Starting from here with C as HEAD:

(F) A-B-C ↑ master

Do this, omitting --hard:

bash
git reset HEAD~1

In this case, the result is:

(F) A-B-C ↑ master

In both cases, HEAD is simply a pointer to the latest commit. When you run git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. However (unless you use --hard), you'll keep your working directory unchanged. Now git status shows that the changes from commit C are staged. You haven't lost anything!

Option 3: git reset --soft

For the mildest case, you can even undo your commit but keep your working directory and the index:

bash
git reset --soft HEAD~1

This not only doesn't affect your working directory, but also the index. When you do this, git status will show that the files in the index are the same as before. In fact, after running this command, you can execute git commit and redo the exact same commit you just undid.

Option 4: You've done git reset --hard and need to recover the code

One more thing: suppose you destroyed a commit like in the first example, but later realize you actually need it? Bad luck, right?

No, there is a way to recover it. Run this:

bash
git reflog

You'll see a list of the (partial) commit SHA hashes you've moved through (i.e., the hash values). Find the commit you destroyed and run this:

bash
git checkout -b someNewBranchName shaYouDestroyed

You've now recovered the commit. In Git, commits aren't actually destroyed for about 90 days, so you can usually recover commits you didn't intend to delete.

2024年6月29日 12:07 回复

你的答案