156

Say I have two branches - master and redesign. How would I go about overwriting the file default.aspx.cs in my redesign branch with the one from master?

I found this question but it seems to go over how to revert a file back to it's previous version in the same branch. I also considered using a merge, but I don't want to merge, I want to overwrite. Suggestions?

flq
  • 22,247
  • 8
  • 55
  • 77
Abe Miessler
  • 82,532
  • 99
  • 305
  • 486

2 Answers2

220

git checkout master path/to/default.aspx.cs

Before doing this, you probably have to : git checkout redesign

So, just git checkout FROM_BRANCH_NAME path/to/file

prayagupa
  • 30,204
  • 14
  • 155
  • 192
Nepomuk Pajonk
  • 2,972
  • 1
  • 19
  • 29
  • 1
    Is there a way to reverse this change with the contents from the original local file? – raychz Sep 21 '17 at 19:53
  • 3
    With `git checkout` you will overwrite local changes without the possibility to revert those changes. AFAIK. You can try `git show :path/to/file` (with the : prefix). But this assumes, you have staged your file before. See [gitrevisions](https://git-scm.com/docs/gitrevisions#gitrevisions-emltrevgtltpathgtemegemHEADREADMEememREADMEememmasterREADMEem). – Nepomuk Pajonk Sep 22 '17 at 06:17
  • 2
    For me `git checkout origin/master ` worked. `master` along didn't work. – Saurav Sahu Dec 03 '20 at 13:32
4

To overwrite a file in a branch from another branch, i.e, master to redesign do (when redesign is the current branch)

git checkout master ./path_to_file/default.aspx.cs

git checkout branch_name ./path_to_file/file_name.[file_extension]

To know more about the command checkout git checkout documentation