Martin Thoma
Martin Thoma

Reputation: 136339

Can I merge files in git?

I have three files Shell-ijk-ArrayList.java, Shell-ijk-Vektor.java and Shell-ikj-ArrayList.java in this git-repository. I have "merged" them manually to MatrixMultiplication.java. Now I would like to get rid of the other three, but I want to keep the history of them. What is the best way to do this?

I have found git merge-file, but when I try

git merge-file MatrixMultiplication.java Shell-ijk-ArrayList.java Shell-ijk-Vektor.java 

I get

error: Could not stat MatrixMultiplication.java

I could simply remove the old files with git rm, but then I'll lose the history, won't I?

I could also move it with git mv, but what would be a good folder for outdated files?

Upvotes: 8

Views: 3221

Answers (3)

Jacob Midtgaard-Olesen
Jacob Midtgaard-Olesen

Reputation: 161

It looks to me like you are merging different files with separate logic into one file? That is not what git merge-file is for.

From 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.

Example (also from the help page):

 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.

To combine logic from completely separate files into one, you are right in combining them manually.

Deleting a file does not remove its history, you can see the log of a deleted file with:

git log -- <path_to_file>

See related Q/A:

Git: How to search for a deleted file in the project commit history?

Find and restore a deleted file in a Git repository

Upvotes: 2

VonC
VonC

Reputation: 1324208

git rm won't loose the history of those files: see "Querying deleted content in git":

To see the commit history of that file, you can't do it the usual way:

$ git log file2
fatal: ambiguous argument 'file2': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

Instead, you need to do this:

$ git log -- file2

As for the git-merge-file, try to apply it on an empty, but added to the index, MatrixMultiplication.java file.

Upvotes: 1

rx_tx
rx_tx

Reputation: 300

I'm not sure git merge-file is what you need. This sounds more like code refactoring/shuffling around.

git rm keeps history, and is non-retroactive. If you revert to a previous version they will be there. When I go and check my old check ins the files I "git rm"'d are still there with full history.

Upvotes: 1

Related Questions