1

It is possible to copy a file from one branch to another without the destination being committed, for example:

$ git checkout otherbranch somefile
$ git reset HEAD somefile

However, can the above be done with a single git command?

2
  • Out of curiosity, what is the reason behind the single-command limit? Commented Mar 11, 2019 at 16:33
  • Idle curiosity. Commented Mar 11, 2019 at 19:27

1 Answer 1

2
git cat-file --filters -p otherbranch:somefile > somefile

otherbranch:somefile refers to the blob of somefile on otherbranch.

git cat-file -p prints the content of the blob.

> somefile overwrites somefile in the worktree with the content.

As @jthill commented, --filters should be used in case any filter is configured and somefile is affected. git lfs is one of the use cases.

Sign up to request clarification or add additional context in comments.

2 Comments

Consider the --filters option if you've got lfs or anything that processes the repository content on the way to the worktree , git cat-file --filters -p other:path >path.
@jthill thanks. I've never used filters in practice before. It should be considered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.