Nobody
Nobody

Reputation: 143

How to create a branch and reset another with git?

I am working on a project on GitHub. On my computer I made six commits that I have not yet pushed to the GitHub server. Now I realize that I want to keep the current master branch that is in the cloud, and put the commits in a separate branch called development. How can I do this? I am new to Git and GitHub.

EDIT: I also have a few commits that have been pushed to GitHub from before the unpushed commits. I want to keep these in both branches, whie only keeping the unpushed commits in development.

Upvotes: 2

Views: 645

Answers (3)

Robert Siemer
Robert Siemer

Reputation: 34727

The first thing you want to do is create a branch at the point where you are and "be" on it. git checkout is the only command which can change the current branch. And its -b option creates a branch on top (at the point you are at the moment, if you don't tell it otherwise):

git checkout -b development

Then you want to set local branch master back to where the remote is. This you do by setting it to the so-called "local remote-tracking branch", which is a snapshot of the remote branch (from the time of last fetch/push):

git branch --force master origin/master

You have to use --force here, because otherwise git branch will not change existing branches.

Upvotes: 1

harry
harry

Reputation: 310

assuming you have made the commits to your local master branch; create a new branch in local and call that development

git branch development
git checkout development

your commits would be there in this new branch and you can push to cloud aka remote in git world

git push origin development

please make sure you are not doing

 git push origin master

if so the remote/cloud master would have your commits; which I believe you don't need

Upvotes: 0

Mad Physicist
Mad Physicist

Reputation: 114440

Make a new branch where you are called development:

git branch development

Then move master to where it is on the remote:

git checkout master
git reset --hard origin/master

Now development points to your last commit while master is where it was before the commits.

You can push the new branch and set up remote tracking (so push and pull work with no additional arguments) with

git push -u origin development 

Upvotes: 2

Related Questions