0

I branched feature off of master a few days ago and hacked away, putting my commits into feature. master (and origin/master) never moved, so, if I were on master, I could fast-forward to feature. But I'm on feature. Obviously it isn't hard to checkout master and merge it to feature, but is there a way I can fast forward a parent branch to my working branch while on my working branch?

From:

|--|--| << master
       \
        \
         |--|--| << feature (checked out)
               ^                  
              HEAD

git magic-command

|--|--| 
       \
        \
         |--|--| << master (checked out), feature
               ^                  
              HEAD

Just trying git merge master returns 'Already up-to-date', and rebase doesn't make sense since replaying that work would leave me in the exact same state. I guess I could git reset master or something, but that seems hacky.

ABMagil
  • 4,579
  • 4
  • 21
  • 35
  • The only way I've really known of doing this without inspiring a lynch mob (i.e. rebasing a pushed common branch) is to just merge the two branches together. That's all you really should be doing, anyway. What are you trying to do with the other approach? – Makoto Jan 15 '15 at 22:01
  • @Makoto I can't figure out how to simply move the master ref forward to the feature ref without checking out `master` first. – ABMagil Jan 15 '15 at 22:04
  • Why is this a bad thing? Unless you've pushed your repository remotely, and you only intend to move the remote reference of master, then you're going to be forced to check out `master` once more. It also keeps your own Git history in line, just in case the remote decides to lose its marbles. – Makoto Jan 15 '15 at 22:06
  • I don't understand what the remote has to do with anything, but I'm just trying to placate my own intense laziness. If I'm done with a feature branch and it's ready to be pushed to master, I'd love if git supported my being able to just say: "Hey `master`. I'm ready for you all the way up here! Come on over!" – ABMagil Jan 15 '15 at 22:11
  • 1
    `git branch -f` or `git update-ref` should both work for you. – Andrew C Jan 16 '15 at 00:02

2 Answers2

1

You can use git branch to force up date if you specify -f

-f 
--force
Reset <branchname> to <startpoint> if <branchname> exists already. Without -f git branch refuses to change an existing branch.

Note that Git will update your branch even if it isn't a fast-forward merge. If you wanted a measure of safety you might want to script something that calls git merge-base --is-ancestor to verify you are in a fast-forward state before doing the update.

Andrew C
  • 13,845
  • 6
  • 50
  • 57
  • combined with http://stackoverflow.com/questions/2816715/branch-from-a-previous-commit-using-git, there is the option to create a branch at a specific commit, which would effect the behavior I'm looking for: `git branch -f master feature` – ABMagil Mar 30 '15 at 18:54
  • Yes sorry, i thought it was obvious with the and in the quoted text. – Andrew C Mar 30 '15 at 19:14
-2

To answer the question, no - not unless you want to use rebase and inspire a lot of ill will between you and your fellow collaborators*.

It sounds like you could benefit from the git flow tool though. It uses the Git Flow inspired by Vincent Driessen and creates a nice set of commands around it.

*: I say "ill will" because of the perils of rebasing. You're rewriting history, and if someone depended on history that was rewritten by an errant rebase, they now must spend time and effort to restore their working state so that they can continue to work. It is far simpler to just do the merge or even use Git Flow as I've described above.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • I'm really unclear on why rebasing would inspire ill will. I'm not touching my remote repo _at all_. I'm fast-forwarding a branch, just not from that branch... – ABMagil Jan 15 '15 at 22:46
  • Eventually you will do `git push origin master`, hopefully not followed by `git push -f origin master`. You're going to have to update your remote at some point. – Makoto Jan 15 '15 at 22:48
  • Look at the example again- *I am not rewriting history*. There is linear history between my master and feature branches. There is a difference between a fast forward merge and a merge which creates a merge commit. – ABMagil Jan 16 '15 at 13:36