我可以在git中合并文件吗?

Mar*_*oma 6 git github

我有三个文件Shell-ijk-ArrayList.java,Shell-ijk-Vektor.javaShell-ikj-ArrayList.java在这个混帐存储库.我已经手动"合并"了它们MatrixMultiplication.java.现在我想摆脱其他三个,但我想保留它们的历史.做这个的最好方式是什么?

我找到了git merge-file,但是当我尝试的时候

git merge-file MatrixMultiplication.java Shell-ijk-ArrayList.java Shell-ijk-Vektor.java 
Run Code Online (Sandbox Code Playgroud)

我明白了

error: Could not stat MatrixMultiplication.java
Run Code Online (Sandbox Code Playgroud)

我可以简单地删除旧文件git rm,但后来我将失去历史,不是吗?

我也可以移动它git mv,但对于过时的文件,什么是一个很好的文件夹?

Jac*_*sen 2

在我看来,您正在将具有单独逻辑的不同文件合并到一个文件中?这不是 git merge-file 的用途。

来自git help merge-file

git merge-file incorporates all changes that lead from the <base-file> to 
<other-file>  into <current-file>. The result ordinarily goes into 
<current-file>. 

git merge-file is useful for combining separate changes to an original. 
Suppose <base-file> is the original, and both <current-file> and <other-file> 
are modifications of <base-file>, then git merge-file combines both changes.
Run Code Online (Sandbox Code Playgroud)

示例(也来自帮助页面):

 git merge-file README.my README README.upstream
       combines the changes of README.my and README.upstream since README, 
       tries to merge them and writes the result into README.my.
Run Code Online (Sandbox Code Playgroud)

要将完全独立的文件中的逻辑合并为一个,您手动将它们组合起来是正确的。

删除文件不会删除其历史记录,您可以使用以下命令查看已删除文件的日志:

git log -- <path_to_file>
Run Code Online (Sandbox Code Playgroud)

参见相关问答:

Git:如何在项目提交历史中搜索已删除的文件?

在 Git 存储库中查找并恢复已删除的文件