Search code examples
gitbranchcommit

How to create branches correctly GIT


I want my "settings"-branch to be a child of the develop-branch.

We always start from the "main"-branch.

So I create a new develop-branch

git checkout -b develop main  
git commit -m "created develop" 
git push 

but it doesn't allow me to simply push, GIT says that I need to write

git push --set-upstream origin develop  

1st question: does that mean that origin - is our main and we create "develop" from main?

so when I use:

git push --set-upstream origin develop
git checkout -b "setting"  

am I creating this setting from develop?
or I am creating it from main again???) and what command I need to use to be develop parent of settings

How to make one branch be the parent of another branch. And what --set-upstream origin does?


Solution

  • I think the key misunderstanding here is about what a branch is. Maybe you've used a different version control system in the past, or have formed an incomplete picture in your head.

    In git, a branch is simply a label pointing at a set of commits. More strictly, it points at a single commit, the "tip" of the branch; that commit then points to its parent or parents, and the history can be constructed backwards. Creating a new commit "on a branch" consists internally of first creating the commit, and then moving the branch pointer to point at that commit.

    There are a few things that follow from this:

    • Branches have no relationship with each other. At the time you create branch A, it might point at the same commit as branch B, but later both have moved on to different "tips". There is no record in git of whether A or B was the "original" or "parent" branch.
    • Creating a branch doesn't need to involve any new commits. You are just creating a new pointer to a particular commit, which you will then move by making new commits (or using other specific commands).

    The other important thing is to understand that the branch pointers on your local computer and the pointers with the same name on a remote server (like Github or Gitlab) can move independently. Most simply, if you unplug your computer from the internet, you can carry on creating commits, and completely new branches. When you connect again, you can synchronise them using something called a "remote"; the default remote is called "origin"

    With this in mind, we can explain a couple of commands:

    git checkout -b develop main  
    

    This creates a new branch pointer called "develop", initially pointing at the same place as "main", and then make that our currently checked out branch (the one new commits will add to). There is no on-going relationship between "develop" and "main"; they just happen to point at the same commit right now.

    git push --set-upstream origin develop
    

    This talks to the remote server configured with the name "origin", and tells it about your new branch pointer called "develop". The "--set-upstream" is just a convenience: it sets "origin" as the default remote for this branch, so you can run "push" and "pull" commands without typing "origin" every time.