问题
I recently ran into a problem where I need to select a few files to push to remote branches with git. My specific use case here is there is only one file created/modified in every single commit, and I need to programatically push the selected files (in their latest state). I did a bit of research and found 2 tricks that came close to what I need to do:
- I can use cherry-pick to pick certain commits into a new branch and merge that branch into the remote master.
- I can use rebase -i to reorder the commits, I assume I can reorder so that the commits relating to those selected files are all in front and I can just push the last commit in that list.
For cherry-pick it is a bit confusing. I can create a new branch without all the dirty commits and pick the file commits into that branch. However if the file already exists in the branch, it always throws a conflict that I have to fix manually, not ideal.
For rebase -i, from what I have read I need to goto an interactive editor where I need to manually reorder the commits, and after that I can do a git push origin to apply everything up to the commit-SHA (which is reordered). Not ideal since I have to do manual work.
Overall I think rebase came closer to what I need, but I couldn't find an easy way to do it programatically. Can anyone come up with some git analogy operations that can accomplish my task?
回答1:
I think I have found the solution to this problem: cherry-pick --strategy=recursive -Xtheirs <commit-SHA>
.
Specifying the 'theirs' option in recursive merge strategy forces conflicts to auto-resolve cleanly favoring the version that's being merged in. This will ignore all the conflict warning but grab whatever is in the commit.
I guess now I just want to know why cherry-pick would produce a conflict warning when picking a commit that modified an existing file in the current branch, even if just adding a line.
来源:https://stackoverflow.com/questions/15937405/git-how-to-push-changes-made-to-only-certain-files