0

I am new to GIT & in learning phase of GIT Concepts. I have two simple & quick questions.

Q1. Whenever we want to create a new branch, we type below command:

git checkout -b BranchName

Here, my question is that git checkout consider which branch as it's source branch while creating new branch?

  • Does it consider master branch as source branch?
  • Does it consider current branch as source branch?

Q2. Suppose, whatever branch which it is considering as source branch is not up to date from remote. So while creating a new branch from that source branch, does it create a pull for the source branch & then create new one OR it considers local branch as it's base and doesn't pull anything from server?

Ankush Jain
  • 3,773
  • 3
  • 21
  • 42
  • 1
    You will create a branch from your current branch and if it is not up to date your new branch won't have any new commits as well. So first `git pull` on your current branch - then create a new one and. – ikos23 May 12 '18 at 21:25
  • Ok..so it means source branch will be my current branch & best practice is to first pull your current branch & then create new one. But I did not get your point "if it is not up to date your new branch won't have any new commits as well". Could you please explain it? – Ankush Jain May 12 '18 at 21:30
  • 1
    Let's say you have branch A with commits #1, #2, #3 (imaginary hashes). It's your local branch. Your team member adds new stuff and pushes to the remote. This will be commit #4. You don't have it in your local branch until `git pull`. So if you create a new branch `B` and don't do `git pull` on A first, you won't have the latest changes (#4) in `B`, because your local A will be used as a source and it is not up-to-date with the remote. Does it make any sense ?) – ikos23 May 12 '18 at 21:42
  • ohh...what a great explaination @john...thanks very much...:) – Ankush Jain May 12 '18 at 21:46
  • What do you think "source branch" means? It doesn't mean anything in Git. – jthill May 13 '18 at 01:27
  • @AnkushJain Have your problems been solved yet? – Marina Liu May 15 '18 at 07:06
  • yes...@MarinaLiu-MSFT – Ankush Jain May 15 '18 at 07:11
  • @AnkushJain So you can mark the answer (by clicking √ symbol on the left of the answer) which helps you solve the problem. And it will also benefit others who meet similar questions :) – Marina Liu May 16 '18 at 06:27
  • @MarinaLiu-MSFT - none of the answer helped me here..it is the comment by john..which helped me here. – Ankush Jain May 16 '18 at 17:37
  • @AnkushJain So you can add the answer by yourself and mark it. – Marina Liu May 17 '18 at 09:16

2 Answers2

1

There is no notion of source branch. The branch is what we call a 'ref', a pointer toward a commit.

So when you create a branch, the branch point toward the commit you are on (or indirectly, the first commit pointed by the current branch you are on).

If you don't specify a specific commit via a has or a refspec, git will create the branch on the commit with the following hash that you could get with the command:

git rev-parse HEAD
Philippe
  • 21,230
  • 5
  • 41
  • 62
0

It will consider as source your current branch or use one more arg:

git checkout -b BranchName StartPoint

Note: below I answer to Q2:

If the start point is not up to date and you did not commit anything locally, you can pull, otherwise you could rebase it (alternatively you can merge, still using the pull). git fetch --all refreshes history from the remote You may be a few commits behind. Then git rebase origin/master (or whatever the branch) thus updating it and putting your commits on top.

Attersson
  • 4,127
  • 1
  • 11
  • 27