Managing Branches

Home

Create a new branch

Create a new branch is easy.
There're two methods to create a branch.
I'll show you the easiest.

So, we want to create a branch to fix the logout of our site.
I'll name it fix/logout...

You should have a Branch Naming Convention

git checkout -b fix/logout

We're now on the fix/logout branch!
Push it the remote repo:

git push --set-upstream origin fix/logout

Change branch

To begin, we have to know the branches we have and on which one we are:

$ git branch
* develop
  feature/AddComments
  feature/Unknown
  fix/logout
  master

We have 4 branches:

The asterisk shows us the branch we're currently on.

Now that we know where we are, we can choose where we're going: I choose feature/AddComments.

We only have to write this command:

git checkout feature/AddComments

And we are on the branch!

Delete a branch

The deletion of branches is easy, but we have to be careful: a branch have to be deleted locally (on your PC) and remotely (e.g. on GitHub).

We are on feature/AddComments and, for some reason, we abandon the functionality. We should delete its branch then.

First, we have to change branch. Let's say we go to develop:

git checkout develop

We can delete the local branch:

git branch -d feature/AddComments

Now we delete the remote one:

git push origin -d feature/AddComments

Merge two branches

So, we are on fix/logout and we have to merge it with develop.
To do so, we have to:

  1. return to develop
git checkout develop
  1. merge the two branches
git merge --no-ff fix/logout
  1. delete fix/logout (locally)
git branch -d fix/logout
  1. push the changes
git push -u origin develop
  1. delete fix/logout (remotely)
git push origin -d fix/logout

Rename a branch

There're only three branches left:

$ git branch
* develop
  feature/Unknown
  master

We're going to work on that unknown feature but we should rename it.
Indeed the name isn't representative of our feature...

As we want to add articles to the site, let's call it feature/AddArticles.
Because we are on develop, the command is:

git branch -m feature/Unknown feature/AddArticles

If we were on feature/Unknown, the command would be:

git branch -m feature/AddArticles

Now we have to push the changes:

git push origin :feature/Unknown feature/AddArticles

Then we push the new branch and connect the remote and local branches:

git push --set-upstream origin feature/AddArticles

ToolKit © 2017
About