Paul
Paul

Reputation: 620

Creating new branches in GIT and carrying over code changes

When using GIT i normally create feature branches off an updated master branch then once i have completed work i merge the feature branch back in to the remote master branch.

To do this i normally switch to the local master branch, do a git pull and use git checkout -b 'myFeatureBranchName' which will create & switch me to a new local branch. I do my changes commit and push then merge back into to master.

However, in this instance i have switched to master done a git pull but forgot to create and switch to a new branch. Is there a command that will allow me to create & switch to a new feature branch and also carry over all my changes to the new branch?

Thanks in advance

Upvotes: 3

Views: 1709

Answers (2)

bork
bork

Reputation: 1674

You might want to check out this question: Move the most recent commit(s) to a new branch with Git

Pro tip: Never work on the master branch. Branch it off to a 'develop' branch, and create your feature branches from there.

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521194

Your question actually contains the answer to your question. You can use this command:

git checkout -b 'myFeatureBranchName'

This will create a new feature branch from master containing all of your work. You can push this branch to the remote, issue a pull request, and merge in as normal.

However, there is one lingering issue about what to do with your local master branch, since it will now contain new commits which really belonged only in the feature branch. Here are two options you can consider:

One is to rollback master to before you started making commits for the feature. You can nuke the commits in master which don't really belong there. If there were 3 commits in master, you would do this:

git reset --hard HEAD~3

The second option is to delete your local master branch and then simply do a git pull to get a fresh one from the remote:

git branch -d master
git checkout --track -b origin/master

In either case, you now have your feature branch pushed out to GitHub and your local master is in the correct state.

Upvotes: 4

Related Questions