=====================
Branching and merging
=====================

Each project in Data Science & AI Workbench has its own Git repository that the files for the project are stored in. You can create branches off of your project to develop changes locally, then commit and push them to a collaborative remote project repository, where other users will then be able to pull your changes.

Creating a branch
=================

New branches are *always created off of the currently active branch*. 

From the |file-code| **Projects** page: 

#. Open your project and start a session.
#. Open the |branch| branch dropdown menu. 
#. Click |plus| *Create branch*.
#. Provide a name for your branch.
#. Click **Create**.

.. tip:: 
    
    The current active branch and its parent branch are always identified in the branch dropdown. 

Switching between branches
==========================

You can switch between branches that exist in your project using the |branch| branch dropdown menu. 

#. Open the branch dropdown menu. 
#. Select the branch you want to work in.

.. caution:: 
    
    Your working branch must be clean to switch between branches. :ref:`Commit and push <commit_push>` your work before switching branches! 

.. note:: 
    
    You may need to refresh your editor view for it to detect the most recent changes. 

.. _commit_push:

Pushing changes to a branch
===========================

#. Click |cloud-arrow-up| **Commit and push changes**.
#. Select the files you want to commit to the remote branch from your local branch.
#. Enter a commit message for your changes. 
#. If necessary, tag a version for your commit.
#. Click **Commit and Push**.

Merging changes to a parent branch
==================================

When the changes on your branch are ready, you can merge them into the parent branch using the branch dropdown menu.

#. Open the branch dropdown menu.
#. Click |merge| *Merge changes into parent branch*.
#. Click **Merge**. 

Pulling changes from a parent or remote branch
==============================================

If the parent or remote branch has changes applied to it that the active branch does not, you must pull the changes into your local branch to avoid merge conflicts before you push your commits. 

#. Click the **Pull** |cloud-arrow-down| icon. 
#. Select the branch that contains changes you need to pull.
#. Open the **Pull** dropdown and select a pull method:

    Keep Theirs and Pull
        Discards your local changes in favor of content on the parent branch. Your changes will be lost.

    Keep Mine and Pull
        Discards changes on the parent branch in favor of your local changes. Changes on the parent branch will be overwritten. 

    Keep Both and Pull
        Saves the conflicting files with different file names so you can compare their content and decide how you want to reconcile the differences manually.

Deleting a branch
=================

There is currently no method for deleting a branch from the UI. This feature will be added in a later release. However, you can still delete branches from your project manually if necessary. To delete a branch from a project: 

#. Open your project and start a session.
#. Open a terminal in your project session. 
#. Run the following commands: 

    .. code-block:: none

        # Replace <BRANCH> with the name of the branch you want to delete
        git branch -D <BRANCH>
        git push origin :<BRANCH>

    .. caution::

        If you attempt to delete a branch that another user currently has active, the branch will not be deleted.

    .. note:: 

        You can execute other Git commands from the project's terminal as well. However, Git actions performed at the command line are not recorded in your user activity log.

            .. thumbnail:: ../../images/git_command_no_record.png
                :align: center

.. |cloud-arrow-up| raw:: html

    <i class="fa-light fa-cloud-arrow-up"></i>

.. |cloud-arrow-down| raw:: html

    <i class="fa-light fa-cloud-arrow-down"></i>

.. |merge| raw:: html

    <i class="fa-light fa-merge"></i>

.. |plus| raw:: html

    <i class="fa-light fa-plus"></i>

.. |branch| raw:: html

    <svg height="1em" width="1em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M80 112a32 32 0 1 0 0-64 32 32 0 1 0 0 64zm80-32c0 35.8-23.5 66.1-56 76.3V256c20.1-15.1 45-24 72-24h96c39.8 0 72-32.2 72-72v-3.7c-32.5-10.2-56-40.5-56-76.3c0-44.2 35.8-80 80-80s80 35.8 80 80c0 35.8-23.5 66.1-56 76.3V160c0 66.3-53.7 120-120 120H176c-39.8 0-72 32.2-72 72v3.7c32.5 10.2 56 40.5 56 76.3c0 44.2-35.8 80-80 80s-80-35.8-80-80c0-35.8 23.5-66.1 56-76.3V352 156.3C23.5 146.1 0 115.8 0 80C0 35.8 35.8 0 80 0s80 35.8 80 80zm240 0a32 32 0 1 0 -64 0 32 32 0 1 0 64 0zM80 464a32 32 0 1 0 0-64 32 32 0 1 0 0 64z"/></svg>

.. |file-code| raw:: html

   <svg class="inline" height="1.2em" width="1.2em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path d="M64 496c-26.5 0-48-21.5-48-48V64c0-26.5 21.5-48 48-48H204.1c1.3 0 2.6 .1 3.9 .2V136c0 22.1 17.9 40 40 40H367.8c.2 1.3 .2 2.6 .2 3.9V448c0 26.5-21.5 48-48 48H64zM358.6 157.3c.9 .9 1.7 1.8 2.4 2.7H248c-13.3 0-24-10.7-24-24V22.9c1 .8 1.9 1.6 2.7 2.4L358.6 157.3zM64 0C28.7 0 0 28.7 0 64V448c0 35.3 28.7 64 64 64H320c35.3 0 64-28.7 64-64V179.9c0-12.7-5.1-24.9-14.1-33.9L238.1 14.1c-9-9-21.2-14.1-33.9-14.1H64zm85.7 269.7c3.1-3.1 3.1-8.2 0-11.3s-8.2-3.1-11.3 0l-56 56c-3.1 3.1-3.1 8.2 0 11.3l56 56c3.1 3.1 8.2 3.1 11.3 0s3.1-8.2 0-11.3L99.3 320l50.3-50.3zm96-11.3c-3.1-3.1-8.2-3.1-11.3 0s-3.1 8.2 0 11.3L284.7 320l-50.3 50.3c-3.1 3.1-3.1 8.2 0 11.3s8.2 3.1 11.3 0l56-56c3.1-3.1 3.1-8.2 0-11.3l-56-56z"/></svg>