Git : Make the same edits on two different branches at the same time

I would like to know if it’s possible to work on two branches simultaneously if I want to write code that is supposed to be in those two branches. So that, if I commit my changes, it actually updates both branches.

For example :

  • git push on branch src refspec does not match any
  • Subversion branch reintegration in v1.6
  • Compare only changes in a commit with current working dir
  • Git remote branches missing when cloning via http, but available when cloning locally in the remote machine
  • How do you create the 'master' branch on git if you forgot to create it before branching?
  • Branch descriptions in git, continued
  • The code in my branch A is this

    print "I like cake"

    And the one in my branch B is

    print "I love bananas"

    But I’d like them both to have this line of code :

    print "Everyone loves stackoverflow"

    Do I really have to write it in branch A, copy it, checkout B, and paste it ?

    Can’t I apply changes to two different branches ?

  • Heroku : Username for '': git
  • git daemon slow first request on ubuntu 14.04
  • what is meant by -u option in git
  • Find the merge base between a remote and a commit
  • Why after merge does GIT say “Already up-to-date”, but differences between branches still exist?
  • Simple way of pushing repo to cdn?
  • One Solution collect form web for “Git : Make the same edits on two different branches at the same time”

    If you want to work on two branches simultaneously you should seriously think about why you have separate branches to begin with. Branches are usually there so you have separated commits that do not interfere with the other ones.

    That being said you could† cherry-pick the commit on B, once it’s done on branch A. This will reapply the commit on the other branch. I.e. git cherry-pick A to pick the top commit on A and reapply it to the current branch.

    †The problem is that it would try to add the diff, where the line print "I like cake" is being removed which is not present on branch B, so the patch would fail and as such also the cherry-pick, resulting in a conflict. And that actually makes sense: How exactly should Git know that those two cake/banana-love lines are actually related to each other?

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