0

Say I commit 5 files with changes. Then I realize that I want to revert so that I can get rid of the changes in 2 of them, but I want to keep the changes in the other 3. Is there a reasonable way to do this? Does that change if I've pushed?

Thanks.

Jesse Liberty
  • 1,314
  • 1
  • 11
  • 26
  • 2
    Does this answer your question? [Git revert just some files in commit](https://stackoverflow.com/questions/36201607/git-revert-just-some-files-in-commit) – mkrieger1 Aug 17 '22 at 19:43
  • Regarding your last question, "Does that change if I've pushed?" It depends on if it's a shared branch that you pushed. If you're the only one working on your branch and force pushing your own branches is fine, then I'd rewrite your branch and force push. If not, then I'd do a partial revert (make a new commit that undoes the changes you didn't intend to make). And of course if you didn't push yet, amend or rewrite your local branch instead of a partial revert. – TTT Aug 17 '22 at 21:35

1 Answers1

1

You can use git checkout to check out a previous version of the file using a "good" commit ID:

git checkout good-commit-sha -- path/to/file1
git checkout good-commit-sha -- path/to/file2
git commit
mipadi
  • 398,885
  • 90
  • 523
  • 479