- Published on
Hướng dẫn sử dụng git
- Authors
- Name
Table of Contents
Sử dụng git cơ bản
1. Tạo project mới
- Git push using SSH
git push --set-upstream git@gitlab.com:DungSherlock/{tên_project_mới}.git master
- Git push using HTTPS
git push --set-upstream https://gitlab.com/DungSherlock/{tên_project_mới}.git master
2. Hướng dẫn push
Git clone
3. Hard reset 1 commit
Trường hợp bạn muốn quay lại git ở một thời điểm nào đó và xoá các commit phía sau nó. Lệnh này sẽ back cứng lại các file git trên local kể cả đang mở file hay không. Đưa về nguyên trạng thái tại thời điểm commit được chỉ định.
Ví dụ: Muốn back lại commit 3aced4d4320347edd16a0648fe5ae7afdaa7c62a
- Bước 1
git reset --hard 3aced4d4320347edd16a0648fe5ae7afdaa7c62a
- Bước 2
git push origin HEAD -f
4. Github Actions
name: dung.men
on: push
jobs:
deploy:
runs-on: ubuntu-18.04
steps:
- name: Git checkout
uses: actions/checkout@v2
- name: Setup hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: "latest"
extended: true
- name: Get Current Time
uses: 1466587594/get-current-time@v2.0.0
id: current-time
with:
format: YYYYMMDDHHMMSS
utcOffset: "+07:00"
- name: Build
run: hugo -D --gc --minify
- name: Find and Replace css
uses: jacobtomlinson/gha-find-replace@0.1.2
with:
find: "dung.men/css/"
replace: "cdn.jsdelivr.net/gh/DungSherlock/dung.men@${{ steps.current-time.outputs.formattedTime }}/css/"
include: "public/"
- name: Find and Replace js
uses: jacobtomlinson/gha-find-replace@0.1.2
with:
find: "dung.men/js/"
replace: "cdn.jsdelivr.net/gh/DungSherlock/dung.men@${{ steps.current-time.outputs.formattedTime }}/js/"
include: "public/"
- name: Find and Replace 404.html
uses: jacobtomlinson/gha-find-replace@0.1.2
with:
find: "dung.men/404.html"
replace: "cdn.jsdelivr.net/gh/DungSherlock/dung.men@${{ steps.current-time.outputs.formattedTime }}/404.html"
include: "public/"
- name: Find and Replace index.xml
uses: jacobtomlinson/gha-find-replace@0.1.2
with:
find: "dung.men/index.xml"
replace: "cdn.jsdelivr.net/gh/DungSherlock/dung.men@${{ steps.current-time.outputs.formattedTime }}/index.xml"
include: "public/"
- name: Find and Replace manifest.json
uses: jacobtomlinson/gha-find-replace@0.1.2
with:
find: "dung.men/manifest.json"
replace: "cdn.jsdelivr.net/gh/DungSherlock/dung.men@${{ steps.current-time.outputs.formattedTime }}/manifest.json"
include: "public/"
- name: Find and Replace /om/index.xml
uses: jacobtomlinson/gha-find-replace@0.1.2
with:
find: "dung.men/om/index.xml"
replace: "cdn.jsdelivr.net/gh/DungSherlock/dung.men@${{ steps.current-time.outputs.formattedTime }}/om/index.xml"
include: "public/"
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
personal_token: ${{ secrets.GH_TOKEN }}
external_repository: DungSherlock/dung.men
publish_dir: ./public
keep_files: false
user_name: dungsherlock
user_email: dungsherlock@gmail.com
publish_branch: master
cname: dung.men
tag_name: ${{ steps.current-time.outputs.formattedTime }}
5. Bỏ qua khi update git với .gitignore
# =========================
# Node.js-Specific Ignores
# =========================
# Build directory
public*/
resources/
# Gatsby cache
.cache/
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# =========================
# Operating System Files
# =========================
# OSX
# =========================
.DS_Store
.AppleDouble
.LSOverride
# Thumbnails
._*
# Files that might appear on external disk
.Spotlight-V100
.Trashes
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# Windows
# =========================
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msm
*.msp
# Windows shortcuts
*.lnk
.vscode
6. Xoá hoàn toàn 1 commit
- Lấy history commit và copy
idCommit
muốn xoá
git log
- Xoá commit và đẩy lên repo
git reset --hard {idCommit}
git push origin HEAD -f
7. Tạo SSH key để truy cập
Tạo ssh-key mới
- Cách 1
ssh-keygen -t rsa -b 4096 -C "dungsherlock@gmail.com"
- Cách 2
ssh-keygen -t rsa
8. Dùng nhiều tài khoản git trên 1 máy
- Truy cập vào thư mục
.ssh
cd ~/.ssh
- Tạo ssh key
ssh-keygen -t rsa -C "github-email-address"
Sau khi enter nhập tên file muốn lưu. Ví dụ: github
thì sẽ tạo ra các file github
và github.pub
trong cùng thư mục ~/.ssh
. Mở và copy toàn bộ nội dung của file github.pub
sau đó truy cập web: https://github.com/settings/keys
để add SSH key vào tài khoản github
- Mở file cấu hình git bằng Visual Code hoặc VIM để cấu hình bằng lệnh
code ~/.ssh/config
. Sau đó nhập liệu như sau:
# Tài khoản git cá nhân vẫn dùng của bạn
Host github.com # Tài khoản mặc định
HostName github.com
User git
IdentityFile ~/.ssh/github # id_rsa_tuan mà chúng ta vừa tạo
PreferredAuthentications publickey
# Tài khoản công việc
Host work.github.com # Tài khoản công việc thêm 1 tiền tố bất kỳ vào trước github.com. Ví dụ: work
HostName github.com
User git
IdentityFile ~/.ssh/github_2
PreferredAuthentications publickey
- Chạy lệnh để áp dụng cấu hình ssh mới
ssh-keygen -R github.com
- Cách clone 1 repo từ tài khoản chỉ định Tài khoản mặc định thì với
Host github.com
thì sử dụng bình thường. Với tài khoản thứ 2 (Tài khoản công việc) thì thêm tiền tố của tên miền như sau:
git clone git@github.com:DungSherlock/sample_react.git (Mặc định) git clone git@work.github.com:DungSherlock/sample_react.git (Chỉnh sửa để clone bằng tài khoản git thứ 2)