- ติดตั้ง Git
- Getting a Git Repository
- Recording Changes to the Repository
- Branch
- Delete branch
- Rename branch
- Tag
- Fix
1. ติดตั้ง Git
Install Git with Yum
sudo yum install git
Install Git with apt
sudo apt install git
ตรวจสอบการติดตั้ง
git --version
Set Up Git
Name and email address
git config --global user.name "Your Name" git config --global user.email "you@example.com"
Git can handle this by auto-converting CRLF line endings into LF when you add a file to the index, and vice versa when it checks out code onto your filesystem. You can turn on this functionality with the core.autocrlf
setting. If you’re on a Windows machine, set it to true
— this converts LF endings into CRLF when you check out code:
Formatting
git config --global core.autocrlf true
If you’re on a Linux or macOS system that uses LF line endings, then you don’t want Git to automatically convert them when you check out files; however, if a file with CRLF endings accidentally gets introduced, then you may want Git to fix it. You can tell Git to convert CRLF to LF on commit but not the other way around by setting core.autocrlf
to input:
git config --global core.autocrlf input
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
Ignorecase
git config --global core.ignorecase false
Checking Your Settings
ค่าที่แสดงออกมา มีทั้งค่า global และ user
git config --list
You can view all of your settings and where they are coming from using:
ดูว่า config อะไรมาจากไฟล์ไหนใช้ --show-origin
git config --list --show-origin
Git config file locations
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>
แต่ถ้าจะ discard (Undoing Uncommitted Changes) – Git Undo Commit: How to Undo Changes in Git (cloudbees.com)
$ git stash
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
ถ้า clone แล้วได้ error
fatal: unable to access 'https://gitxxx.com/jack/jack.git/': Peer's Certificate issuer is not recognized.
ให้แก้ไขโดยการ ignore CA – git – what does this error message imply: fatal: unable to access ‘https:URL’: Peer’s Certificate issuer is not recognized? – Stack Overflow
env GIT_SSL_NO_VERIFY=true git clone https://github...
หรือ set http.sslverify ให้เป็น false เลย
$ git config --global http.sslverify false
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
git fetch
git fetch
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
Delete branch
git branch --delete <branch_name>
ถ้าเป็น branch ที่ยังไม่ได้ fully merged จะ error: The branch ‘<branch_name>
‘ is not fully merged.
If you are sure you want to delete it, run ‘git branch -D <branch_name>
‘.
git branch -D <branch_name>
Rename branch
There are a few ways to accomplish that:
- Change your local branch and then push your changes
- Push the branch to remote with the new name while keeping the original name locally
Renaming local and remote
# Rename the local branch to the new name git branch -m <old_name> <new_name> # Delete the old branch on remote - where <remote> is, for example, origin git push <remote> --delete <old_name> # Or shorter way to delete remote branch [:] git push <remote> :<old_name> # Prevent git from using the old name when pushing in the next step. # Otherwise, git will use the old upstream name instead of <new_name>. git branch --unset-upstream <new_name> # Push the new branch to remote git push <remote> <new_name> # Reset the upstream branch for the new_name local branch git push <remote> -u <new_name>
ลองเปลี่ยนชื่อ branch จาก develop เป็น dev
> git branch -m develop dev > git push origin --delete develop > git branch --unset-upstream dev > git push origin dev > git push origin -u dev
Renaming Only remote branch
# In this option, we will push the branch to the remote with the new name # While keeping the local name as is git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>
5. Tag
Listing Your Tags
Listing the existing tags in Git is straightforward. Just type git tag
(with optional -l
or --list
):
$ git tag v1.0 v2.0
This command lists the tags in alphabetical order; the order in which they are displayed has no real importance.
You can also search for tags that match a particular pattern. The Git source repo, for instance, contains more than 500 tags. If you’re interested only in looking at the 1.8.5 series, you can run this:
$ git tag -l "v1.8.5*" v1.8.5 v1.8.5-rc0 v1.8.5-rc1 v1.8.5-rc2 v1.8.5-rc3 v1.8.5.1 v1.8.5.2 v1.8.5.3 v1.8.5.4 v1.8.5.5
Creating Tags
Git supports two types of tags: lightweight and annotated.
A lightweight tag is very much like a branch that doesn’t change — it’s just a pointer to a specific commit.
Annotated tags, however, are stored as full objects in the Git database. They’re checksummed; contain the tagger name, email, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG). It’s generally recommended that you create annotated tags so you can have all this information; but if you want a temporary tag or for some reason don’t want to keep the other information, lightweight tags are available too.
Annotated Tags
Creating an annotated tag in Git is simple. The easiest way is to specify -a
when you run the tag
command:
$ git tag -a v1.4 -m "my version 1.4" $ git tag v0.1 v1.3 v1.4
You can see the tag data along with the commit that was tagged by using the git show
command:
$ git show v1.4 tag v1.4 Tagger: Ben Straub <ben@straub.cc> Date: Sat May 3 20:19:12 2014 -0700 my version 1.4 commit ca82a6dff817ec66f44342007202690a93763949 Author: Scott Chacon <schacon@gee-mail.com> Date: Mon Mar 17 21:52:11 2008 -0700 Change version number
Push a tag to a remote repository
To push a single tag:
git push origin <tag_name>
And the following command should push all tags (not recommended):
# not recommended git push --tags
6. 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
ถ้ารันคำสั่ง git
เช่น git fetch
แล้วถาม password ทุกครั้ง ให้ set ตามนี้เพื่อจำ password (Make Git store the username and password and it will never ask for them.) – How to fix Git always asking for user credentials (freecodecamp.org)
git config --global credential.helper store