Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Reset a modified file to the state in another branch?

Tags:

git

Say I created my own branch from develop branch, and modified fileA a few times in a few commits, now I want to revert just this file to the state in develop (without reverting my other changes), is there an easy way of doing this? (I know I could checkout it out from develop and overwrite it in my branch).

Thanks!

like image 842
hzxu Avatar asked Jun 27 '17 05:06

hzxu


2 Answers

You won't be able to "revert" since that's done on a commit by commit basis (rather than file by file). There are two alternatives though. You can pick which suits you. Let's assume the file in question is quux.c.

  1. Do a git rebase -i from the point where you cut off the develop branch and then manually undo the changes you made to quux.c in each commit since then. Git will rewrite the commits so that it will look like quux.c was never changed sicne develop was cut.
  2. A simpler route is to simply say git show master:quux.c > quux.c. This will overwrite quux.c with the version of the file on master. Then add it and commit it. This will create an extra "revert" commit but it's simpler than the above.

Update

As the other answers correctly indicate, git checkout branch -- file is more user friendly than the git show command I've mentioned here though the effect is the same.

like image 28
Noufal Ibrahim Avatar answered Sep 22 '22 21:09

Noufal Ibrahim


You do

git checkout develop -- <path/to/file>
like image 172
Martin G Avatar answered Sep 23 '22 21:09

Martin G