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?
This question is related to
git
git-branch
branching-and-merging
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 it is better than other options?
git checkout otherbranch -- path/to/myfile.txt
- It copy file to working directory but also to staging area (similar effect as if you would copy this file manually and executed git add
on it). git restore
doesn't touch staging area (unless told it to by --staged
option).
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 enconding or you could get broken file if it's binary. With git restore
changing files is done all by git
executable.
Another advantage is that you can restore whole folder with:
git restore --source otherbranch path/to
or with git restore --overlay --source otherbranch path/to
if you want to avoid deleting files. For example if there is less files on otherbranch
than in current working directory (and these files are tracked) without --overlay
option git restore
will delete them. But this is good default bahaviour, 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"
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
here sub_branch
means where you have that file followed by filename you need to copy.
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 ^_^
What about using checkout command :
git diff --stat "$branch"
git checkout --merge "$branch" "$file"
git diff --stat "$branch"
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
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
here sub_branch
means where you have that file followed by filename you need to copy.
Please note that in the accepted answer, the first option stages the entire file from the other branch (like git add ...
had been performed), and that the second option just results in copying the file, but doesn't stage the changes (as if you had just edited the file manually and had outstanding differences).
Git copy file from another branch without staging it
Changes staged (e.g. git add filename)
:
$ git checkout directory/somefile.php feature-B
$ git status
On branch feature-A
Your branch is up-to-date with 'origin/feature-A'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: directory/somefile.php
Changes outstanding (not staged or committed):
$ git show feature-B:directory/somefile.php > directory/somefile.php
$ git status
On branch feature-A
Your branch is up-to-date with 'origin/feature-A'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: directory/somefile.php
no changes added to commit (use "git add" and/or "git commit -a")
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 ^_^
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
Please note that in the accepted answer, the first option stages the entire file from the other branch (like git add ...
had been performed), and that the second option just results in copying the file, but doesn't stage the changes (as if you had just edited the file manually and had outstanding differences).
Git copy file from another branch without staging it
Changes staged (e.g. git add filename)
:
$ git checkout directory/somefile.php feature-B
$ git status
On branch feature-A
Your branch is up-to-date with 'origin/feature-A'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: directory/somefile.php
Changes outstanding (not staged or committed):
$ git show feature-B:directory/somefile.php > directory/somefile.php
$ git status
On branch feature-A
Your branch is up-to-date with 'origin/feature-A'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: directory/somefile.php
no changes added to commit (use "git add" and/or "git commit -a")
I would use git restore
(available since git 2.23)
git restore --source otherbranch path/to/myfile.txt
Why it is better than other options?
git checkout otherbranch -- path/to/myfile.txt
- It copy file to working directory but also to staging area (similar effect as if you would copy this file manually and executed git add
on it). git restore
doesn't touch staging area (unless told it to by --staged
option).
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 enconding or you could get broken file if it's binary. With git restore
changing files is done all by git
executable.
Another advantage is that you can restore whole folder with:
git restore --source otherbranch path/to
or with git restore --overlay --source otherbranch path/to
if you want to avoid deleting files. For example if there is less files on otherbranch
than in current working directory (and these files are tracked) without --overlay
option git restore
will delete them. But this is good default bahaviour, 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"
What about using checkout command :
git diff --stat "$branch"
git checkout --merge "$branch" "$file"
git diff --stat "$branch"
Source: Stackoverflow.com