5

Simple Git Branching Tutorial

 2 years ago
source link: https://dev.to/realedwintorres/simple-git-branching-tutorial-3n0f
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Git Branching Tutorial

Here is a quick tutorial on using branches in Git.

Assumptions

  • You already have a GitLab/GitHub repo, for example: my-repo.
  • Your repo has one branch named master.
  • You already cloned the repo to the local computer.

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.

Pay attention to the # comments. Enter the following commands on your local computer.

$  # go into your repo; substitute your repo name here
$  cd my-repo  # substitute your repo/folder name here
$
$  # make sure you are on the master branch and the status is clean 
$  git status
$
$  # create a new local branch from my-repo; name it: my-branch
$  git checkout -B my-branch
$
$  # the status shows that you are on your new branch
$  git status
$
$  # list the files. it has the same files as your master branch
$  ls
$
$  # switch back to the master branch
$  git checkout master
$
$  git status # on master branch
$
$  # switch back to your new branch
$  git checkout my-branch
$
$  # when you switch branches, you stay in the same folder. Git changes the branch files for you:
$  pwd
$
$  # add some files to your new branch
$  echo "print('hello')" > hello.py
$
$  # see the pending changes in your new branch
$  git status
$
$  # commit files to your new branch
$  git add .
$  git commit -m "changes to my new branch" . 
$  git push origin my-branch  # push new branch and changes to GitLab/GitHub
$
$  # From your browser, look at your new branch in GitHub/GitLab
$  # The master branch is the default; you will have to select the my-branch branch manually
$
$  # Now your new branch is available locally on your computer and remotely in GitHub/GitLab
$  # You can keep your local my-branch branch if you need to make changes to that branch
$
$  # with changes in the my-branch branch, you may want to merge my-branch back into master
$  git checkout master  # check out the branch we want to merge to
$  git merge my-branch  # merge the branch into master
$  git push origin master  # update the master branch in the remote
$
$  # to delete your new branch from your local computer
$  # you cannot be in the my-branch branch while deleting it, so switch to master
$  git checkout master
$
$  git branch -D my-branch # delete the local my-branch branch
$
$  # my-branch is still in the remote GitLab/GitHub server
$  # if you also want to delete the branch from the remote GitLab/GitHub server
$  git push origin --delete my-branch  # delete remote branch!
$

Enter fullscreen mode

Exit fullscreen mode

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK