Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will my branch always be created off of the master branch in Git

when i create a branch in Git, do i have to be on the master branch? or will it automatically create the branch from master?

For example, if I'm on a branch called "branch1" and I create a branch like this:

git branch branch2
git checkout branch2

will branch2 have contents from branch1, or will it be a replica of the master branch?

If branch2 is created off of branch1, will I run into conflicts if I merge both branch1 and branch2 into master?

like image 794
DanielD Avatar asked Feb 11 '23 06:02

DanielD


2 Answers

Technically, a branch is not based on a branch, but from a commit. You can branch from anywhere in history, not only from the tip of a branch. And if you branch from say master, Git won't remember that, it will just remember where your new branch is.

To create and check out a branch from the current commit, run

git checkout -b new-branch

To create and check out a branch from any commit, run

git checkout -b new-branch base-commit
like image 199
Matthieu Moy Avatar answered Feb 13 '23 02:02

Matthieu Moy


git branch will create a branch from the active brach. In your case, it will branch off from branch1.

You will run into conflicts whenever the branches you want to merge changed the exact same part of the same file. If that's the case, then you won't be able to automatically merge.

like image 21
vsoftco Avatar answered Feb 13 '23 04:02

vsoftco