Reputation: 24998
I am learning how to use Git for version control. One of the uses of creating branches is to add new features while master
branch contains the stable code. I am using Git Bash
for Windows in conjunction with Eclipse for editing the code.
So, assuming that I want to create a new feature called social-share
and a branch named the same, how would I go about doing that?
Wont I first have to create a branch, commit the entire master
branch to it and then add new feature code?
Upvotes: 1
Views: 279
Reputation: 38856
So I'm assuming that you already have a branch called develop
where your development occurs.
The first thing to do is create the feature branch based on the current branch and switch to the created branch
$ git checkout -b social-share
Optionally if you want to push that branch to a remote Git repo to track it there:
$ git push -u origin social-share
Pushing the branch to the remote repo is dependent on your environment and whether or not you want others to work on this branch.
Once you've finished your changes to the social-share
branch you'll want to merge those back into your develop
branch.
First switch to develop
branch
$ git checkout develop
Merge social-share
into devleop
*
$ git merge --no-ff social-share
Delete social-share branch
$ git branch -d social-share
Push changes to origin
$ git push origin develop
* The reason you use the --no-ff
flag is so the merge uses a new commit object and to avoid just fast-forwarding the develop
branch which preserves the fact that the social-share
branch existed at one point.
If you want to read more about this branching model then checkout the post it was based on. Also, if you would like to learn more about git branching by actually doing it there's a great tutorial series/sandbox that you should checkout (git it?).
Upvotes: 1