Sử dụng GIT , GIT-FLOW , GITLAB
Cấu Trúc Dự Án GIT
Kiến trúc một dự án Git gồm các branch:
# Lưu trữ toàn bộ source hoàn chỉnh
master
# Lưu trữ source trong quá trình dev
develop
# Thư mục lưu trữ các chức năng đang dev của member. Mỗi member nên tạo thư mục con bên trong để lưu trữ chức năng của mình. Vd: feature/[tên]/[tên chức năng]
feature/
# Thư mục lưu trữ các chức năng đang cần hotfix. Nó được base trên branch master. Cũng nên tạo thư mục con tên member bên trong để biết chức năng do ai đang fix
hotfix/
# Thư mục chứa các phiên bản đã phát hành
release/
# Khi sử dụng SourceTree các tên branch có dấu / sẽ được xem như thư mục
Quá trình phát triển chức năng của dự án (có thể dùng command hay SourceTree):
1. Member thực hiện pull source dự án từ branch develop về branch feature/[tên] của mình để tiến hành làm việc.
# Clone source code từ server
git clone [url/to/repo.git]
# Chuyển sang branch develop
git checkout -b develop
# Tạo branch feature từ devlope
git checkout -b feature/[tên member]/[tên chức năng]
2. Thực hiện phát triển chức năng. Add, update, delete file. Commit lên branch để lưu trữ
# Mark the files as ready to commit
git add .
# Commit the changes locally
git commit -m "My commit message"
3. Trong quá trình làm việc, nếu có sự thay đổi nào từ branch develop làm ảnh hưởng đến mình thì member tiến hành merger về feature của mình để dễ update
# Đến đúng branch đang dev
git checkout feature/[tên member]/[tên chức năng]
# Làm mới tất cả các branch trong origin
git fetch origin
# OR #
# Làm mới branch cụ thể trong origin server
git fetch origin [branch-name]
# Merge code từ [branch-name] ở origin server vào branch hiện tại
git merge origin [branch-name]
Do nhánh ban đầu ta tách ra từ 'develop' nên nếu 'develop' có sự update thì mình phải tiến hành merge về branch feature đang làm để không bị out update code làm chạy sai chức năng.
Sửa lỗi khi merger (Resolving merger conflic)
Trong quá trình merge nếu bị conflic thì ta tiến hành sửa các lỗi sai rồi chạy lại. Quá trình merge ta có thể chọn giữ lại file của owner hay dùng file remote. Một số câu lệnh dùng để xử lý merge conflic
# -- Reset file all
git checkout .
# -- Discard, reset file
git checkout <filename>
# -- đặt lại chỉ mục và thư mục làm việc về trạng thái commit cuối cùng
git reset --hard HEAD
# -- Sửa conflicted_file bằng cách lấy phiên bản trên server, <conflicted_file> == '.' sẽ lấy tất cả các file
git checkout --theirs <conflicted_file>
git add <conflicted_file>
# -- Sửa conflicted_file bằng cách lấy phiên bản trên local, <conflicted_file> == '.' sẽ lấy tất cả các file
git checkout --ours <conflicted_file>
git add <conflicted_file>
# -- Kiểm tra trạng thái các file thay đổi
git status
Tham khảo thêm resolving conflic
4. Sau khi phát triển xong chức năng sẽ tiến hành tạo merge request để merge feature của mình vào branch develop
git push origin feature/[tên member]/[tên chức năng]
Sau khi chạy lệnh trên Gitlab sẽ tạo cho chúng ta 1 link, dùng nó dán vào trình duyệt để tiến hành chọn member review code và accept merger.
...
remote: To create a merge request for my-new-branch, visit:
remote: https://gitlab.example.com/my-group/my-project/merge_requests/new?merge_request%5Bsource_branch%5D=my-new-branch
Quá trình tạo merge request này chúng ta cũng có thể tạo bằng WebUI của Gitlab. Tạo Merger Request
5. Tổng hợp các câu lệnh Git thông dụng, tips
# -- Add
git add <filename> | *
# -- Commit <==> commit tất cả
git commit -m "Commit message" <==> git commit -a
# -- Fetch and merge changes on the remote server to your working directory
git pull
# -- To merge a different branch into your active branch
git merge <branchname>
# -- Push the branch to your remote repository, so others can use it
git push origin <branchname>
# -- show current branch (with -a show all remote branch)
git branch
# -- Switched to a new branch "iss53", create new from existing branch if branch "iss53" does not exist
git checkout -b iss53
# -- Switched and create new from existing branch origin/[branch_name]
git checkout -b [branch_name] origin/[branch_name]
# -- Bỏ tất cả sự thay đổi trên toàn bộ file
git checkout .
# -- Bỏ sự thay đổi trên file
git checkout <file_path>
# -- Bỏ tất cả vừa thay đổi trên branch
git reset --hard
git reset --hard origin/<branchname>
Tạo new branch từ TAG
git checkout -b <New Branch Name> <TAG Name>
=> -b stands for creating new branch to local
Steps to do it.
1. git checkout -b NewBranchName v1.0
2. Make changes to pom / release versions
3. Stage changes
4. git commit -m "Update pom versions for Hotfix branch"
5. Finally push your newly created branch to remote repository.
git push -u origin NewBranchName
Kết nối project trên local với Git server (Github, Gitlab)
1 . Go to current folder
2 . git init
3 . git add .
4 . git commit -m "First commit"
5 . git remote add origin [Link Github Repository]
6 . git branch --set-upstream-to=origin/master master
7 . git pull --allow-unrelated-histories
8 . git pull origin master
9 . git pull
10 . git commit -m 'commit'
11 . git push
# -- For error : fatal: refusing to merge unrelated histories
git pull origin master --allow-unrelated-histories
Clone source từ một TAG để phát triển riêng
Use the --single-branch switch (available as of Git 1.7.10). The syntax is:
git clone -b <tag_name> --single-branch <repo_url> [<dest_dir>]
For example:
git clone -b 'v1.9.5' --single-branch https://github.com/git/git.git git-1.9.5
Sử dụng GITLAB (GITHUB cũng có thể dùng tương tự)
GitLab là một nền tảng DevOps toàn diện được sử dụng để quản lý mã nguồn và quy trình phát triển phần mềm. Nền tảng này cung cấp một môi trường tích hợp để quản lý mã nguồn.
1. Tạo SSH key để dễ dàng truy cập Gitlab.
# -- Sử dụng ED25519:
ssh-keygen -t ed25519 -C "<comment>"
# -- Để trống toàn bộ kể cả passphrare
# -- File tạo ra sẽ được lưu trong /Users/[username]/.ssh/id_ed25519
2. Add SSH key vào GitLab account.
# -- Run
tr -d '\n' < ~/.ssh/id_ed25519.pub | pbcopy
#-- để copy noi dung file pub vào clipboard
#-- Goto GitLab Server => Select Edit profile => select SSH Keys => Select Add new key
3. Xác nhận kết nối đến server Gitlab để truy vấn dữ liệu không cần login.
# -- Run command line
ssh -T git@[gitlab domain]
# -- Ex : ssh -T git@gitlab.example.com
# -- Type Yes để chấp nhận kết nối
# -- chạy lại câu lệnh trên nếu thấy thông báo chào mừng với username cần đế nối là OK
4. Clone source từ server sử dụng SSH.
Thay vì dùng Gitlab account cần nhập password, ta dùng SHH để dễ dàng thao tác code mà không cần nhập pw. Ứng dụng cho server tự động update source mà không cần auth
git clone git@[gitlab domain]:[username]/[project-dir].git
# -- có the copy link clone từ project repository
Thay vì dùng Gitlab account cần nhập password, ta dùng SHH để dễ dàng thao tác code mà không cần nhập pw
5. Access Token.
Chúng ta có thể dùng Access Token có thời hạn thay cho Password đăng nhập.
# -- Tạo Access Token trên Gitlab server
Gitlab server => Edit profile => Access Tokens => Add new token
# -- Sử dụng pull code tu dong
git clone https://[username]:[Password or Access Token]@gitlab.com/user/project.git
Tự động triển khai code dùng GIT Hooks
Ý tưởng là khi có một lệnh push code vào branch 'master' sẽ tự động thực hiện checkout code từ 'master' copy vào trong thư mục cần deploy. Tham khảo link . Các bước cần thực hiện:
1. Tạo thư mục deylop trên production server.
Dùng thư mục /var/www/html xem như là thư mục ví dụ chứa website trên production server.
# -- Cài đặt server, git
sudo apt-get update
sudo apt-get install apache2
sudo apt-get install git
# -- Phân quyền cho /var/www/html là deploy folder
sudo chown -R `whoami`:`id -gn` /var/www/html
# -- Sử dụng ssh để chứng thực. Xem lại phần sử dụng ssh trên gitlab
ssh -T git@[git domain]
2. Tạo thư mục bare của dự án
Đây là thư mục chứa kho lưu trữ. Chứ hook script chứ không chứa các thư mục làm việc.
mkdir ~/production-project
cd ~/production-project
git init --bare
3. Post-receive hook script
Đây là file hook chứa trong thư mục ./hook.
# -- Tạo file post-receive
vi ./hooks/post-receive
# -- Set quyền execute
chmod +x ./hooks/post-receive
#!/bin/bash
TARGET="/var/www/html"
GIT_DIR="/home/webuser/production-project"
BRANCH="master"
while read oldrev newrev ref
do
# only checking out the master (or whatever branch you would like to deploy)
if [ "$ref" = "refs/heads/$BRANCH" ];
then
echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
else
echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
fi
done
4. Cấu hình trên máy local
Trong thư mục dự án đã cấu hình git ở local.
# -- Thư mục dự án ở máy local
cd ~/local-project
# -- Add the remote server as a remote called production
# -- Bước này chúng ta có thể tạo nhiều server khác nhau như là "staging", "uat" or "test"
git remote add production [user]@[remote_server_domain_or_IP]:production-project
5. Push to the production server
Trong thư mục dự án đã cấu hình git ở local.
# -- Gọi lệnh push master branch lên production server
git push production master
Sau khi nhập password nếu bạn không có ssh. Bạn có thể thấy thông báo hiện ra
Output]
Counting objects: 8, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 473 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote: Master ref received. Deploying master branch...
To sammy@107.170.14.32:proj
009183f..f1b9027 master -> master
Tham khảo
Các thuật ngữ quan trọng trong Git
Use Git Hooks To Automate Development and Deployment Tasks
Simple automated GIT Deployment using GIT Hooks
Bỏ túi 21 lệnh Git cơ bản
How to Use Git and GitHub – a Guide for Beginners and Experienced Developers