anaval
anaval

Reputation: 1148

How to revert files to a previous commit but keep other files to latest commit

Assume I have these commits (the letters are the hash number):

-a(initial commit)
-b(implemented feature request affected foo.php and bar.php)
-c(someone else's commit)
-d(someone experimented on foo.php/bar.php and forgot to discard changes)
-e(another commit)
-f(latest commit)

How do I change foo.php and bar.php to what it was during commit "c". So far I've tried git reset c and git reset --hard c but it changes the whole repository instead.

Another method I tried so far is to make another clone of the repo and reset --hard c, then I manually copy pasted foo.php and bar.php to my working copy then I staged and pushed the changes to remote.

Upvotes: 0

Views: 42

Answers (1)

hspandher
hspandher

Reputation: 16763

Just revert the file to that old commit, and commit the changes.

git checkout <old_commit_id> file_path

For instance -

git checkout <hash_of_c> foo.php
git checkout <hash_of_c> bar.php
git add -u
git commit -m "revert foo and boo"

Upvotes: 2

Related Questions