13

I have several feature branches and a master branch. Feature2 is done. Normally I would rebase (working with a remote SVN repo and would like to keep the history, so no regular merge) and ff-merge. But since master hasnt changed since I branched, I would like to move the master head (at E) to G. Using git branch -f master G does not result in any visible changes, I assumed this is because G is on a different branch.

Is it safe to use git update-ref -f master G here instead? Should I stick with rebase/ff-merge? Something even better?

feature1      C-D  
             /
master    A-B-E            
               \                      
feature2        F-G  

Thank you.

kostja
  • 56,537
  • 45
  • 164
  • 213
  • 1
    What's wrong with a regular fast-forward merge of `G` into `master`? No need to rebase. You'd keep the history, and you'd get a straight graph (`A-B-E-F-G`). – ellotheth May 25 '12 at 13:52
  • aah, the obviousness of it! the graph looks exactly like after a rebase. I wonder why I ever thought about sth else. Care to repost as an answer? – kostja May 25 '12 at 14:49
  • The branch feature2 already has a 'straight graph' - just rename feature2 to master (or move master to feature2)! – GoZoner May 25 '12 at 16:07
  • @GoZoner It's just the small difference of one line and two lines of typing. Following the first virtue of programming ;) – kostja May 25 '12 at 18:41
  • Sure, now that we've all posted like eighteen ways to do exactly the same thing. ;p – ellotheth May 25 '12 at 22:30

5 Answers5

13

You don't have to merge the branches, a reset is enough. Assuming master is checked out:

git reset --hard feature2
ralphtheninja
  • 107,622
  • 20
  • 101
  • 118
10

No merging required - just rename the branches. Since you don't care about feature2 ('is done') nor the existing master (at 'E') you just need the following.

git branch -d master
git branch -m feature2 master

Simple is better?

Remember there are two key concepts involved:

  1. The Git commit graph, and
  2. The Git references

When you do a merge (of various flavors and including rebase) you are changing the commit graph. The changes involve adding nodes, adding links or perhaps moving links. References (including branches and tags) only point to commits and thus changing a reference just changes the pointed-to commit - not the structure of the graph.

So, in your case, there is no needed change to the structure, just a changing of the references.

A one line version is:

git branch -f master feature2

which keeps the feature2 branch around (unlike the prior two-liner which axes feature2).

GoZoner
  • 59,252
  • 19
  • 87
  • 137
  • 1
    Thanks, GoZoner. Nice alternative I have not thought about. But I guess I'm sticking to ff-merge in my case. – kostja May 25 '12 at 15:35
  • seems to work locally. But how do I push the changes to the remote server ? It complains of a non-fast-forward merge – josinalvo Mar 20 '17 at 15:23
  • By moving the 'master' you have created an inconsistency with all remotes. The fix is `git push master --force` but the '--force' argument should make you be wary of its impact on any other users of `` – GoZoner Mar 20 '17 at 15:28
10

A regular merge of G into master will do the trick, no need to rebase:

    feature1      C-D  
                 /
    master    A-B-E            
                   \                      
    feature2        F-G

git checkout master
git merge feature2

    feature1              C-D  
                         /
    master, feature2  A-B-E-F-G
ellotheth
  • 3,803
  • 1
  • 15
  • 28
1

The update-ref is safe. A branch head is nothing more than a little "read me!" tag hung on a commit. It's purely by convention that git picks it up and hangs it on a different commit for you at times.

Using git branch -f master G does not result in any visible changes

What does git log --decorate --oneline --all say? git show master?

jthill
  • 42,819
  • 4
  • 65
  • 113
0

It might be best to perform a non fast forward merge of feature 2 into master by using git merge --no-ff feature2 if you have checked out onto the master branch. You should end up with the following

feature1      C-D  
             /
master    A-B-E-----H          
               \   /                   
feature2        F-G 
Dan Lister
  • 2,337
  • 1
  • 19
  • 34
  • I would like to avoid regular merging onto master where possible to keep the history, I said so in the question. And, why do you think that a no-ff-merge is better suitable than ref move? – kostja May 25 '12 at 11:28