DEV Community

ashutosh049
ashutosh049

Posted on

Git- Managing branches

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.

    $ git pull
  • Create the branch on your local machine and switch onto this new branch

    $ git checkout -b [name_of_your_new_branch]
    Example
        $ git checkout -b my-new-branch
        Switched to a new branch 'my-new-branch'
    
  • verifying the newly created branches

    $ git branch -a
    Example
        $ git branch -a
          master
        * my-new-branch
          remotes/origin/HEAD -> origin/master
          remotes/origin/master
  • 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

    $ 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

    $ git push origin -u [name_of_your_new_branch]
    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 :

$ git remote add [name_of_your_remote] [name_of_your_new_branch]

Push changes from your commit into your branch :

$ git push [name_of_your_new_remote] [url]

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

$ git fetch [name_of_your_remote]
Enter fullscreen mode Exit fullscreen mode

Then you need to apply to merge changes if your branch is derivated from develop you need to do :

$ git merge [name_of_your_remote]/develop

Delete a branch on your local filesystem :

$ git branch -d [name_of_your_new_branch]

To force the deletion of local branch on your filesystem :

$ git branch -D [name_of_your_new_branch]

Delete the branch on github :

$ git push origin :[name_of_your_new_branch]

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:

$ git branch <name_of_your_new_branch>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)