Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving master head to a branch

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.

like image 605
kostja Avatar asked May 25 '12 11:05

kostja


People also ask

Can I pull from master to branch?

Use the git merge Command to Pull Changes From master Into Another Branch. First, we need to switch to the branch we want to work. The checkout command updates the files in the working tree according to the specified branch.


1 Answers

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).

like image 147
GoZoner Avatar answered Sep 27 '22 19:09

GoZoner