1

I have the following branches where A is the current active branch:

*A  C
 | / 
 B

Often, I want to rebase C onto A and then merge C into A. So far I'm doing it with single commands:

git checkout C
git rebase A
git checkout A
git merge C

# and optional to clean up no longer needed branches
git branch -d C
git push origin :C

What would be a simple way to do this in one command? Is there a "git-way" or do I have to rely on a shell script?

23tux
  • 14,104
  • 15
  • 88
  • 187
  • I guess you have to use shell script only somewhat like this http://stackoverflow.com/questions/18860696/syntax-for-git-aliases-with-multiple-commands or this http://stackoverflow.com/questions/7534184/git-alias-multiple-commands-and-parameters but if the rebase causes any conflicts to occur then you might have an issue in merging. – Deepesh Dec 07 '16 at 09:35

1 Answers1

1

For routines like these I usually setup aliases:

git config --global alias.rebmerge '!git checkout C && git rebase A && ...'

or if you want to accept parameters:

git config --global alias.rebmerge '!f() { git rebase checkout $1 && git rebase $2 && ... }; f'

and then just call

git rebmerge A B C
Ivan Frolov
  • 1,008
  • 8
  • 6
  • Could you explain the `'!f() { ... }` a little bit more? – 23tux Dec 07 '16 at 09:46
  • I know some people who use `hub` to have similar shortcuts, but I've never tried https://github.com/github/hub – pedrorijo91 Dec 07 '16 at 10:07
  • 2
    @gusto `'!f() { ... }` here, '!' means shell script, `!f(){..}` is a function that takes `A`, `B` & `C` as parameter accessing by `$1`, `$2` & `$3` – Sajib Khan Dec 07 '16 at 10:12