0

I had pushed those 10 commit files in git and pushed them in master branch on github but now I have to revert 2 file of them back to the previous version.As far as I know if I revert back with commit Id all 10 files will get reverted but I don't want that. Can anyone help me out?

Yash
  • 1
  • 1
  • Welcome to SO, Yash. I'm afraid this is 2) not technically a question and 2) it's also not detailed enough for anyone to give a useful answer. Can you elaborate? (before probable downvotes start to rain down) – Romain Valeri Aug 10 '20 at 12:19
  • Possible duplicate of https://stackoverflow.com/questions/215718/how-can-i-reset-or-revert-a-file-to-a-specific-revision – Jona Aug 10 '20 at 12:20
  • Slightly better now, but it's probably a duplicate, see above. Thanks for the quick update. – Romain Valeri Aug 10 '20 at 12:33

1 Answers1

1

you can do:

git revert <your-commit-id>
git reset --mixed HEAD~
git add <files-to-be-reverted>
git commit -m 'revert the two files'
git reset --hard

explanation:

git revert - reverts the whole commit, all the 10 files will be reverted

git reset --mixed HEAD~ - takes the index back to the previous commit but leaves the work tree as is

git add - only add the changes you want before committing

git commit - create a new commit

git reset --hard - clean-up your work tree and index from unwanted changes

Nitsan Avni
  • 753
  • 6
  • 6
  • @matt when you push the new commit, the relevant files will be removed on the remote too – Nitsan Avni Aug 10 '20 at 13:03
  • The remote will refuse to accept the push, and if you force it, you’ll wreck everyone else’s life. – matt Aug 10 '20 at 13:19
  • I see no reason why you'd need to force the push; what you see in my suggestion is only **adding** a new commit, this is not re-writing any shared/public history. – Nitsan Avni Aug 10 '20 at 17:10