Hướng dẫn các lệnh git cơ bản năm 2024

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.

Hướng dẫn các lệnh git cơ bản năm 2024

Hướng dẫn các lệnh git cơ bản năm 2024

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

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 = [email protected]  
    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 "[email protected]"  
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

Hướng dẫn các lệnh git cơ bản năm 2024

1. Remote:

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

1. Add remote

git remote add  

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  

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 

2. Add files

1. Add multi files

[core]  
    preloadindex = true  
    fscache = true                     
    autocrlf = true  
    quotepath = off  
[gc]  
    auto = 256  
[user]  
    email = [email protected]  
    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

0

2. Add all files

[core]  
    preloadindex = true  
    fscache = true                     
    autocrlf = true  
    quotepath = off  
[gc]  
    auto = 256  
[user]  
    email = [email protected]  
    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

1

3. Pull

[core]  
    preloadindex = true  
    fscache = true                     
    autocrlf = true  
    quotepath = off  
[gc]  
    auto = 256  
[user]  
    email = [email protected]  
    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

2

4. Push

[core]  
    preloadindex = true  
    fscache = true                     
    autocrlf = true  
    quotepath = off  
[gc]  
    auto = 256  
[user]  
    email = [email protected]  
    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

3

5. Fetch

[core]  
    preloadindex = true  
    fscache = true                     
    autocrlf = true  
    quotepath = off  
[gc]  
    auto = 256  
[user]  
    email = [email protected]  
    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

4

IV. Main commands

1. Init

[core]  
    preloadindex = true  
    fscache = true                     
    autocrlf = true  
    quotepath = off  
[gc]  
    auto = 256  
[user]  
    email = [email protected]  
    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

5

2. Clone

[core]  
    preloadindex = true  
    fscache = true                     
    autocrlf = true  
    quotepath = off  
[gc]  
    auto = 256  
[user]  
    email = [email protected]  
    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

6

3. Status

[core]  
    preloadindex = true  
    fscache = true                     
    autocrlf = true  
    quotepath = off  
[gc]  
    auto = 256  
[user]  
    email = [email protected]  
    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

7

4. Branch

[core]  
    preloadindex = true  
    fscache = true                     
    autocrlf = true  
    quotepath = off  
[gc]  
    auto = 256  
[user]  
    email = [email protected]  
    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

8

5. Add files

[core]  
    preloadindex = true  
    fscache = true                     
    autocrlf = true  
    quotepath = off  
[gc]  
    auto = 256  
[user]  
    email = [email protected]  
    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

9

6. Commit

git config --global user.email "[email protected]"  
git config --global user.username"lcdung"

0

7. Logs

git config --global user.email "[email protected]"  
git config --global user.username"lcdung"

1

8. Checkout

git config --global user.email "[email protected]"  
git config --global user.username"lcdung"

2

9. Reset

git config --global user.email "[email protected]"  
git config --global user.username"lcdung"

3

10. Revert

git config --global user.email "[email protected]"  
git config --global user.username"lcdung"

4

11. Merge

git config --global user.email "[email protected]"  
git config --global user.username"lcdung"

5

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.

git config --global user.email "[email protected]"  
git config --global user.username"lcdung"

6

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

git config --global user.email "[email protected]"  
git config --global user.username"lcdung"

7

13. Cherry-pick

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

git config --global user.email "[email protected]"  
git config --global user.username"lcdung"

8

14. Diff

So sánh code

git config --global user.email "[email protected]"  
git config --global user.username"lcdung"

9

15. Stash

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

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

0

V. Tips & Notes

List all ignore files:

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

1

Count modified files:

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

2

Ignore chmod file:

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

3

Some commands need to push with option -f:

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

4

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 config --global credential.helper cache git config credential.helper cache

    5 và

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

    6 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.