Fly with code

Get wet inside ocean of code

0%

Tips for beginner to use Git

Why need to learn version control system (Git) ?

When programmer works on project, there will be lots of files and source codes created during the development.
In the ancient times, people will create lots of files using different name tags, to identify with each other.

1
2
3
2020-08-21.html
2020-08-22.html
2020-08-23.html

But, smart people don’t follow this way.
Instead, smart people use version control system (Git).

Local Repository Setting

  1. Install Git
  2. Install Sourcetree
  3. Create your project folder.
  4. Open your Git Bash, using CMD command to enter that folder.
    Belows are cheat sheet for some of the most common command.
# Windows MacOs / Linux Description
1 cd [file route] cd [file route] Enter into destination folder
2 cd pwd Get current file location
3 dir Is Show file list in the current folder
4 mkdir mkdir Add new folder
5 copy copy Duplicate file
6 move mv Move file
7 del rm Delete file

Now we can initialize folder

1
git init

Also can confirm if you install Git successfully

1
git --version //It will show your Git version

Set up your personal info

1
2
git config --global user.name "username" //set up user name
git config --global user.email "username@gmail.com" //set up user email

Check all Git setting

1
git config --list
  1. Some basic Git command

Check file status in the folder

1
git status

Add all of the files from working directory to staging area

1
git add .

Get file ready in local repository before pushing to remote repository

1
git commit -m "commit comment "

Examine commit history

1
git log

When using git log, you can see SHA-1 number distributed for each commit operation
then use that SHA-1 number , move your HEAD location to designated commit position.
And it will allow you to check previous version status.

1
git checkout

Connect with Remote Repository

When we are ready to push local repository onto Github
Next step is, adding remote repository on Github

Now, we have existed local repository, therefore, we input below in Git Bash

1
2
3
…or push an existing repository from the command line
git remote add origin https://github.com/a7474267/131.git
git push -u origin master

Here,
https://github.com/a7474267/131.git means web link of remote repository
origin means name of remote repository
master means branch name
-u means default repository service supplier, ex. Github, Bitbucket, Gitlab

By the way, for future update in local repository, just use below command to push to Github

1
git push origin master

But, if we don’t build local repository in advance, then we turn to use below instead

1
2
3
4
5
6
echo "# 131" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/a7474267/131.git
git push -u origin master

One repository name can only be used for one time

Due to the requirement amid real software development, multiple remote repositories are needed.
For example, repository for testing, repository for official release…etc

For each remote repository, we must use different repository name, even these repositories are stored on different service supplier site.
For instance, there is a repository on Github named origin, then you can’t open another repository named origin on Bitbucket.

Other small tips

If you want to shorten the length of Git command
even speed up your coding efficiency
then, abbreviation setting might be your good friend.
For example, we type git commit, but you can make it easier by applying git ci only


Here are some simple abbreviations example
All can be customized in accordance with your coding habit.

1
2
3
4
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.ci commit