Home > Software engineering >  Merge/rebase remote tracking branch
Merge/rebase remote tracking branch

Time:04-25

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)?

CodePudding user response:

can we modify the remote tracking branch by any other git commands (locally)?

Both push and fetch (which is what pull is) modify remote tracking branches. You cannot do it any other way, because the entire purpose of a remote tracking branch is (wait for it) to track the remote, ie to match it perfectly. If you could do anything to upset that match, by editing a remote tracking branch directly without communicating with the remote, your repo would break.

CodePudding user response:

. 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/*)

  • Related