Những lệnh GIT thường sử dụng từ cơ bản đến nâng cao

17152

Bài viết được sự cho phép của tác giả Lê Chí Dũng

I. Intro & Install

Git là 1 hệ thống Distributed revision control (Distributed version control or decentralized version control), tạm dịch là hệ thống quản lý source phân tán.

Chỉ mất 3s để đồng bộ metadata.

Cài  đặt 

https://git-scm.com/downloads

Documents

https://help.github.com/enterprise/2.9/

II. Config

1. Short commands

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.st 'status -s '

Linux:
Config file’s location: ~/.gitconfig

  12 điều cực "cool" mà bạn có thể làm với Github
  10 Vấn đề về Git thường gặp và Giải pháp

Xem thêm các việc làm COBOL hấp dẫn trên TopDev

Windows:
Config file’s location: C:Users{your_name}.gitconfig

[core]
    preloadindex = true
    fscache = true                   
    autocrlf = true
    quotepath = off
[gc]
    auto = 256
[user]
    email = lcdung@example.com
    name = lcdung
[color]
    ui = true
[alias]
    tree   = log --oneline --decorate --all --graph
    st     = status -s
    ig     = update-index --assume-unchanged
    no-ig  = update-index --no-assume-unchanged
    co     = checkout
    getlog = log --oneline -10
[credential]
    helper = cache
git config --global user.email "your_email@example.com"
git config --global user.username"lcdung"

2. Remember username and password when using [http]

git config --global credential.helper cache
git config credential.helper cache

3. Ignore files

Edit file .gitignore

git update-index --assume-unchanged
git update-index --no-assume-unchanged

III. Basic commands

Mô hình Git

1. Remote:

Mặc định (origin repository trên server và local repository trên local)

1. Add remote

git remote add <remote_name> <url>

2. Checking name of current remote

git remote -v
# View existing remotes
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)

3. Rename remote

git remote rename <old_name> <new_name>

4. Set URL remote

git remote set-url origin https://github.com/user/repo2.git
# Change the 'origin' remote's URL

5. Delete

git remote rm <remote_name>

2. Add files

1. Add multi files

git add <file1> <file2> ....

2. Add all files

# Only adding modified files and new files
git add .

# Adding all (deleted, modified and new)
git add --all

3. Pull

git pull
git pull <remote_name> <branch_name>

4. Push

git push <remote_name> <branch_name>

# Force update
git push -f <remote_name> <branch_name>

5. Fetch

# Update remote without deleting branches
git fetch <remote_name>

# Update remote and deleting branch (if have)
git fetch <remote_name> --prune

IV. Main commands

1. Init

git init

2. Clone

# Clone repo:
git clone <url> {folder_name}

# Clone single branch of repo:
git clone -b <branch_name> <url> {folder_name}

3. Status

git status
git status -s

4. Branch

# List all branch of local:
git branch

# List all branch of local and remote:
git branch --all

# Create new branch and does not switch to new branch
git branch <new_branch>

# Create new branch and switch to new branch
git checkout -b <new_branch>

# Rename current branch to new_branch_name
git branch -m new_branch_name

# Delete branch on local
git branch -D <branch_name1> <branch_name2> ...

# Delete branch on remote
git push origin :<branch_name1> :<branch_name2> ...
git push origin --delete <branch_name1> <branch_name2>

5. Add files

# Add multi files
git add <file1> <file2> ....


# Add all files
## Only adding modified files and new files
git add .

## Adding all (deleted, modified and new)
git add --all

6. Commit

# Commit a message
git commit -m "Sample message"

# Edit commit message of last commit
git commit --amend -m "Edit sample message"

7. Logs

# View short logs:
git log --oneline -10

# View pretty logs:
git log --oneline --decorate --all --graph

8. Checkout

# Checkout to branch
git checkout <branch_name>

# Checkout to commit hash
git checkout <commit_hash>

# Checkout file with commit hash
git checkout <commit_hash> <file_path>

# Checkout file from another branch
git checkout <branch_name> <file_path>

9. Reset

# Reset HEAD
git reset --hard
git reset origin/master --hard

# Reset with commit hash
git reset <commit_hash>

10. Revert

# Revert with commit hash
git revert <commit_hash>

11. Merge

git merge <branch_name>

12. Rebase

Để tích hợp các thay đổi từ nhánh này vào nhánh khác và commit trong nhánh hiện tại. (https://git-scm.com/book/vi/v1/Ph%C3%A2n-Nh%C3%A1nh-Trong-Git-Rebasing)

Rebase on branch: When rebase other brach need checkout other branch.

# Rebase <branch_name> into current branch
git rebase <branch_name>

# Rebase <branch_name2> into <branch_name1>
git rebase <branch_name1> <branch_name2>

Rebase on commit: When rebase other brach need checkout other branch.

git rebase -i <commit_hash>
  
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

13. Cherry-pick

lấy commit từ branch khác về branch hiện tại.

git cherry-pick <commit_hash1> <commit_hash2> ..

14. Diff

So sánh code

# Diff two commit:
git diff <commit_hash1> <commit_hash2>

# Diff two branches:
git diff <branch_name1> <branch_name2>

# Diff file on two branches:
git diff <branch_name1> <branch_name2> -- <file_path>

15. Stash

Backup hiện trạng đang làm và nhảy code về bất kỳ version được backup trước đó.

# Stashed all changed
git stash

# List all stash
git stash list

# Show detail a stash
git stash show stash@{0}

# Apply latest stash to current branch
git stash pop

# Apply stash{0} into current branch
git stash pop stash@{0}

# Apply stash{0} into current branch
git stash apply stash@{0}

# Drop stash{0} into current branch
git stash drop stash@{0}

# Clear all stash
git stash clear

V. Tips & Notes

List all ignore files:

git ls-files -v | grep '^h'

Count modified files:

git status | grep 'modified:' | wc -l

Ignore chmod file:

git config core.fileMode false

Some commands need to push with option -f:

reset
rebase
commit --amend

VI. Q&A

  • Lúc commit rồi mà phát hiện ra commit đó sai quá sai thì phải undo hay xóa cái commit đó ra sao?
  • Muốn quay lại commit thứ N thì làm sao? 

Thông thường và dể dùng nhất là dùng git reset tuy nhiên còn vài cách khác dễ dùng hơn nửa đấy!

Bạn có thể tham khảo tại bài này -> Giới thiệu 3 cách undo commit hoặc loại bỏ commit cơ bản


  • Tôi đang chưa hiểu rõ sự khác biệt giữa git merge và git rebase là gì?
  • Tại sao tôi lại dùng git rebase mà không phải merge trong dự án này?
  • git rebase nó hay bị nhiều conflict hơn git merge?
  • git merge dễ tra log hơn git rebase đúng không vì rebase sửa log lại hết rồi  ?

Lý do áp dụng rebase trong dự án là cho mọi thứ đều dễ dàng xem, điều tra log khi xem qua transport plan trong git. Chính vì thế việc áp dụng rebase cho từng brach riêng biệt là cách để cac developer có tư duy logic tốt log một cách có hệ thống những commit thực sự quan trọng và cần thiết để tra cứu sau này hạn chế commit spam ngoài ý muốn.

Việc conflict trong git là điều không thể tránh khỏi khi merge hay rebase branch khác mà làm chung trên 1 file mình chưa thấy dẫn chứng nào là rebase conflict nhiều hơn merge.

Bạn muôn biết rõ sự khác biệt này thì vào xem bài viết này ->


  • Trước khi commit mới thì mình phải pull code mới về, mà nếu gặp conflict thì phải xử lý, nếu xử lý ko khéo thì mất code của mình làm, vậy phải làm sao đây?
  • Nếu chỉ backup 1 lần cho 1 commit thì cực quá vì pull nhiều branch thì sao?
  • Mình đang làm branch hiện tại chưa muốn commit, mà phải qua branch khác fix bug dùm member vậy thì phải làm sao?

Nhìn chung mọi câu hỏi đều xoay quanh việc bạn muốn backup code mà chưa thể commit được! Có 1 câu thần chú mà mình hay dùng đó là git stash nó cho phép bạn backup mọi lúc, không giới hạn số lần backtup và bạn dễ dàng tra cứu lại những gì bạn backup nữa.

Bạn muôn biết rõ sự khác biệt này thì vào xem bài viết này -> Tổng hợp sử dụng git stash hiệu quả

VII. References

Bài viết gốc được đăng tải tại lcdung.top

Có thể bạn quan tâm:

Xem thêm tuyển dụng IT hấp dẫn trên TopDev