Branching
Branching
Branching is used to develop features isolated from others.
By default, git creates a master branch. This branch is considered the root and should ultimately contain all code changes.
The idea is that you want to create a new feature but you don't want to check directly into the master branch. Or let's say you are working on a prototype and not really sure if it's going to work out.
Creating a new branch
To create a new branch:
git branch MyNewBranch
To switch to that branch:
git checkout MyNewBranch
Or to create a new branch and switch to it:
git checkout -b MyNewBranch
Viewing branches
To see what branches are local and remotely:
git branch -a
Output:
* master remotes/origin/HEAD -> origin/master remotes/origin/master
The * indicates the current branch. The remotes/origin/master is the remote copy of your local branch.
Deleting a local branch
To delete the branch locally:
git branch -d MyNewBranch
Deleting a remote branch
To delete the branch on the remote:
git push origin :MyNewBranch
Merging your branch
After you've completed your feature work, you'll want to merge your branch back into master.
NOTE: This is assuming all your changes are committed.
-
This gets any changes from any other team members working on your branch.
git pull
-
This assures that your remote branch has all of your local changes.
git push
-
This gets all changes from master.
git checkout master
git pull
-
git merge MyNewBranch
-
git branch -d MyNewBranch
git push origin :MyNewBranch
Links
Exercises
- TBD