- ติดตั้ง Git
- Getting a Git Repository
- Recording Changes to the Repository
- Branch
- Fix
1. ติดตั้ง Git
Install Git with Yum
sudo yum install git
Install Git with apt
sudo apt install git
ตรวจสอบการติดตั้ง
git --version
Set Up Git
git config --global user.name "Your Name" git config --global user.email "you@example.com"
Checking Your Settings
git config --list
You can view all of your settings and where they are coming from using:
git config --list --show-origin
Your default branch name
By default Git will create a branch called master when you create a new repository with git init
. From Git version 2.28 onwards, you can set a different name for the initial branch.
git config --global init.defaultBranch main
2. Getting a Git Repository
Initializing a Repository in an Existing Directory
cd /home/user/my_project git init
git add *.c git add LICENSE git commit -m 'Initial project version'
หรือ add ทุกไฟล์ใน directory ด้วย
git add .
แต่ถ้าจะลบไฟล์ก็ git rm
(ลบไฟล์พร้อมกับ stage ให้ด้วย)
git rm <filename>
Cloning an Existing Repository
You clone a repository with git clone <url>
. For example, if you want to clone the Git linkable library called libgit2
, you can do so like this:
git clone https://github.com/libgit2/libgit2
If you want to clone the repository into a directory named something other than libgit2
, you can specify the new directory name as an additional argument:
git clone https://github.com/libgit2/libgit2 mylibgit
3. Recording Changes to the Repository
Checking the Status of Your Files
git status
ถ้าจะให้แสดง diff (staged) ด้วยก็
git status -v
หรือถ้าจะดูสรุปสั้นๆก็
git status -s
$ echo 'My Project' > README $ git status On branch master Your branch is up-to-date with 'origin/master'. Untracked files: (use "git add <file>..." to include in what will be committed) README nothing added to commit but untracked files present (use "git add" to track)
Tracking New Files
git add README
$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: README
Ignoring Files
$ cat .gitignore *.[oa] *~
Viewing Your Staged and Unstaged Changes
To see what you’ve changed but not yet staged, type git diff
with no other arguments:
git diff
If you want to see what you’ve staged that will go into your next commit, you can use git diff --staged
or git diff --cached
.
git diff --staged
หรือ
git status -v
Committing Your Changes
git commit -m "Story 182: fix benchmarks for speed"
ดูประวัติการ commit
git log git log --oneline git log --oneline --graph
การ reset (ย้อนกลับ) โดย default จะเป็นแบบ mixed
git reset <เลข commit ที่ได้จาก git log>
ดูประวัติ commit (รวมที่ reset ไปแล้วด้วย)
git reflog
ถ้าอยากย้อนกลับไป commit ที่เคย reset หายไปแล้ว ก็ทำได้ด้วย
git reset <เลข commit ที่ได้จาก git reflog>
git push
git push origin main
git pull
git pull
4. Branch

Create Branch
git branch <branch_name>
Remove Branch (Local)
git branch -d <branch_name>
Switch Branch
git checkout <branch_name>
Create and Switch Branch
git checkout -b <branch_name> <commit_id (optional)>
List branch
git branch
Git Merge
git merge <branch_name> git merge <branch_name> --no-ff # merge commit
5. Fix
ยังไม่ได้เชื่อมต่อด้วย SSH
ดูที่ Generate an SSH key pair
SSL certificate problem: unable to get local issuer certificate
Resolution #1 – Self Signed certificate
Tell git to not perform the validation of the certificate using the global option:
git config --global http.sslVerify false
You must use a personal access token with ‘read_repository’ or ‘write_repository’ scope for Git over HTTP.
วิธีแก้
สร้าง personal access token ที่มีสิทธิ read_repository or write_repository เป็นอย่างน้อย
สั่งรัน git clone ใหม่ แล้วจะมีไดอะล็อก ให้ใส่ username, password ก็ใส่ตามนี้
username: login password: <credential ที่ได้จากตอนสร้าง personal access token>
แต่ถ้าไม่มีไดอะล็อก ให้ลองไปลบ ของเดิมที่ Credential Manager