Can I do a partial revert in GIT
Solution 1:
You can revert the commit without creating a new one by adding the '--no-commit' option. This leaves all the reverted files in the staging area. From there, I'd perform a soft reset and add in the changes I really wanted. For an example workflow:
git revert <sha-of-bad-commit> --no-commit
git reset // This gets them out of the staging area
<edit bad file to look like it should, if necessary>
git add <bad-file>
git checkout . // This wipes all the undesired reverts still hanging around in the working copy
git commit
Solution 2:
You can interactively apply old version of a file using the checkout
command.
For example, if you know the COMMIT
where the code to add back was removed, you can run the following command:
git checkout -p COMMIT^ -- FILE_TO_REVERT
Git will prompt you to add back the hunks that are missing from the current version of the file. You can use e
to create a patch of the change before applying it back.
Solution 3:
You can just manually check out the old, good contents of the files you want to revert using git checkout
. For instance, if you want to revert my-important-file
to the version it was in the version abc123
, you can do
git checkout abc123 -- my-important-file
Now you have the old contents of my-important-file
back, and can even edit them if you feel like, and commit as usual to make a commit which will revert the changes that he made. If there are only some parts of his commit that you want to revert, use git add -p
to select only a few hunks from the patch that you are committing.