问题
I am using git and working on master branch. This branch has a file called app.js
.
I have an experiment
branch in which I made a bunch of changes and tons of commits. Now I want to bring all the changes done only to app.js
from experiment
to master
branch.
How do I do that?
Once again I do not want a merge. I just want to bring all the changes in app.js
from experiment
branch to master
branch.
回答1:
git checkout master # first get back to master
git checkout experiment -- app.js # then copy the version of app.js
# from branch "experiment"
See also git how to undo changes of one file?
Update August 2019, Git 2.23: with the new git switch and git restore commands, that would be:
git switch master
git restore -s experiment -- app.js
By default, only the working tree is restored.
If you want to update the index as well (meaning restore the file content, and add it to the index in one command):
git restore -s experiment --staged --worktree -- app.js
# shorter:
git restore -s experiment -WS -- app.js
As Jakub Narębski mentions in the comments:
git show experiment:path/to/app.js > path/to/app.js
works too, except that, as detailed in the SO question "How to retrieve a single file from specific revision in Git?", you need to use the full path from the root directory of the repo.
Hence the path/to/app.js used by Jakub in his example.
As Frosty mentions in the comment:
you will only get the most recent state of app.js
But, for git checkout
or git show
, you can actually reference any revision you want, as illustrated in the SO question "git checkout revision of a file in git gui":
$ git show $REVISION:$FILENAME
$ git checkout $REVISION -- $FILENAME
would be the same is $FILENAME is a full path of a versioned file.
$REVISION
can be as shown in git rev-parse:
experiment@{yesterday}:app.js # app.js as it was yesterday
experiment^:app.js # app.js on the first commit parent
experiment@{2}:app.js # app.js two commits ago
and so on.
schmijos adds in the comments:
you also can do this from a stash:
git checkout stash -- app.js
This is very useful if you're working on two branches and don't want to commit.
回答2:
Everything is much simpler, use git checkout for that.
Suppose you're on master
branch, to get app.js from new-feature
branch do:
git checkout new-feature path/to/app.js
// note that there is no leading slash in the path!
This will bring you the contents of the desired file. You can, as always, use part of sha1 instead of new-feature branch name to get the file as it was in that particular commit.
回答3:
Supplemental to VonC's and chhh's answers.
git show experiment:path/to/relative/app.js > app.js
# If your current working directory is relative than just use
git show experiment:app.js > app.js
or
git checkout experiment -- app.js
回答4:
git checkout branch_name file_name
Example:
git checkout master App.java
This will not work if your branch name has a period in it.
git checkout "fix.june" alive.html
error: pathspec 'fix.june' did not match any file(s) known to git.
回答5:
Or if you want all the files from another branch:
git checkout <branch name> -- .
回答6:
Review the file on github and pull it from there
This is a pragmatic approach which doesn't directly answer the OP, but some have found useful:
If the branch in question is on GitHub, then you can navigate to the desired branch and file using any of the many tools that GitHub offers, then click 'Raw' to view the plain text, and (optionally) copy and paste the text as desired.
I like this approach because it lets you look at the remote file in its entirety before pulling it to your local machine.
回答7:
If you want the file from a particular commit (any branch) , say 06f8251f
git checkout 06f8251f path_to_file
for example , in windows:
git checkout 06f8251f C:\A\B\C\D\file.h
回答8:
git checkout master -go to the master branch first
git checkout <your-branch> -- <your-file> --copy your file data from your branch.
git show <your-branch>:path/to/<your-file>
Hope this will help you. Please let me know If you have any query.
来源:https://stackoverflow.com/questions/2364147/how-to-get-just-one-file-from-another-branch