Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating git branch after many commits

I have a repo on GitHub with a single branch master that I've been working on in my local repo. At some point I stopped pushing commits back up to master on GitHub because I was worried that they'd break things. Now I have lots of commits in my local repo that I want to push back up to GitHub.

However, rather than pushing back up to master I would prefer to create a new branch on GitHub (development) and push all my local commits back up to that branch (which I will merge back into master only after they've been better tested).

What is the simple way to do this?

like image 410
Herbert Sitz Avatar asked Mar 17 '11 18:03

Herbert Sitz


People also ask

Should I create a new branch for every commit?

Generally create a branch for every feature you're working on. Commit all your changes there. Then when you're done, merge it (pull request or not) to wherever it needs to go.

How do you create a new branch from a particular commit?

First, checkout the branch that you want to take the specific commit to make a new branch. Then look at the toolbar, select Repository > Branch ... the shortcut is Command + Shift + B. And select the specific commit you want to take. And give a new branch name then create a branch!

How do I create a new branch in git?

New Branches The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.


1 Answers

on master:

git checkout -b newbranch or as suggested below, simply do a git branch newbranch to create the new branch without switching to it.

This will create a branch that is at your current commit at master. Once done:

git checkout master

followed by:

git reset --hard <commit_hash>

Where <commit_hash> should be replaced by the commit ID you want master rolled back to.

Now you can switch to your new branch and push it out to the remote.

git checkout newbranch
git push origin newbranch
like image 190
Grant Limberg Avatar answered Oct 14 '22 17:10

Grant Limberg