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.

  1. This gets any changes from any other team members working on your branch.

    git pull
  2. This assures that your remote branch has all of your local changes.

    git push
  3. This gets all changes from master.

    git checkout master
    git pull
  4. git merge MyNewBranch
  5. git branch -d MyNewBranch
    git push origin :MyNewBranch

Links

Exercises

  1. TBD
    
Copyright 1999-2021 by Cstructor LLC. All rights Reserved.