Nic*_*ilt 1154 git git-checkout
我正在使用git并在master分支上工作.这个分支有一个名为的文件app.js.
我有一个experiment分支,我做了一堆变更和大量的提交.现在我希望把做只对所有变化app.js从experiment到master分支.
我怎么做?
再一次,我不想合并.我只是想把所有的变化app.js从experiment分支带到master分支.
Von*_*onC 1472
git checkout master # first get back to master
git checkout experiment -- app.js # then copy the version of app.js
# from branch "experiment"
Run Code Online (Sandbox Code Playgroud)
另请参阅git如何撤消一个文件的更改?
正如JakubNarębski在评论中提到的那样:
git switch master
git restore -s experiment -- app.js
Run Code Online (Sandbox Code Playgroud)
也可以工作,除了如SO问题" 如何从Git中的特定修订中检索单个文件? "中详述的那样,您需要使用repo根目录中的完整路径.
因此,Jakub在他的例子中使用了路径/ to/app.js.
正如Frosty在评论中提到的那样:
你只会获得app.js的最新状态
但是,对于git switch或者git restore,您实际上可以引用您想要的任何修订,如SO问题" git gui中文件的git checkout修订版 "所示:
git restore -s experiment --staged --worktree -- app.js
# shorter:
git restore -s experiment -WS -- app.js
Run Code Online (Sandbox Code Playgroud)
将是相同的是$ FILENAME是版本化文件的完整路径.
git checkout可以如下所示git show:
git show experiment:path/to/app.js > path/to/app.js
Run Code Online (Sandbox Code Playgroud)
等等.
你也可以从藏匿处做到这一点:
Run Code Online (Sandbox Code Playgroud)$ git show $REVISION:$FILENAME $ git checkout $REVISION -- $FILENAME如果您正在处理两个分支并且不想提交,那么这非常有用.
Dmi*_*mov 323
一切都更简单,使用git checkout.
假设you're on master分支,得到app.js from new-feature分支做:
git checkout new-feature path/to/app.js
// note that there is no leading slash in the path!
Run Code Online (Sandbox Code Playgroud)
这将为您带来所需文件的内容.您可以像往常一样使用sha1的一部分而不是 新功能 分支名来获取该特定提交中的文件.
Ale*_*sen 35
补充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
Run Code Online (Sandbox Code Playgroud)
要么
git checkout experiment -- app.js
Run Code Online (Sandbox Code Playgroud)
小智 30
git checkout branch_name file_name
Run Code Online (Sandbox Code Playgroud)
例:
git checkout master App.java
Run Code Online (Sandbox Code Playgroud)
Gab*_*les 13
git# check out all files in <paths> from branch <branch_name>
git checkout <branch_name> -- <paths>
Run Code Online (Sandbox Code Playgroud)
来源:http : //nicolasgallagher.com/git-checkout-specific-files-from-another-branch/。
另见man git checkout。
例子:
# Check out "somefile.c" from branch `my_branch`
git checkout my_branch -- somefile.c
# Check out these 4 files from `my_branch`
git checkout my_branch -- file1.h file1.cpp mydir/file2.h mydir/file2.cpp
# Check out ALL files from my_branch which are in
# directory "path/to/dir"
git checkout my_branch -- path/to/dir
Run Code Online (Sandbox Code Playgroud)
如果您没有指定,branch_name它会自动假定为HEAD,这是您对当前检出分支的最新提交。所以,你也可以这样做来检查“somefile.c”并让它覆盖任何本地的、未提交的更改:
# Check out "somefile.c" from `HEAD`, to overwrite any local, uncommitted
# changes
git checkout -- somefile.c
# Or check out a whole folder from `HEAD`:
git checkout -- some_directory
Run Code Online (Sandbox Code Playgroud)
# General form
git show my_branch_or_commit_hash:my_file.cpp > any/path/my_file.cpp
# Example: check out `main.cpp` from 3 commits ago in your currently-checked-out
# branch (3 commits prior to `HEAD`, or `HEAD~3`) into a temporary directory
mkdir ../temp
git show HEAD~3:main.cpp > ../temp/main_old.cpp
Run Code Online (Sandbox Code Playgroud)
我学到的来源:@Jakub Nar?bski's answer to: git-checkout old revision of a file at a new name
git merge,git cherry-pick,git rebase,或git revert改变?那么,在这种情况下,你最好这样做:
# Keep `--theirs` for all conflicts within this file
git checkout --theirs -- path/to/some/file
# OR: keep `--ours` for all conflicts within this file
git checkout --ours -- path/to/some/file
Run Code Online (Sandbox Code Playgroud)
或者:
# Keep `--theirs` for all conflicts within files inside this dir
git checkout --theirs -- path/to/some/dir
# OR: keep `--ours` for all conflicts within files inside this dir
git checkout --ours -- path/to/some/dir
Run Code Online (Sandbox Code Playgroud)
checkout在此之前不要使用上一节中的常规形式,除非这是您真正想做的。请参阅我的回答中的“警告警告”部分:根据 Git,谁是“我们”,谁是“他们”?.
处理path does not have our version或path does not have their version错误:
如果你看到这样的错误:
Run Code Online (Sandbox Code Playgroud)error: path 'path/to/some/dir/file1.cpp' does not have our version # OR error: path 'path/to/some/dir/file1.cpp' does not have their version
...运行上述命令时,您只需git rm要先访问这些文件,然后再次尝试git checkout --oursorgit checkout --theirs命令。有关这些命令的详细说明,请参阅我的回答,包括自动查找和删除这些错误文件的表单:git checkout --ours when file spec 包含已删除的文件。
在这种情况下,这git checkout my_branch -- some_file_or_dir还不够,因为如果您在当前签出的分支或提交中存在指定目录中的文件但不存在于 中my_branch,那么您希望它们在本地被删除,但git checkout不会删除本地存在但不在指定提交中的任何文件,相反,它仅使用指定提交中的版本覆盖本地文件。因此,要在本地删除不应该存在的文件,以便您在本地得到的内容是您所拥有内容的精确副本my_branch,您必须执行以下操作:
git reset my_branch -- path/to/some/file_or_dir
git checkout-index -fa
git clean -fd
git commit -m "hard reset path/to/some/file_or_dir to its state \
as it was at my_branch"
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请在此处查看我自己的答案:Why git can't do hard/soft resets by path? .
git checkout在这里的回答中展示了更多这样的例子:根据 Git,谁是“我们”,谁是“他们”?.Con*_*ith 11
要从另一个分支恢复文件,只需从您的工作分支使用以下命令:
git restore -s my-other-branch -- ./path/to/file
该-s标志是source您要从中提取文件的分支的缩写。
(所选择的答案信息量很大,但也有点压倒性。)
如果您想要来自特定提交(任何分支)的文件,请说06f8251f
git checkout 06f8251f path_to_file
Run Code Online (Sandbox Code Playgroud)
例如,在 Windows 上:
git checkout 06f8251f C:\A\B\C\D\file.h
Run Code Online (Sandbox Code Playgroud)
或者,如果您想要来自另一个分支的所有文件:
git checkout <branch name> -- .
Run Code Online (Sandbox Code Playgroud)
这是一种务实的方法,它并不直接回答原始帖子,但有些人发现有用:
如果相关分支位于 GitHub 上,那么您可以使用 GitHub 提供的众多工具导航到所需的分支和文件,然后单击“原始”查看纯文本,并(可选)将文本复制并粘贴为想要的。
我喜欢这种方法,因为它可以让您在将远程文件拉到本地计算机之前完整地查看它。
| 归档时间: |
|
| 查看次数: |
512587 次 |
| 最近记录: |