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 reinsert my one line change after bringing it over into my branch.
So what's the easiest way in Git to do this?
You can't clone a single file using git. Git is a distributed version control system, the Idea behind its clone functionality is to have a complete copy of project and all versions of files related to that project.
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):
myfile.txt
and mydir
An alternative:
git show commit_id:path/to/file > path/to/file
I would use git restore
(available since Git 2.23):
git restore --source otherbranch path/to/myfile.txt
Why is better than other options?
git restore
modify files only in working directory.git checkout otherbranch -- path/to/myfile.txt
copy file to working directory (your files on disk) but also to staging area. It's similar effect as if you would copy this file manually and executed git add
on it. git restore
by default change only working directory.
To get the same result as for git checkout otherbranch -- path/to/myfile.txt
you can write git restore --source otherbranch --staged --worktree path/to/myfile.txt
git restore
deletes files from working directory when they are missing in other branchgit restore
can be used to restore the whole folder with git restore --source otherbranch path/to/dir
. You can do similar operation with git checkout
but git restore
by default will delete files that are missing on otherbranch
. To get git checkout
behaviour use --overlay
option.
For example, if there are fewer files on otherbranch
than in the current working directory (and these files are tracked) without --overlay
option git restore
will delete them. But this is good default behaviour, you most likely want the state of directory to be "the same like in otherbranch
", not "the same like in otherbranch
but with additional files from my current branch".
To get the same result as for git checkout otherbranch -- path/to/dir
you can write git restore --source otherbranch --staged --worktree --overlay path/to/dir
git restore
doesn't use shell redirection to create file (Powershell only problem)git show otherbranch:path/to/myfile.txt > path/to/myfile.txt
uses standard shell redirection. If you use PowerShell then there might be problem with text encoding or you could get broken file if it's binary. With git restore
changing files is done all by the git
executable.
I ended up at this question on a similar search. In my case I was looking to extract a file from another branch into current working directory that was different from the file's original location. Answer:
git show TREEISH:path/to/file > path/to/local/file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With