Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Git branch from another branch or from master?

So I am new to Git. I recently pulled a fresh version of the master branch from the repo.

I created a branch(1) for a feature and pushed it to the repo and created up a pull request. Now I created a new branch(2) with another feature, but since my pull request hasn't merged yet, pulling master again won't have that feature that I created in branch1.

Only when I create a new branch from branch1 I get that feature. Before, I've been created new branches from the previous branches so I haven't had this issue, but I've been told that although it isn't a big deal, they want me to create new branch from the master instead.

What would git command would I have to do? Git merge on my local master?

like image 806
UsernameNotFound Avatar asked Sep 12 '25 00:09

UsernameNotFound


2 Answers

There are a couple ways to do this.

If you just want to create and check out a new branch from master without checking it out, you can do git checkout -b new-branch master. If, instead, you'd prefer not to check it out, but just create it, you can run git branch new-branch master.

If you are already on master, perhaps since you're pulling in new changes from the remote, then you can use git checkout -b new-branch or git branch new-branch, depending on whether you want to check it out immediately or not.

When I'm creating new branches, I usually go to the main branch, run git pull --ff-only origin main to update my main branch, and then use git checkout -b new-branch to create and check out my new branch so I can work on it. If you're not sure what to do, I recommend that approach.

like image 117
bk2204 Avatar answered Sep 13 '25 14:09

bk2204


I would start all branches from the same branch (root branch, in your case the master branch). So if branch1 is in a pull request, you start with branch2 from the master branch. If the pull request from branch1 is good (approved) and has been merged into the master branch, you can merge the master branch into your branch2 so that you then have the latest data from the master branch in the current branch2.

But if the work in branch2 is based on the changes where are in branch1, branch2 must be based on branch1, and then branch2 can also be started from branch1. If branch1 is merged in master, the source branch of branch2 is master then branch1 was merged in master.

Look at this stackoverflow-question.

like image 39
SwissCodeMen Avatar answered Sep 13 '25 15:09

SwissCodeMen