Git – How to selectively apply changes from one branch to another?

Is it possible to selectively apply changes from one branch to another with Git?

More specifically, I use a public dev branch for GitHub and a private master branch for deployment. When changes are made to one branch they will need to be applied to the other, but some lines of code need to stay different. In my case it’s a few css classes and a feed.

  • bitbucket git push authentication failed (mac os x)
  • How to find out the space requirements of files to be committed?
  • What are the advantages of writing a Maven plugin in Groovy compared with Java?
  • How to 'pipe' password to remote.update() with gitPython
  • Getting started with Version Control
  • Why can't I delete a remote git branch with git push origin :branchname?
  • I’m new to Git but I’ve made my research :

    • git merge --no-commit --no-ff can be used followed by a git mergetool to chose what I want in case of a conflict. The problem is that it only works for conflicts Git can’t automatically merge, so what I want to stay different gets replaced before I get the chance to use my mergetool.

    • git difftool --cached is useful as it allows me to see the differences, but I need to copy what I want to keep from there and manually replace it with a text editor, as I cannot simply choose and save like I can with mergetool.

    • git cherry-pick seems to apply a specified commit to another, but what I want to stay different may be scattered to different commits, and these commits may not only include what I want to stay different. I cannot see this working unless I make millions of commits that would drive me mad.

    Also to be clear, I don’t want one branch to become another, what seems to be the case with a merge. I want two separate branches with their respective differences and apply changes from one to the other.

    Is there a better workflow that would allow me to keep a development and a deployment version by applying their changes and keeping a few differences? I don’t mind using separate repositories or different tools if it leads to a solution.

  • How do I generate a git commit log for the last month, and export it as CSV?
  • Is a full clone the only way to submodule add a branch?
  • How to manage large data files with GitHub?
  • error: Your local changes to the following files would be overwritten by merge:
  • Is it impossible to checkout a different branch in Jenkinsfile?
  • Force git to add dotfiles to repository
  • 2 Solutions collect form web for “Git – How to selectively apply changes from one branch to another?”

    I don’t think there is a way to pick parts of a commit from the same file. I would say you need to simply re-factor your code to move such parts in different files.

    BTW, if you want to take some files from a commit, you can use cherry-pick with a combination of other commands as explained here.

    I’ve also found out about patching :

    To create the patch : git diff dev > master.patch

    To apply it : patch < master.patch

    Git Baby is a git and github fan, let's start git clone.