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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With