Simple Git Branching Tutorial
Tags: git, github, beginners, tutorial
Published: 2022-01-14 16:22:45

---
title: Simple Git Branching Tutorial
published: true
description: A simple tutorial on Git branching
tags: #git, #github, #beginners, #tutorial
//cover_image: https://direct_url_to_image.jpg
---
## Git Branching Tutorial

Here is a quick tutorial on using branches in Git.

### Assumptions

* You already have a GitLab/GitHub repo. This tutorial uses a repo named `my-repo`.
* Your repo has one branch named `master`.
* You already cloned the repo to your local computer.
* Your repo is clean: `git status`

### Tutorial

Here is a terminal session that demonstrates Git branching. You will create a new branch named `my-branch` from your `master` branch. This makes a copy of the `master` branch. Then, you will make changes to the new branch and push it to your remote GitLab/GitHub server.

1. From your local computer, open a terminal and go to your local repo and make sure you are on the `master` branch and the working tree is _clean_:
    ```bash
    $  cd my-repo
    $  git status  # On branch master? working tree clean?
    $
    ```

1. Create a new local branch from `master` and name it `my-branch`:
    ```bash
    $  git checkout -B my-branch  # create new branch
    $
    $  git status  # you are on the new branch now
    $
    $  ls  # this branch has the same files as master
    $
    ```
1. Switch back and forth between branches. Note that you stay in the same folder:
    ```bash
    $  git checkout master
    $  git status # now on master branch
    $
    $  git checkout my-branch
    $  git status # now on my-branch
    $
    $  # when you switch branches, you stay in the same folder. Git changes the branch files for you:
    $  pwd
    $
    $  # shortcut to switch to the previous branch
    $  git checkout -
    $
    ```
1. Add some files to your new branch:
    ```bash
    $  git checkout my-branch
    $  echo "print('hello')" > hello.py  # create a new file
    $
    $  git status  # changes pending in new branch
    $
    $  # commit files to your new branch locally
    $  git add .
    $  git commit -m "changes to my new branch" .
    $
    ```
1. You created and updated your new branch locally, but it is not yet in the remote GitLab/GitHub server. So, push your local branch to the remote:
    ```bash
    $  git checkout my-branch
    $  git push origin my-branch  # push to the remote
    $
    $  # From your browser, look at your new branch in GitHub/GitLab
    $  # The master branch is the default, so manually select the my-branch branch to see it
    $
    ```
1. Now your new branch exists locally on your computer and remotely in GitLab/GitHub. You can keep your local my-branch branch if you need to make future changes to that branch.
1. To merge your new branch back into `master`:
    ```bash
    $  git checkout my-branch
    $  git status  # make sure it is clean
    $
    $  git checkout master
    $  git status  # make sure it is clean
    $
    $  # do the merge
    $  git checkout master  # check out branch we are merging into
    $  git merge my-branch  # merge new branch into master
    $  git push origin master  # push the updated master branch to the remote
    $
    ```
1. Now that you merged `my-branch` into `master`, you may not need `my-branch` anymore. If you want to delete `my-branch` from your local computer:
    ```bash
    $  git checkout master  # you cannot be on the branch you are deleting
    $  git branch -D my-branch # delete my-branch locally
    $
    ```
1. To also delete `my-branch` from the remote GitLab/GitHub server:
    ```bash
    $  git checkout master
    $  git push origin --delete my-branch  # deletes my-branch from the remote
    $
    ```
This ends this Git branching tutorial. Use branches to perform parallel work. Delete local branches if you no longer need them. Delete remote branches if you no longer need them, or you've merged them into another long-term branch.

For more information and original content for this blog, see the Git [website](https://git-scm.com/).

Thanks for reading!

Follow me on Twitter [@realEdwinTorres](https://twitter.com/realEdwinTorres) for programming tips, software engineering content, and career advice. 😊