1

Assume i have made 10 commits on myBranch (and myBranch is checked out). I want to rebase with master and squash all commits.

our current workflow is ...

  1. git checkout master
  2. git fetch origin
  3. git pull -r origin master
  4. git checkout myBranch
  5. git rebase -i master
  6. git push -f origin myBranch

A total of 6 steps AND interactive rebase consoles to deal with too (not to mention the risk of force pushing).

Is there a shorter workflow that will have the same effect?

danday74
  • 52,471
  • 49
  • 232
  • 283

3 Answers3

2

There's no reason to update your local master branch if your goal is simply to rebase against the upstream master branch. You can replace these steps:

git checkout master
git fetch origin
git pull -r origin master
git checkout myBranch
git rebase -i master

With:

git fetch origin
git rebase -i origin/master

If you want to avoid the interactive rebase, you can do what is described in this answer:

git fetch origin
git reset --soft origin/master
git commit -m 'message for merged commit'

This resets the current branch to origin/master without affecting your working directory, and it leaves all of your changes staged in the index. This means that the git commit will commit all of your changes in a single step.

Community
  • 1
  • 1
larsks
  • 277,717
  • 41
  • 399
  • 399
2

If all you want to do with your local commits is to squash them, you don't even need to do an interactive rebase. You can do it using git reset --soft, which is much more convenient:

git reset --soft HEAD~10 #soft reset to 10 commits ago

git commit -m "Message for the squashed commits" #all your changes will be already added to the index at this point

Then you can rebase against the remote's master branch:

git fetch origin git rebase origin/master

Note that squashing the commits is only safe if nobody (except you) has pulled these 10 commits from myBranch yet, otherwise you will rewrite history and you're gonna have a bad time.

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
1
git fetch origin
git rebase -i origin/master
git push -f origin myBranch

or if you want to save the changing of the stanzas on all lines and want to use a completely new commit message

git fetch origin
git rebase origin/master
git reset --soft origin/master
git commit
git push -f origin myBranch

or if you want to save the changing of the stanzas on all lines and want to reuse the commit messages from all squashed commits

git fetch origin
git rebase origin/master
git reset --soft origin/master
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"
git push -f origin myBranch
Vampire
  • 35,631
  • 4
  • 76
  • 102