Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Edwin Torres
Edwin Torres

Posted on • Edited on

     

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 namedmy-repo.
  • Your repo has one branch namedmaster.
  • 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 namedmy-branch from yourmaster branch. This makes a copy of themaster 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 themaster branch and the working tree isclean:

    $cdmy-repo$ git status# On branch master? working tree clean?$
  2. Create a new local branch frommaster and name itmy-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 intomaster:

    $ 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 mergedmy-branch intomaster, you may not needmy-branch anymore. If you want to deletemy-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 deletemy-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 Gitwebsite.

Thanks for reading!

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

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Department Chief Engineer @MITREcorp. CS professor @monmouthu. Proud husband and father ❤️. My tweets are my own.
  • Location
    NJ
  • Education
    Doctor of Engineering, George Washington University
  • Work
    Principal Software Engineer at MITRE
  • Joined

More fromEdwin Torres

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp