Các lệnh git hay dùng trong dự án thực tế

  • May 30, 2021
  • 1597

Các lệnh git hay dùng trong dự án thực tế mà mọi lập trình viên phải biết. Bài viết này mình sẽ liệt kê cách lệnh git mình hay dùng trong các dự án mà mình từng làm, hi vọng giúp ích cho mọi người.

1. Git config

Dùng để set user name và email của bạn trong main configuration file


// Config username, email cho mọi project
git config --global user.name "John Doe"
git config --global user.email "john@example.com"

// Config username, email cho 1 project
git config user.name "John Doe"
git config user.email "john@example.com"

// Xem các config
git config --list

2. Git init

Khởi tạo 1 git repository cho 1 project mới chưa sử dụng git, cần phải init mới dùng được các lệnh trong git


cd project
git init

3. Git clone

Dùng để lấy code từ remote repository về máy


git clone [remmote_url]
git clone git@github.com:User/UserRepo.git

4. Quản lý remote repository


// Kiểm tra remote repo đang dùng
git remote -v

// Thêm remote repo
git remote add  [remote_name] [remote_url]
git remote add origin git@github.com:User/UserRepo.git

// Đổi remote repo
git remote set-url [remote_name] [remote_url]
git remote set-url origin [remote_url]

// Xoá remote
git remote remove origin

5. Các lệnh liên quan đến branch


// Kiểm tra xem đang đứng ở branch nào
git branch

// Chuyển sang 1 nhánh đã tồn tại
git checkout [branch_name]
// Chuyển sang branch develop
git checkout develop

// Tạo nhánh mới
git branch [branch_name]

// Đổi tên branch
git branch -m [branch_name]

// Tạo 1 nhánh mới và chuyển sang nhánh mới luôn
git checkout -b [branch_name]
// Vd tạo 1 branch mới từ branch develop git checkout develop git pull git checkout -b [new_branch_name]

6. git status

Kiểm tra các file thay đổi trong project khi bạn thay đổi code


git status

7. Xem thay đổi code


// Xem các file thay đổi
git status

// Xem code thay đổi
git diff [name_file_change]

8. Xem log

Xem lịch xử commit, giúp xem cách đặt tên branch, cách đặt tên commit


git log
git log --oneline

9. Commit code

Commit các code thay đổi


// Thêm tất cả các thay đổi trong thư mục code
git add .
// Tạo 1 commit
git commit -am "comment"

// Sửa một commit
git commit --amemd -m "new commit"

10. Gộp commit

Gộp commit sau với commit trước đó


// Thêm các thay đổi
git add .
// Gộp commit sau với commit trước
git commit --amend
// Thoát để gộp với commit trước
Nhấn phím q!
// Commit lại code lên branch remote
git push -f

11. Push code lên remote repository

Đẩy code lên branch remote repo cho mọi người pull code về để sử dụng


git push [remote_name] [branch_name]

// Vd
git push origin develop

12. Pull code từ remote repository về local

Lấy code từ branch remote repo về branch local máy tính của mình


git pull [branch_name]

// Vd
git pull develop

13. Merge code

Có 2 cách merge code, sử dụng git merge hoặc git rebase. Điểm khác nhau giữa 2 lệnh là rebase sẽ lấy commit của nhánh rebase làm cơ sở, nên các commit được sắp xếp theo đúng thứ tự merge, còn merge thì không


// Merge code với nhánh develop
git merge develop

// Rebase code với nhánh develop
git rebase develop

14. Git stash

Thêm các thay đổi vào stash, các thay đổi này sẽ không được thêm vào commit


// Thêm các thay đổi không muốn commit vào stash
git stash

// Lấy các thay đổi từ stash vào commit
git stash pop

// Xoá các thay đổi lưu trong stash
git stash drop

15. Get fetch

Lấy các branch trên remote repo về local


git fetch [remote_name]
git fetch orign

16. Remove cache gitignore

Khi ignore 1 file đã được add vào git, cần phải remove cache gitignore để loại bỏ file ra khỏi commit


# remove specific file from git cache
git rm --cached filename

# remove all files from git cache
git rm -r --cached .
git add .
git commit -m ".gitignore is now working"

17. Remove all history commit


# Checkout to new branch
git checkout --orphan latest_branch

# Add all the files
git add .

# Change commit
git commit -am "init project"

#Delete the branch
git branch -D master

# Rename the current branch to master
git branch -m master

# Finally, force update your repository
git push -f origin master

Trên đây là tất cả các lệnh git mà mình hay dùng. Các bạn còn hay dùng command nào nữa thì comment phía bên dưới nhé. Thank you :D