1

I am trying to integrate git-rebase into my workflow. I have a feature branch my-feature that was branched from develop. In order to have the most recent changes from develop my plan is that I every morning I should (while on my-feature) git rebase develop. This works pretty well, however when I try to push my feature branch

$ git push origin my-feature

I get this error:

 ! [rejected]        my-feature -> my-feature (non-fast-forward)
error: failed to push some refs to 'git@some-domain:some-directory/my-repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I am thinking that git has given shared commits new local SHAs as the logs for origin/my-feature and local my-feature show different ones for the same commit messages. In fact, doing a git pull like it suggests gives me conflicts to reconcile. But I am the only one using this feature branch, so the conflicts are ..with myself?

I was aware that interactive rebase will assign new SHAs when commits are squashed or removed. However I thought what I was doing was a pretty standard workflow for integrating a remote develop branch for longer-running features.

How can I use the rebase functionality to integrate a remote dev branch on a long running feature without introducing conflicts when I go to push my feature?

For clarity my process was basically:

  1. On develop
  2. Checkout new branch my-feature
  3. Do work on my-feature
  4. Push code to origin/my-feature (success)
  5. Do more work on my-feature
  6. While on myfeature, $ git rebase develop (success)
  7. git push origin my-feature <-- ERROR (above)

Switching to develop and git rebase my-feature gives me a few expected conflicts I need to fix, but no errors.

Edit:

Also, I am aware I can use --force, however I can foresee sharing a feature branch so don’t think introducing that flag as part of the workflow is a great solution.

1252748
  • 14,597
  • 32
  • 109
  • 229
  • If you want to avoid force-pushing do not edit pushed commit. Never do `commit --amend`, `filter-branch`, `rebase` or `reset` on pushed commits. `commit` and `revert` are your only options. With commits not yet pushed you can do whatever you want. But change pushed commits and `push --force` is unavoidable. – phd Dec 11 '19 at 21:32
  • @phd that’s if i want to continue to `git rebase develop` every so often, right? – 1252748 Dec 12 '19 at 02:23
  • As I said it depends if rebased commits have been pushed or not. – phd Dec 12 '19 at 10:22

1 Answers1

2

There's a lot of great git rebase knowledge here: Git workflow and rebase vs merge questions

But if that doesn't help, often the only way you can push after rebasing is to git push --force, but as the --force flag should indicate, this can be dangerous when there are other people working on the project.

You're correct that rebasing changes the commits and SHA's, since rebase takes the commits you're rebasing and re-plays them on the target branch. But since each commit depends on the commits before it (and that's how the SHA is generated) they will always be new. This causes the problem when you push, since the remote has one set of commits, and the content you're pushing has different commits. The --force flag tells the remote to replace its commits and replace them.

My personal workflow when working on a feature is:

git checkout -b branch_name
# do work
git add .; git commit -m 'my commit message'; git push -u origin branch_name
# do work
git add .; git commit -m 'my commit message'
# there's been an update to dev/master/etc since my last push
git rebase master # (success)
git push --force

But again you have to be careful when using --force

Note that this only happens when you've first pushed the branch to the remote, and then rebased after. You can rebase and push without the error you posted above as long as you haven't pushed before.

Adam Marshall
  • 6,369
  • 1
  • 29
  • 45
  • That was a really informative read, thanks so much for that link! – 1252748 Dec 11 '19 at 19:54
  • I kind of don’t like the force flag as I can foresee it being problematic if I am sharing a feature branch with another developer. I should have mentioned that in the question. Thank you for this answer. Cheers. – 1252748 Dec 11 '19 at 19:55
  • Yep, that is the payoff for it. It's really powerful and helps in many situations -- as long as the disclosure is understood. The only other way to get around it is to not push as often, but that has its own issues. – Adam Marshall Dec 11 '19 at 20:29
  • Also, you can use the `--force` flag when working with other people, but you just have to communicate before you do it. – Adam Marshall Dec 13 '19 at 17:38