Git: howto manually merge file from working tree with a file from HEAD
I don't know if what i'm trying to do makes sense but here is the explanation:
I have some source files that are generated by a tool and then changed a little and committed. What happens is that sometimes I need to regenerate these files.
I need to do a manual merge of this new file with the on开发者_开发知识库e that was previously committed and pick which parts to take from the old version and which parts to take from the new version.
Is there a way to do this?
Thanks!
You can create a branch like "generated_files" what you never touch but when you generate your files.
In your "master" branch you can merge the branch "generated_files".
Each time you generate new files you must commit it into the branch "generated_files" which will create a new delta from the previous generated version.
If you merge after this branch in "master", this will apply only the delta from the previous version and keep your modification.
Maybe you can use a strategy during the merge to avoid conflict.
Try something like:
git clone
git branch -a "temp"
git checkout temp
... generate new files ...
git add .
git commit -m "commit text"
git checkout master
git merge temp
In essence, do your stuff in the other branch and simply merge that branch to the master branch.
One way of doing it is to make use of -m
option to "git checkout":
Check out the base (ancestor) version, for example if you want to do a three-way merge between current version (HEAD), previous version (HEAD^) and generated file (in working directory):
git checkout HEAD^
Do not worry about warning about detached HEAD.
Generate your files
Use
-m
option of "git checkout" to merge changes from working area:git checkout -m @{-1} # or "git checkout -m <branch>"
From git checkout manpage:
-m::
--merge::When switching branches, if you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context. However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.
Not tested!
As a fallback you always have git merge-file low level (plumbing) command.
精彩评论