-1

I've recently had to switch to git from mercurial and my normal workflow has become incredibly annoying to replicate, so I'm looking for some pointers. Happy to answer anything I miss here. But what I like to do is as follows:

  1. On master, pull to get all recent changes.
  2. create my working branch off master.
  3. Do work essentially just amend everything to my first commit.
  4. push the change for review.
  5. Address issues, again amend, and then re-push
  6. If I somehow get behind the master branch, I usually just pull all the changes to master to have my branch just sit on top of them to avoid merge conflicts prior to landing.
  7. land.

My question:

So, I've abandoned my constant ammending due to gits structure which is fine. So now after any comments I make a new commit. The real issue happens when I get behind master. I usually commit my changes. Go to master. git pull. Switch back to my branch git rebase master and try to re-push my changes. This, for some reason always breaks with the following "my branch is behind its remote counterpart". What am i missing here?

Thanks

user1352683
  • 397
  • 1
  • 3
  • 14
  • Can you elaborate on how this breaks? Besides the fact you could have saved switching to master (`git fetch origin && git rebase origin/master`), the workflow sounds completely reasonable. – Mureinik Mar 19 '20 at 20:35
  • it consistently says "my branch is behind its remote counterpart" is that command safe to run from a non master? Assuming it is if I read that correctly. – user1352683 Mar 19 '20 at 20:37
  • You say, *this [...] always breaks*. What do you mean by *breaks*? You need to explain the specific problem you're having. – Daniel Mann Mar 19 '20 at 20:38
  • 2
    `git fetch` just updates your **local** copies of `origin`, it's pretty safe. You can then rebase any branch on top of any branch. From a git perspective, it's safe - assuming it makes sense from a functional perspective. – Mureinik Mar 19 '20 at 20:38
  • @Mureinik for some reason using that seemed to wrok vs. what I did. Thank you ! – user1352683 Mar 19 '20 at 20:45

1 Answers1

3

Note: hg pull == git fetch (and not git pull, which would be comparable to hg pull -u).

The error message you are seeing about your branch being behind is most certainly caused by something known as a non-fast-forward merge/push. When you rebase, you change existing history. If you have already pushed your old version of the history then it is different from your local one and thus git tells you that it cannot push your branch, because it would modify the history. You have two options: 1. merge (does not what you want in this case) 2. force push

Also read Git push rejected "non-fast-forward" which provides a solution for exactly this problem.

DISCLAIMER: never force-push history on shared branches unless you really know what you are doing. Force-pushing destroys existing history and if the old history was already used by somebody else, you are in for some nasty surprises in the future.

That said, the solution basically boils down to:

git fetch
git rebase origin/master
git push origin +your_branch:your_branch
knittl
  • 246,190
  • 53
  • 318
  • 364