How to rebase effectively without letting commits pile up locally?

288 Views Asked by At

I'm trying to implement a workflow whereby I rebase my feature branch off master before merging it in. I've found that it's not a good idea to push my local commits on the feature branch to the remote repo before rebasing, as that essentially duplicates the commits on the feature branch and requires a subsequent force push as described here. (Git 101 I know.) I definitely don't want to get into the habit of --force pushing commits, and by letting commits first accumulate locally without pushing, then rebasing, and then pushing I avoid this.

However, I'd like to have my commits pushed to the remote server somewhere before rebasing, particularly in cases when the local repo is on a local machine (which could experience HD failure, etc.). Is there a way to do this, without --force pushing? I've thought of creating some kind of intermediate my_work branch but it seems like this would also lead to the 'local/remote diverged' problem. Maybe there's a clever (or not so clever) way of doing this I just haven't thought of though.

1

There are 1 best solutions below

0
VonC On

A possible worfflow would be:

  • Create and work on your feature branch. Regularly commit your changes.
  • (optional) Push your feature branch to a backup branch or a different remote server for safety.
  • Fetch the latest master and rebase your feature branch onto it.
  • After successful rebase, force push to your feature branch if necessary.
  • If needed, merge your feature branch into master remotely without checking out master locally.
# Working on feature branch
git checkout -b feature
git commit -m "Your commit message"

# Optional: Backup
git push origin feature:feature_backup  # or to a different server

# Rebase
git fetch origin
git rebase origin/master

# Force push to feature branch
git push origin +feature

# Merging into master remotely
git fetch origin
git push origin feature:master