1

Suppose I clone from somewhere...

git clone git://test.git
cd test

Then I make 20 branches from the master branch...

git branch alt1
git branch alt2
....
git branch alt20

Then I make individual commits within each branch, so each branch has their own unique history.

Now several days pass and I decide to pull from the initial remote:

git checkout master
git pull

Which will work fine, but now I want to merge these new changes into the 20 branches I created earlier.

Obviously I can manually git checkout alt and git merge master within each branch, but I was wondering if I can automate this process just by doing git pull earlier. Is this possible by configuring my repository somehow? Or is shell scripting the only way to automate this?

Cinolt Yuklair
  • 331
  • 1
  • 10

2 Answers2

3

Is this possible by configuring my repository somehow?

No.

Or is shell scripting the only way to automate this?

Yes.

  1. I don't know what your use-case is, but normally you would not keep all branches up-to-date at all times. It is common to do this manually as needed since fully automating it is impossible, due to potential merge conflicts (be they syntactic or semantic).

  2. Are you sure, that you want merge and not rebase? Merging will create a commit on each branch that has diverged. Over time there may be a lot of merge commits, which depending on your use-case may not create meaningful history. That said, choosing rebase may make it harder return to a known working state of the branch, since its history is rewritten and its old history will eventually be garbage collected.

  3. If, despite (1), you really want to do this:

Merge

for branch in $(git for-each-ref --format='%(refname:short)' 'refs/heads/alt*'); do
  git checkout $branch && git merge --no-edit master
done

Rebase

for branch in $(git for-each-ref --format='%(refname:short)' 'refs/heads/alt*'); do
  git checkout $branch && git rebase master
done

But, do note that either of these may fail due to merge conflicts. At the very least, you should break the loop if merging fails.

jsageryd
  • 4,234
  • 21
  • 34
-1

Which will work fine, but now I want to merge these new changes into the 20 branches

The answer is YES. (but with a script) Here is a detailed post on how to do it:

http://blogs.atlassian.com/2013/05/git-automatic-merges-with-server-side-hooks-for-the-win/


Other options:

If you decide to do it manually you can merge as many branches as you want.

you can do for example:

git merge branchA branchB .... branchN

Another option which you can do it is to a cherry-pick.

# get the list of branches you wish to commit to :
# for example all the branches in this case but you can filter them as 
# you like
for ref in $(git branch -a); do .... done;

git rerere

Here is the killer feature for you. Lets say that your merge will cause a conflict each time you merge it.

How will you resolve it using script?

The answer is to turn on git rerere.

The git rerere functionality is a bit of a hidden feature (Git actually has a lot of cool hidden features, if you haven't figured that out yet).

The name stands for "reuse recorded resolution" and as the name implies, it allows you to ask Git to remember how you've resolved a hunk conflict so that the next time it sees the same conflict, Git can automatically resolve it for you.

To enable the rerere functionality, you simply have to run this config setting:

git config --global rerere.enabled true

https://git-scm.com/blog/2010/03/08/rerere.html

enter image description here

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167