Search code examples
gitgit-workflow

How to push the same edit to different branches in git?


I have a Git repository which has a master branch. Now I want to create different branches which have the same changes in every branch (for example the database). So I can have these files in master:

  • a.java
  • b.java
  • database-1.xml

and two branches, called branch-1 and branch-2, with these files:

  • a.java
  • b.java
  • database-branch-1.xml

and

  • a.java
  • b.java
  • database-branch-2.xml

Now, let that I find a bug in a.java, obviously I would like to fix it and commit the same change in every branch. How can I achieve my goal?

Note: The database files must remain different and the other files will be always the same.


Solution

  • merge. But it's best to change the file a.java at the common starting point where there is only database-1.xml otherwise it will get clobbered.

    Cherry pick is the other way. But managing branches that way becomes unmanagable quickly.

    This brings up the point about deploy config. You should not branch just because your config is changing based on where you're deploying. Make database.xml not work at all. Rely on deployment scripts or smudge/clean scripts to adjust the config based on where you're deploying - even locally.

    This means the config should have the structure you need but be adjusted only in the areas it needs to be - usually just a connection string.

    Think of what happens when you need to adjust something in the database file that has to go across all branches.