TIL `git fetch origin main:main` will fetch updates *and* apply them to your local main branch, even when on another branch.
No more checking out main, pulling changes, checking out feature branch, and rebasing on main for me!
TIL `git fetch origin main:main` will fetch updates *and* apply them to your local main branch, even when on another branch.
No more checking out main, pulling changes, checking out feature branch, and rebasing on main for me!
As some folks have commented, `git rebase origin/main` will also rebase your feature branch without needing to switch (though AFAIK that won't update your local copy of main)
Blogged this for posterity: https://geoffrich.net/posts/git-rebase-with-latest-changes/
@geoffrich Oh excellent tip! Thank you so much for sharing.
@geoffrich I like this. I think I usually fetch and then rebase but with the origin version of main in order to skip switching branches. That doesn’t update the branch locally though.
```
git fetch —-all # all because why not
git rebase origin/main # might not need the forward slash
```
@knowler @geoffrich This is something that often gets missed conceptually.
You don't need a local "main" branch at all, up-to-date, stale, or otherwise. The remote version of every branch "exists" locally under "<remote>/<branch>" as soon as you fetch.
@geoffrich if you want to rebase your feature branch on top of the latest remote changes, just `git pull -r origin main` while on the feature branch does that too.
that's a nice trick in any case and I'm not sure if i misunderstand, but on a feature branch i just do `git pull --rebase origin main`
@geoffrich A rebase can deal with conflicts, how would that work with `git fetch origin main:main`?
@geoffrich It doesn't seem to work here, which makes sense as fetch does nothing to apply, afaik. It needs a merge or rebase after fetching. But it's nice to not have to swap to your main branch locally first.
@geoffrich LOVE IT. i've had this aliased to git sync <branch name> a while, and i think it's still my favorite because it'll clobber my local if the histories diverged (which is what i want, most of the time):
!sh -c 'git fetch --all --prune && git branch -f $0 $0@{upstream}'
@ellotheth @geoffrich thank you! This will become a 1-liner script on Monday morning on my machine.
I shall name it: 'rebase_main'
@geoffrich I exclusively rebase against origin/main to avoid these kinds of shenanigans
@geoffrich you can rebase on the remote branch without pulling your local one too; git rebase origin/main
Still need to fetch though. Honestly I think fetching should be implicit.
@geoffrich @zachleat Useful! You can also fetch and rebase onto origin/main without updating local main.
@geoffrich
It might be also worth to just delete the local main branch. I mean... There's no point in keeping it.
(But to be honest, I don't do this myself although I should)