Notes sử dụng GIT (github)
Cấu hình
Khai báo cấu hình git trên máy tính
Mở git bash
ra và lần lượt chạy các lệnh sau:
Nhớ thay {your_name} và {your_email} tương ứng với thông tin account github của bạn.
git config --global user.name {your_name}
git config --global user.email {your_email}
ssh-keygen -t rsa
ls ~/.ssh/
ssh-agent -s
ssh-add ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub
Tạo SH Key cho github
Truy cập https://github.com/settings/keys Tạo một key mới
paste
key copy ở bước cuối phần config trên vào và lưu lại.
Sử dụng cơ bản
Có 3 lệnh cơ bản:
-
pull
: Để kéo mã nguồn, cập nhật những thay đổi từ trên github về local -
push
: Để đẩy mã nguồn, cập nhật code từ local lên github -
commit
: Có thể xem như đóng gói những thay đổi để chuẩn bị pull hay push
Có 2 quy tắc chính khi làm việc với github:
Quy tắc 1
Về quy tắc là mình phải luôn pull trước rồi mới bắt đầu làm việc, thêm, xóa, sửa code rồi mới push sau. Nhất là khi làm việc nhóm để đảm bảo cái mình edit luôn là cái mới nhất.
Quy tắc 2
Khi làm việc nhóm thì nên tạo branch
riêng
Một số lệnh cơ bản: (Để push code lên thì mình cũng nên thực hiện từ trên xuống hết các lệnh này)
# pull
git pull origin ten_branch
# add
git add --all
# Commit
git commit -m "ghi chú của bạn"
# Push
git push origin ten_branch
Lệnh khác (ít dùng hơn)
# Tạo branch
git branch ten_branch
# Thay đổi branch sử dụng
git checkout ten_branch
Giải quyết một số lỗi hay gặp
fatal: refusing to merge unrelated histories
git pull origin branchname --allow-unrelated-histories
https://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories-on-rebase
Tìm tên, đường dẫn của reposite mình đang có ở local
git config --get remote.origin.url
Git - Cloning Specific Commits
https://github.com/<repo_name>/tree/<commit_sha>
https://github.com/soiqualang/thongke_phanloai_raster/commit/1a222352b31fcdfc31e0ab7a325cf27b3dfdf2ac
https://github.com/soiqualang/thongke_phanloai_raster/tree/1a222352b31fcdfc31e0ab7a325cf27b3dfdf2ac
Clone The Repo And Checkout The Specific Commit
git clone -n <repo_name>
git checkout <commit_sha>
Clone The Repo And Checkout The Specific Commit Into A Branch
git clone -n <repo_name>
git checkout -b <new_branch> <commit_sha>
https://coderwall.com/p/xyuoza/git-cloning-specific-commits
How to delete a git tag locally and remote
# delete local tag '12345'
git tag -d 12345
# delete remote tag '12345' (eg, GitHub version too)
git push origin :refs/tags/12345
# alternative approach
git push --delete origin tagName
git tag -d tagName
https://gist.github.com/mobilemind/7883996
Git Delete Commands
## Delete a remote branch
$ git push origin --delete <branch> # Git version 1.7.0 or newer
$ git push origin :<branch> # Git versions older than 1.7.0
## Delete a local branch
$ git branch --delete <branch>
$ git branch -d <branch> # Shorter version
$ git branch -D <branch> # Force delete un-merged branches
## Delete a local remote-tracking branch
$ git branch --delete --remotes <remote>/<branch>
$ git branch -dr <remote>/<branch> # Shorter
$ git fetch <remote> --prune # Delete multiple obsolete tracking branches
$ git fetch <remote> -p # Shorter
https://gist.github.com/cmatskas/454e3369e6963a1c8c89
Git clone with username
You can leave out the password so that it won't be logged in your Bash history file:
git clone https://[email protected]/username/repository.git
It will prompt you for your password.
Alternatively, you may use:
git clone https://username:[email protected]/username/repository.git
soiqualang_chentreu