0

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.

Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
  • 2
    There is nothing at all wrong with force pushing your `feature` branch, assuming you be the only one working on it. – Tim Biegeleisen Apr 10 '21 at 03:20
  • @TimBiegeleisen If I get into the habit of force pushing `feature` I could very well slip and force-push a public branch at some point. YMMV but better for me just to not let that habit form in the first place. – Matt Phillips Apr 10 '21 at 03:25
  • 1
    No. The rebase workflow you are using includes rebasing `feature` on `master` to put the former branch directly ahead of the latter. The force push of `feature` is not wrong, and there is really no way to avoid it anyway. – Tim Biegeleisen Apr 10 '21 at 03:28
  • Let's get the topic out there, once and for all: One way to avoid the fact that you could someday force-push a _public_ shared branch is to **not** have one mirror of a shared branch in your local repo in the first place. I mean, it's not like you can't work with local feature branches if you decide to _not_ use a local main/develop/whatever branch locally. There.... I said it. :-D It's not like it makes it impossible to force-push into a shared branch... but it requires more willfulness to do it: `git push -f some-id-or-local-branch:main`. It's _not_ like you can do it just by accident. – eftshift0 Apr 10 '21 at 04:05
  • 1
    There is also nothing wrong with saving a temp "copy" of a branch. I save WIP (work in progress) branches all the time. No one else knows about them, no one else sees them, they are just backup. This is totally standard procedure; don't worry, be happy. – matt Apr 10 '21 at 04:07
  • @eftshift0 I see what you're getting at, you're saying work from local feature branch but just keep `main` or `develop` or whatever as a remote-tracking branch — just never check it out to local. – matt Apr 10 '21 at 04:08
  • Still another possibility is to back up to a different server. I do that all the time too (i.e. I've got two remotes). – matt Apr 10 '21 at 04:09
  • yes, @matt. And I see posts everywhere where people checkout main/develop/whatever, pull, then go to their branches, pull there... it's so unnecessary, if you ask me: `git checkout my-feature origin/master;` Days later: `git pull`. – eftshift0 Apr 10 '21 at 04:10
  • _and_ you can check them out if you need to.... _just_ checkout the remote branch: `git checkout origin/main` so that it's not created locally. – eftshift0 Apr 10 '21 at 04:12
  • @eftshift0 oh yeah, I see, you'll be warned you're in detached head mode and you just laugh a wicked laugh and ignore the warning. – matt Apr 10 '21 at 04:32
  • @eftshift0 You can't merge `feature` into `master` without having `master` checked out locally though can you? – Matt Phillips Apr 10 '21 at 12:52
  • ??? Ah, what? You mean, merging into the remote master forces you to have a local master? Not at all. You can pull it off, in a different fashion. Taking origin is _the_ remote you are playing with: `git fetch origin; git checkout origin/master; git merge feature -m "my feature"; git push origin HEAD:master` – eftshift0 Apr 10 '21 at 13:58
  • By the way.... I am not saying this is like the easiest to pick up. I am _just_ saying that it's a **possibility** to work without having the shared branches locally _and_ it would also avoid you from shooting your own foot by pushing into those branches unwillingly/unknowingly. – eftshift0 Apr 10 '21 at 14:06
  • ... and, that was the long route. You could pull it off also from your local branch if you created it from the remote branch: `git checkout feature; git pull; git push origin feature:master` – eftshift0 Apr 10 '21 at 14:13

0 Answers0