6

Something I often find myself doing is, "Rebase a feature branch, merge it & delete it." To do that I run:

git rebase master feature
git checkout master
git merge feature
git branch -d feature

That seems quite laborious for something I'd imagine to be a common workflow. Does anyone know a faster way?

(Obviously I could write a script, but I'm wondering if there's a built-in approach I've missed.)

Kris Jenkins
  • 4,083
  • 30
  • 40
  • git being quite low-level, I don't think there's any way to reduce number of commands other than scripting – CharlesB Sep 21 '11 at 08:40
  • 1
    True. It may well be the case that the answer to my question is, 'No'. It seemed worth asking though, just in case. :-) – Kris Jenkins Sep 22 '11 at 08:35
  • make up your mind -- either rebase or merge, not both. – J-16 SDiZ Sep 26 '11 at 14:35
  • @J-16SDiZ I'm baffled by your comment. How is it an either/or decision? Rebase rewrites commit histories, merge joins them. Both are useful. – Kris Jenkins Sep 27 '11 at 13:47
  • both are useful, but "rebase-just-before-merge" is not. This merge does nothing. – J-16 SDiZ Sep 29 '11 at 01:10
  • In my example it certainly does something. It fast-forwards 'master' to match 'feature'. – Kris Jenkins Sep 29 '11 at 03:23
  • Yes. And if you don't rebase before the merge won't be a fast-forward and you'll have a new merge commit. This can be useful if you want to preserve the notion of the branch in the repo history but usually a fast-forward merge is the way to go as it leaves the history cleaner. – Gnustavo Feb 19 '12 at 23:22

4 Answers4

1

The general approach remains scripting or defining aliases, as illustrated in "Streamline your git workflow with aliases", except you might need a parameter as in "git alias with positional parameters":

rebmrg = "!f() { git rebase master $1; git checkout master ; git merge $1 ; git branch -d $1 }; f"
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    Thanks VonC, that's kind of you to provide an example. I'm really wondering if there's a native way to do it. I was hoping that someone would say something like, "There's a merge & delete flag that you've missed." For what it's worth, if I were writing an alias I'd do `git rebase master $1 && git...` - ie., use `&&` instead of `;` so that the command stops if any part fails... – Kris Jenkins Sep 22 '11 at 08:38
0

If you're on master already then it would be faster to just merge it in and delete it (git merge feature and git branch -d feature).

That's just two commands and you avoid re-checking out an old master simply to fast-forward it.

You'll also just do a single resolve instead of potentially multiple resolves as you might require rebasing multiple commits of the feature branch.

In addition, your history better reflects what commit the feature branch was actually developed on top of. rebase destroys this history.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • Interesting thought. Personally I've always preferred to rebase, presenting my commits to the world /as though/ they were written against the current latest version. I think the main development log looks cleaner that way. Am I just stuck in a subversion way of thinking? – Kris Jenkins Sep 22 '11 at 08:42
  • @KrisJenkins No, you're not. There are many of us who work like you do, it's just not to everyone's tastes. – James Gregory Sep 22 '11 at 08:44
  • 1
    @KrisJenkins: You can work any way you want but your question was asking for a faster way to integrate a feature branch. As far as I can see, a straight merge is fundamentally faster and easier than a rebase and merge. A rebase and merge is a more involved process by its very nature. – CB Bailey Sep 22 '11 at 08:50
0

You can save one command (after rebase the merge will resolve to a fast-forward anyway):

git rebase master feature
git checkout -B master
git branch -d feature

But better use aliases as others have suggested or don't rebase at all, simply merge

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
knittl
  • 246,190
  • 53
  • 318
  • 364
0

It seems that your merged branch is exactly the same the as rebase one, therefore you can just rename it to master (wich the -M flag to force it as master already exists)

git rebase master feature
git branch -M feature master
mb14
  • 22,276
  • 7
  • 60
  • 102