Git- Managing branches
Tags: git, github
Published: 2020-04-22 22:13:14

> At some point in any software project, you will need to share your code with other developers. If you’re using Git for source control, there are three primary options: Github, Bitbucket, or Gitlab. Understanding the differences and tradeoffs between these three repository management platforms is vital to choosing the best option for your team.

#Managing branches

#### Create and push a new branch
 - Before creating a new branch, pull the changes from upstream. Your master needs to be up to date.

    <pre>$ git pull</pre>

 - Create the branch on your local machine and switch onto this new branch 

    <pre>$ git checkout -b [name_of_your_new_branch]</pre>
    
    ######Example 
      ```jshelllanguage
        $ git checkout -b my-new-branch
        Switched to a new branch 'my-new-branch'
      ``` 
 

 - verifying the newly created branches 

    <pre>$ git branch -a</pre>
    
    ######Example 
    <pre>
        $ git branch -a
          master
        * my-new-branch
          remotes/origin/HEAD -> origin/master
          remotes/origin/master</pre>

 - Make changes in this new branch and push the new branch on github 
 
    For most of the cases when you try to push for the first time using `git push` you will get something like this
    
    ```jshelllanguage
    $ git push
    fatal: The current branch my-new-branch has no upstream branch.
    To push the current branch and set the remote as upstream, use
    
        git push --set-upstream origin my-new-branch

    ```
    As it is clear that our new branch is still currently in local and has not remote tracking branch so we need to explicitly push this new branch 
    to set a remote tracking to it.
    
    `-u` OR `--set-upstream`
        
    <pre>$ git push origin -u [name_of_your_new_branch]</pre>
    
    ######Example 
        <pre>
            $ git push origin -u my-new-branch
            Enumerating objects: 4, done.
            Counting objects: 100% (4/4), done.
            Delta compression using up to 4 threads
            Compressing objects: 100% (2/2), done.
            Writing objects: 100% (3/3), 273 bytes | 273.00 KiB/s, done.
            Total 3 (delta 1), reused 0 (delta 0)
            remote:
            remote: To create a merge request for my-new-branch, visit:
            remote:   https://gitlab.com/<username>/<repo-name>/-/merge_requests/new?merge_request%5Bsource_branch%5D=my-new-branch
            remote:
            To https://gitlab.com/<username>/<repo-name>.git
             * [new branch]      my-new-branch -> my-new-branch
            Branch 'my-new-branch' set up to track remote branch 'my-new-branch' from 'origin'.</pre>

Add a new remote for your branch : 

<pre>$ git remote add [name_of_your_remote] [name_of_your_new_branch]</pre>

Push changes from your commit into your branch :

<pre>$ git push [name_of_your_new_remote] [url]</pre>

Update your branch when the original branch from official repository has been updated : 

```sh
$ git fetch [name_of_your_remote]
```
Then you need to apply to merge changes if your branch is derivated from develop you need to do : 

<pre>$ git merge [name_of_your_remote]/develop</pre>

Delete a branch on your local filesystem : 

<pre>$ git branch -d [name_of_your_new_branch]</pre>

To force the deletion of local branch on your filesystem : 

<pre>$ git branch -D [name_of_your_new_branch]</pre>

Delete the branch on github : 

<pre>$ git push origin :[name_of_your_new_branch]</pre>

The only difference is the: to say delete, you can do it too by using GitHub interface to remove branch: https://help.github.com/articles/deleting-unused-branches.

If you want to change default branch, it's so easy with GitHub, in your fork go into Admin and in the drop-down list default branch choose what you want.

If you want create a new branch:
```sh
$ git branch <name_of_your_new_branch>
```