【问题标题】:Instead of git checkout file one by one, checkout a whole commit不是一个一个地 git checkout 文件,而是 checkout 整个提交
【发布时间】:2021-08-09 02:29:18
【问题描述】:

我有一种情况,我致力于 B1 分支。在提交中,我有几个文件。现在我希望将这些文件复制到分支 B2。

实际上,我对当前分支 B2 上的提交中的每个文件都使用以下命令:

 $ git checkout B1 path/to/file1
 $ git checkout B1 path/to/file2
 ......
 $ git checkout B1 path/to/fileN

我想应该有一种语法可以让我直接检查从分支 B1 到分支 B2 的特定提交。我尝试了几种解决方案,但他们正在重写我不想要的整个分支,也不是所有文件的历史记录。我只想要特定提交中文件的状态。覆盖不是问题。

如何做到这一点?

【问题讨论】:

  • 您想获得“B1 中的完整回购”吗?或“仅在 B1 上的最后一次提交中修改的文件”?

标签: git bitbucket git-commit git-checkout


【解决方案1】:
git checkout B1 .

你必须指定一个路径才能得到你想要的,所以.是一个指定当前目录的路径。

【讨论】:

  • 我忘了说所有文件不在同一个目录下,也不一定有相同的扩展名
【解决方案2】:

要获取提交 xyz 中修改的文件列表,请运行:

# get only the names for the diff between xyz's parent and xyz :
git diff --name-only xyz^ xyz

如果您只想签出这些文件:

git checkout B1 -- $(git diff --name-only xyz^ xyz)

# for example : the files modified in the last commit on B2 :
git checkout B1 -- $(git diff --name-only B2^ B2)

【讨论】:

  • 将尝试,如果作品将其标记为最佳答案
猜你喜欢
  • 1970-01-01
  • 2020-12-08
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 2018-05-09
  • 2017-12-22
  • 2019-02-07
  • 1970-01-01
相关资源
最近更新 更多