Simple Git Branching Tutorial

realedwintorres

Edwin Torres

Posted on January 14, 2022

Simple Git Branching Tutorial

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:

    $  cd my-repo
    $  git status  # On branch master? working tree clean?
    $
    
  2. Create a new local branch from master and name it my-branch:

    $  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
    $
    
  3. Switch back and forth between branches. Note that you stay in the same folder:

    $  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 -
    $
    
  4. Add some files to your new branch:

    $  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" .
    $
    
  5. 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:

    $  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
    $
    
  6. 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.

  7. To merge your new branch back into master:

    $  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
    $
    
  8. 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:

    $  git checkout master  # you cannot be on the branch you are deleting
    $  git branch -D my-branch # delete my-branch locally
    $
    
  9. To also delete my-branch from the remote GitLab/GitHub server:

    $  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.

Thanks for reading!

Follow me on Twitter @realEdwinTorres for programming tips, software engineering content, and career advice. 😊

💖 💪 🙅 🚩
realedwintorres
Edwin Torres

Posted on January 14, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related