Git tutorial

Oct 18, 2020 00:00 · 429 words · 3 minute read git

This is a tutorial of how to use the git version control system.

Git is a version control system. Using git, you can trace your code change history and better collaborate with others.

1. Git workflow introduction

When working with git, it’s always safe to have your own branch and do your personal work within it simply because the main branch may connect to the production system directly and you don’t want some small bugs to affect all your users. After everything is tested, you can merge your own branch with the main branch.

The following picture shows the basic workflow of using git. In your local desktop, typically you need to create two branches - one is the local main branch and the other one is the local feature branch. After your have a new feature developed, you may want to push the feature branch to remote repo and create a pull request to merge the remote feature branch with the remote main branch.

git_workflow

2. Basic usage

Here is an example of how to do that.

#Clone the main branch
$git clone [your_main_branch_address]
# Setup repo username and email (add --global for global setting)
$git config [--global] user.name [Your git user name]
$git config [--global] user.email [Your git user email]
# Create and switch to your local feature branch
$git checkout -b [name_of_your_feature_branch]
# Check all local branches
$git branch
# Make some changes and check git status
$git status
# Stage your changes
$git add .
# Commit your changes
$git commit -m "I made some changes"
# push changes to your remote feature branch
$git push origin [name_of_your_feature_branch]

3. Merge feature branch with latest main branch

Another situation is that when you were working on your branch, your colleagues have already finished their part and committed new changes to the main branch. To make sure your working branch contains the latest code from main branch, a typical practice is to first update your local main branch and then merge your main branch with your local feature branch.

# Switch to main branch
$git checkout main
# Pull latest code from main branch
$git pull origin main
# Switch to feature branch (assume feature is your local working branch name)
$git checkout feature
# Merge main branch
$git merge main
# Push back to remote feature branch
$git push origin feature

4. YouTube tutorial

Here’s a video tutorial for the above process. Follow this tutorial to see if you can get the same results!

References:

tweet Share