1

https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches

I would like to confirm that at the end of the "Pushing" section on the above page,

(1) Execute git merge origin/serverfix after fetch

(2) Run git checkout -b serverfix origin/serverfix

Am I correct in understanding that operation (1) and (2) have the same effect after all?

nullptr
  • 3,701
  • 2
  • 16
  • 40
森口万太郎
  • 805
  • 6
  • 20

3 Answers3

3
  • git checkout -b creates a different branch and switches you to it. git checkout -b serverfix origin/serverfix specifies that the new local branch serverfix should point to the same commit as the serverfix of the origin remote.

  • git merge merges changes from the specified commit into your current branch, updating your current branch.

They’re completely different. You’ll be on different branches after executing them, and may not even have the same HEAD content.

Ry-
  • 218,210
  • 55
  • 464
  • 476
1

Yes, there is a difference; no, they don't have the same effect.

As the linked document explains, git merge origin/serverfix will merge the remote branch "origin/serverfix" with whichever branch is currently checked out (the current working branch), while git checkout -b origin/serverfix will create a new local branch named "serverfix" based on the remote "origin/serverfix" and then check out the new branch.

outis
  • 75,655
  • 22
  • 151
  • 221
  • @森口万太郎: note that [comments](/help/privileges/comment) should only address specific things, and "thank you" comments are discouraged. Instead, if someone provides a useful [answer](/help/someone-answers), you can upvote it. If one answer resolves your issue, you can [accept](/help/accepted-answer) it. Please read through the [help] for more information on how SO operates. – outis Oct 19 '22 at 09:48
1

The order is important:

First you create a topic branch. Since Git 2.23 (Q3 2019), the right command would be git switch -c:

git switch -c serverfix 

Since git switch has (like git checkout before) a guess mode, this is equivalent to:

git switch -c <branch> --track <remote>/<branch>

The other command is to merge back the topic branch work to your current branch.
If that current branch was your own version of serverfix, a simple git pull would be enough (short for "git fetch + git merge origin/serverfix")

But if you want to directly merge a remote branch to your current branch:

git fetch
git switch main
git merge origin/serverfix
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250