0

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?

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 1
    Why do you "commit" your branch creation? Nothing to commit here, branches are references to commits stored in the repo itself, not in the commits. – Romain Valeri Jul 05 '21 at 08:15
  • Because when I don't "commit" it doesn't create globally, only locally on my computer, so when I push changes from "settings" - develop doesn't creates. If you now how to make it correct - please tell me. – den maccormik Jul 05 '21 at 08:22
  • You answered your first question yourself by doing `git checkout -b develop main`. For the second question, have a look at [this answer](https://stackoverflow.com/a/6089415/15744300). Also, `git help checkout` etc has plenty of information. – Jan Wilamowski Jul 05 '21 at 08:24
  • Thank you guys, I got it. And one more question - is it possible to rename "main" branch without deleting it's children? – den maccormik Jul 05 '21 at 08:46
  • @denmaccormik Yes, branches are completely independent from one another. The "parent" concept is purely logical, nothing in the repo itself "links" branches together in any way. – Romain Valeri Jul 05 '21 at 08:56

2 Answers2

2

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.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
0

Why you want to push if you create and checkout a branch? You can only push file-changes, but not branches...

If you want to create the branch settings, you can simply execute following, if you stay in that branch, where you want to create settings (in your case in branch develop -> git checkout develop to switch to develop):

git checkout -b settings

If you aren't in the develop branch, you can execute this with the following approach:

git checkout -b <new branch> <destination branch>

And in your case:

git checkout settings develop

With following, you create the branch also remotely:

git push <remote-name> <branch-name> 

Now you are able, to change files and push this changed files to remote branch settings

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34