How do I copy a version of a single file from one git branch to another?

I've got two branches that are fully merged together.

However, after the merge is done, I realise that one file has been messed up by the merge (someone else did an auto-format, gah), and it would just be easier to change to the new version in the other branch, and then re-insert my one line change after bringing it over into my branch.

So what's the easiest way in git to do this?

Answers


Run this from the branch where you want the file to end up:

git checkout otherbranch myfile.txt

General formulas:

git checkout <commit_hash> <relative_path_to_file_or_dir>
git checkout <remote_name>/<branch_name> <file_or_dir>

Some notes (from comments):

  • Using the commit hash you can pull files from any commit
  • This works for files and directories
  • overwrites the file myfile.txt and mydir
  • Wildcards don't work
  • but relative paths do

an alternative:

git show commit_id:path/to/file > path/to/file

Need Your Help

Switching backgrounds, while switching text

javascript css onclick getelementbyid

I was able to make the text loop infinitely and the body color change once:

What architecture to provide a modular BPM solution?

java jsf-2 bpm egovernment

I search for an architecture to implement an egovernment solution with java. I would like the possible solution present the following features: Based on Java (JSF, Spring, JPA) Modular architect...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.