If you have already pushed your commits to the origin, then the other solutions described here (using git reset or git checkout and then git commit ) are your only reasonable option.
However , if you have not yet pushed your changes, and you want to delete all traces that have ever been committed, interactive permutation is a good option. Use the following command to restore the last two commits:
git rebase --interactive HEAD~2
An editor opens with your two commits. You want to edit the file to show the following, and then save; in particular, you want to change pick before the first commit to edit :
edit b743075 Commit 1 pick 791962d Commit 2
Git will then put us in a state in which we can edit the first commit (without touching the second). It says that you can change the commit with git commit --amend , but we really want to reset to commit before the voice and undo it completely:
git reset HEAD^
This will push the changes from Commit 1 back to your working tree. Then git add only the files you want to save and overwrite them with git commit :
git add dir1/file1.cpp git add dir1/file1.h git commit -m "Commit 1"
Finally, do a hard reset to delete the files that you do not want to save from your working tree, and complete rebase:
git reset --hard git rebase --continue
When the overflow is complete, your repository will have both commits and traces of file2.* Files.
Blair holloway
source share