Identify the hash of the commit, using git log, then use git revert <commit> to create a new commit that removes these changes. In a way, git revert is the converse of git cherry-pick -- the latter applies the patch to a branch that's missing it, the former removes it from a branch that has it.
Identify the hash of the commit, using git log, then use git revert <commit> to create a new commit that removes these changes. In a way, git revert is the converse of git cherry-pick -- the latter applies the patch to a branch that's missing it, the former removes it from a branch that has it.
I don't like the auto-commit that git revert does, so this might be helpful for some.
If you just want the modified files not the auto-commit, you can use --no-commit
% git revert --no-commit <commit hash>
which is the same as the -n
% git revert -n <commit hash>
Is it possible to "remove" changes done by old commit?
Best way to revert the 3 last commit?
Undoing last commit
How do you revert your code back to your last git commit?
Videos
Say you have this
commit A
commit B
commit C
commit D
where commit A is the earliest, and D is the latest commit.
You now realize that you made mistake in commit B. You want to revert the changes made in B but you do not want to revert the changes in commit C and D.
The safest and probably cleanest way to go is to rebase interactively.
git rebase -i HEAD^^
Or,
git rebase -i baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e^
From there you can squash commits, which puts one or more commits together into the previous commit. To completely delete a commit from the history, delete the line from the list.
You can revert a commit with git revert but its going to add more commit messages to the history, which may be undesirable. Use the -n parameter to tell Git not to commit the revert right away. You can rebase interactively and squash those on up to a previous commmit to keep things clean.
If the two commits you're working with here affect the same file(s), you may see a merge conflict.
Resetting the repository with git reset --hard should be done with care, as it cannot be undone.
Rewriting history should be done with care.
This if from http://nakkaya.com/2009/09/24/git-delete-last-commit/ and it worked for me
Git Delete Last Commit
Once in a while late at night when I ran out of coffee, I commit stuff that I shouldn't have. Then I spend the next 10 - 15 minutes googling how to remove the last commit I made. So after third time I wanted to make a record of it so I can refer to it later.
If you have committed junk but not pushed,
git reset --hard HEAD~1HEAD~1 is a shorthand for the commit before head. Alternatively you can refer to the SHA-1 of the hash you want to reset to. Note that when using --hard any changes to tracked files in the working tree since the commit before head are lost.
If you don't want to wipe out the work you have done, you can use
--softoption that will delete the commit but it will leave all your changed files "Changes to be committed", as git status would put it.Now if you already pushed and someone pulled which is usually my case, you can't use git reset. You can however do a git revert,
git revert HEADThis will create a new commit that reverses everything introduced by the accidental commit.
Git commit only saves it to the stage, which is locally on your computer. Use Push to update it to a remote server (Like github).
Use git revert <ID> to revert back to a previous commit. each commit has an identifying code.
See here for more details on revert
The above answer is not quite correct - git revert <ID> does not set your repository to that commit -- git revert <ID> creates a new commit that undoes the changes introduced by commit <ID>. It's more or less a way to 'undo' a commit and save that undo in your history as a new commit.
If you want to set your branch to the state of a particular commit (as implied by the OP), you can use git reset <commit>, or git reset --hard <commit> The first option only updates the INDEX, leaving files in your working directory unchanged as if you had made the edits but not yet committed them. With the --hard option, it replaces the contents of your working directory with what was on <commit>.
A note of warning that git reset will alter history -- if I made several commits and then reset to the first commit, the subsequent commits will no longer be in the commit history. This can cause some serious headaches if any of those lost commits have been pushed to a public repository. Make sure you only use it to get rid of commits that haven't been pushed to another repository!
This is a theoretical question: If I work on a project where I made changes that seem to be OK and not causing issues, but then, let's say after many other commits and merges with the entire team we realize that the commit I did actually caused some damage that needs to be undone..
for example, I refactored some variable name across the entire project in all occurences and only later realized some were supposed to remain unchanged.
Is it possible to "remove" this commit from the project while keeping all the other merges? Assuming they do not conflict or have anything to do with this change