merge cherry - How do I copy a version of a single file from one git branch to another?
history with (5)
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?
Following madlep's answer you can also just copy one directory from another branch with the directory blob.
git checkout other-branch app/**
As to the op's question if you've only changed one file in there this will work fine ^_^
Run this from the branch where you want the file to end up:
git checkout otherbranch myfile.txt
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
- Wildcards don't work, but relative paths do
- Multiple paths can be specified
git show commit_id:path/to/file > path/to/file
1) Ensure you're in branch where you need a copy of the file.
for eg: i want sub branch file in master so you need to checkout or should be in master
git checkout master
2) Now checkout specific file alone you want from sub branch into master,
git checkout sub_branch file_path/my_file.ext
sub_branch means where you have that file followed by filename you need to copy.
What about using checkout command :
git diff --stat "$branch" git checkout --merge "$branch" "$file" git diff --stat "$branch"
You can easily switch to a branch without using the fancy "git checkout -b somebranch origin/somebranch" syntax. You can just do:
git checkout somebranch
Git will automatically do the right thing:
$ git checkout somebranch Branch somebranch set up to track remote branch somebranch from origin. Switched to a new branch 'somebranch'
Git will check whether a branch with the same name exists in exactly one remote, and if it does, it tracks it the same way as if you had explicitly specified that it's a remote branch. From the git-checkout man page of Git 184.108.40.206:
If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to
$ git checkout -b <branch> --track <remote>/<branch>