Search code examples
gitmergerebase

Merge/rebase remote tracking branch


Is it possible to merge/rebase remote tracking branch in git (local)?

I know it can be modified by git pull. But can we modify the remote tracking branch by any other git commands (locally)?


Solution

  • . just wondering how push will modify remote tracking branch ? (when we push our local branch to hosting site, automatically in the background, git merge runs?

    There is no merge happening on the remote side:

    • either the push adds new commits on top of the remote tracking branch, and said remote branch simply moves its HEAD to reflect the new most recent commit in that (pushed) branch
    • or the push adds a divergent history, and gets canceled (unless you do push --force)

    It is common to use a remote tracking branch to rebase a local branch on top of:

    git switch my-feature-branch
    git fetch
    git rebase origin/main
    git push --force
    

    That way, when you do a PR (Pull Request), requesting your remote feature branch to be merged to the remote main branch, said request will be trivial to solve (since your feature branch will just add new commits on top of the remote main).

    The remote tracking branch origin/main was modified by git fetch (using a default refspec +refs/heads/*:refs/remotes/origin/*)