Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I merge files in git?

Tags:

git

github

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?

like image 841
Martin Thoma Avatar asked Jul 19 '12 06:07

Martin Thoma


People also ask

Can you merge just one file in git?

We can use git checkout for far more than simply changing branches. If we supply it with a branch name and a file, we can replace a corrupted or broken file. Instead, if we want to pass some of the changed content we can use the --patch flag to manually merge an individual file.

How does a merge work in git?

How does git merge work? Git merge combines several sequences of commits into a single history. In most cases, that means merging two branches—most often a feature branch and the master branch. In this case, Git will take the commits from the branch tips and try to find a common base commit between them.

What is an alternative to merging in git?

Using git rebase Instead of git merge. Using the "git merge" command is probably the easiest way to integrate changes from one branch into another. However, it's not your only option: "git rebase" offers another, slightly different way of integration.


3 Answers

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

like image 175
Jacob Midtgaard-Olesen Avatar answered Oct 20 '22 03:10

Jacob Midtgaard-Olesen


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.

like image 39
rx_tx Avatar answered Oct 20 '22 04:10

rx_tx


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.

like image 25
VonC Avatar answered Oct 20 '22 03:10

VonC