我正在使用git并在master分支上工做。 该分支有一个名为app.js
的文件。 html
我有一个experiment
分支,在其中进行了大量更改和大量提交。 如今,我但愿将仅对app.js
进行的全部更改从experiment
转移到master
分支。 java
我怎么作? git
再一次,我不想合并。 我只想将app.js
全部更改从experiment
分支带到master
分支。 app
一切都简单得多,为此可使用git checkout。 ui
假设you're on master
分支上, app.js from new-feature
分支获取app.js from new-feature
请执行如下操做: spa
git checkout new-feature path/to/app.js // note that there is no leading slash in the path!
这将为您带来所需文件的内容。 您能够像往常同样使用sha1的一部分而不是 新功能的 分支名称来获取文件,就像在特定提交中同样。 rest
git checkout master # first get back to master git checkout experiment -- app.js # then copy the version of app.js # from branch "experiment"
另请参见git如何撤消对一个文件的更改? code
更新2019年8月,Git 2.23:使用新的git switch
和git restore
命令,将是: htm
git switch master git restore -s experiment -- app.js
默认状况下,仅还原工做树。
若是您还想更新索引(意味着还原文件内容, 并经过一个命令将其添加到索引中): 索引
git restore -s experiment --staged --worktree -- app.js # shorter: git restore -s experiment -WS -- app.js
正如雅库布·纳伦斯基(JakubNarębski )在评论中提到的那样:
git show experiment:path/to/app.js > path/to/app.js
除SO问题“ 如何从Git中的特定修订版本检索单个文件? ”中详述的以外,该方法也能够工做,您须要使用来自仓库的根目录的完整路径。
所以,Jakub在他的示例中使用了path / to / app.js。
正如Frosty在评论中提到的那样:
您只会获得app.js的最新状态
可是,对于git checkout
或git show
,您实际上能够引用所需的任何修订版,如SO问题“ git gui中文件的git checkout修订版 ”所示:
$ git show $REVISION:$FILENAME $ git checkout $REVISION -- $FILENAME
$ FILENAME是版本文件的完整路径 ,将是相同的。
$REVISION
能够如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
等等。
您也能够从隐藏处执行此操做:
git checkout stash -- app.js若是您正在两个分支上而且不想提交,这将很是有用。
补充VonC和chhh的答案。
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
要么
git checkout experiment -- app.js
或者,若是您但愿来自另外一个分支的全部文件:
git checkout <branch name> -- .
git checkout branch_name file_name
例:
git checkout master App.java
若是您的分支机构名称中包含句点,则此方法将无效。
git checkout "fix.june" alive.html error: pathspec 'fix.june' did not match any file(s) known to git.