git: how do I merge between branches while keeping some changesets exclusive to one branch?

前端 未结 9 550
情书的邮戳
情书的邮戳 2020-12-24 15:38

There\'s a special place in hell for people who hardcode absolute paths and database credentials into multiple random places in web applications. Sadly, before they go to he

相关标签:
9条回答
  • 2020-12-24 16:06

    A technical (Git) solution would be using git attributes, using the attribute merge.

    merge
    

    The attribute merge affects how three versions of a file is merged when a file-level merge is necessary during git merge.

    You will find in the SO question "How do I tell git to always select my local version for conflicted merges on a specific file?" an example of using such an attribute, to force keeping the local version of certain files when merging to a given branch.

    The problem with setting merge attributes is that the files that contain the paths may contain other changed code, which I want merged

    Do not forget you can associate any kind of script to manage those merges through git attributes. That include a script able to keep changes you want local, while merging the rest. It is more complicated to write such a "merge manager", but it is a way toward an ad-hoc automated solution.


    A less-technical solution would be to separate the configuration values from the configuration files:

    • the configuration file contains only names to be replaced
    • the configuration values are several files (one per environment) with the actual values for each name.

    A script is used to replace the name in the actual config file by the values of one of the config values files needed for a given environment.

    0 讨论(0)
  • 2020-12-24 16:13

    You can use git cherry pick to only merge the patches you select. Just cherry pick every commit except for the local one over to the master branch.

    0 讨论(0)
  • 2020-12-24 16:16

    The question is an old one, but I still have not found a good answer. Currently I am facing the same issue and below is my workaround to deal with it:

    There are two branches in my local repo: master and local_settings. Having cut off the local_settings branch from master I committed there all local paths, not tagging and not trying to remember them. During local development I am switched to the local_settings branch, so I can run an application using local paths. But when it is time to commit I stash a current state and switch to the master branch. Then I pop the stashed changeset and commit it into master. And the final step is to switch back to local_settings, merge from master and continue development. To recap: I commit into the local_settings branch only changes that will stay locally and will never go into master; and no merges from local_settings to master.

    Now let's say I need to add a "good" modification to a file with a local path added earlier, but the "good" modification is wanted in the master branch. I do my changes when the working copy is a head for local_settings, stash it and check out master. The stash keeps a changeset, that is relative to local_settings, although I am on master already. git stash pop applies the stashed changeset to the working copy and ends up having a diff relative to master, but only with the recent modification excluding the local path that had been added earlier and was not a part of the recent stashed changeset. Hence it can be committed without messing paths in the master branch. Afterwards again merge from master to local_settings.

    0 讨论(0)
提交回复
热议问题