Git: Reset a modified file to the state in another branch? [duplicate]
Asked Answered
I

4

24

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!

Incompressible answered 27/6, 2017 at 5:4 Comment(0)
A
7

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.

Adest answered 27/6, 2017 at 5:12 Comment(2)
Thanks! The second is a lot simpler since the file has been modified quite a few times.Incompressible
@hzxu yes but it has two "downsides" 1. It will introduce a new commit and 2. The earlier commits will have the modified versions of quux.c so if you cut a branch from there, you'll get back that version.Adest
M
61

You do

git checkout develop -- <path/to/file>
Mendes answered 27/6, 2017 at 5:38 Comment(3)
Yes, but be careful that you will loose all the changes you have in your working directory for that file.Christmann
once after this, we need to add and commit this file to get reflected in upstreams. Otherwise the previously committed version will continueBiflagellate
I had to revert newline and spaces changes. I was trying to revert through editor but couldn't. This command did my job!Elisabeth
A
7

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.

Adest answered 27/6, 2017 at 5:12 Comment(2)
Thanks! The second is a lot simpler since the file has been modified quite a few times.Incompressible
@hzxu yes but it has two "downsides" 1. It will introduce a new commit and 2. The earlier commits will have the modified versions of quux.c so if you cut a branch from there, you'll get back that version.Adest
C
2

See these Links:

How to retrieve a single file from specific revision in Git?

How to get just one file from another branch

git show source_branch:path/to/file > file

works well, except that, as detailed in the SO question "How to retrieve a single file from specific revision in Git?", you need to use the full path from the root directory of the repo.

you will only get the most recent state of file But, for git checkout or git show, you can actually reference any revision you want, as illustrated in the SO question "git checkout revision of a file in git gui":

$ git show $REVISION:$FILENAME
$ git checkout $REVISION -- $FILENAME

would be the same is $FILENAME is a full path of a versioned file.

$REVISION can be as shown in git rev-parse:

experiment@{yesterday}:app.js # app.js as it was yesterday 
experiment^:app.js            # app.js on the first commit parent
experiment@{2}:app.js         # app.js two commits ago

and so on.

Casey answered 27/6, 2017 at 5:33 Comment(0)
D
0

Update in 2.23.0+ (Mar 2021)

Instead of the overloaded git checkout, there is now also the (experimental) option to restore files with the more aptly named git restore like this:

git restore --source develop <path/to/file>

Further Reading

Desiccator answered 30/7 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.