Login
You're viewing the front-end.social public feed.
  • Geoff Richgeoffrich
    Feb 2, 2023, 12:01 AM

    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!

    💬 13🔄 110⭐ 247

Replies

  • Geoff Richgeoffrich
    Feb 2, 2023, 4:11 PM

    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)

    💬 1🔄 0⭐ 5
  • 💬 0🔄 4⭐ 4
  • 💬 0🔄 0⭐ 0
  • Feb 2, 2023, 2:23 AM

    @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
    ```

    💬 1🔄 0⭐ 1
  • Feb 2, 2023, 3:49 PM

    @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.

    💬 0🔄 0⭐ 0
  • Feb 2, 2023, 3:06 AM

    @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.

    💬 0🔄 0⭐ 1
  • Feb 2, 2023, 6:28 AM

    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`

    💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • Feb 2, 2023, 11:58 AM

    @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.

    💬 0🔄 0⭐ 0
  • Feb 2, 2023, 2:27 PM

    @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}'

    #git

    💬 1🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0