Tổng hợp một số câu lệnh khi sử dụng Git

Git là công cụ kiểm soát phiên bản dành cho các nhà phát triển phần mềm vì nó cho phép các lập trình viên quản lý hiệu quả mã nguồn và theo dõi các file thay đổi trong khi làm việc nhóm.



Trên thực tế, Git có rất nhiều cách sử dụng nên việc ghi nhớ các lệnh khác nhau của nó có thể là một nhiệm vụ khó khăn.
Trong bài viết này giới thiêu một số lệnh git hữu ích và thường gặp nhất trong quá trình sử dụng git



Tập hợp các bước thao tác hay sử dụng

B1 : Kéo code về
cd project_path
git clone project_url

Trong code thường được chia thành các nhánh : release, master, staging, dev, features. Với developers thường chỉ làm việc trên nhánh dev
git checkout dev

B2 : Tạo branch mới cho tính năng nào đó rồi thêm sửa xóa code gì đó
git checkout -b feature_branch
git add .
git status
git commit -m "completed feature"

Lưu ý trong lúc mình làm việc với feature thì đã có hàng tá các commit được đẩy lên nhánh dev

B3 : Tiến hành rebase nhánh hiện tại dựa trên nhánh dev
git checkout dev
git pull origin dev
git checkout feature_branch
git rebase dev
git rebase --continue

B4 : Tiến hành tích hợp code vào nhánh dev mới nhất
git checkout dev
git merge feature_branch --no-ff
git push origin dev
Xóa feature_branch (Nếu cần)
git branch -D feature_branch

Tiếp tục lặp lại các bước từ bước 2

B5 : Trong trường hợp muốn đưa code lên nhánh staging cho QA/QC kiểm tra
git checkout staging
git pull origin staging
git merge dev --no-ff
git push origin staging

B6 : Trong trường hợp đến giai đoạn triển khai,  tiến hành đưa code lên các nhánh quan trọng
git checkout master
git pull origin master
git merge dev --no-ff

Trong trường hợp có bug cần fix gấp
git checkout master
git pull origin master
git checkout -b hot_fix_branch
git add .
git checkout master
git merge hot_fix_branch --no-ff
git checkout dev
git merge hot_fix_branch --no-ff

Trong trường hợp đến giai đoạn release,  tiến hành đưa code từ master lên release
git checkout release
git pull origin release
git merge master --no-ff


Nhóm lệnh cơ bản xem thông tin

git --version
git --help
git --help config

git status
git status --short

git remote -v

git branch -avv

git config --list
git config --list --show-origin

git show commit

git log
git log -n
git log --oneline
git log --graph --oneline
git log --stat
git log --oneline --decorate
git log --since=2018-12-14
git log --until=2018-12-14
git log --author="user_name"
git log --pretty=format:"%C(yellow)%h%Creset %ad | %C(green)%s%Creset%C(red)%d%Creset %C(blue)[%an]" --graph --decorate --date=short'/
git log --grep="Something to search"
git log --no-merges
git log --merges

git diff
git diff --base
git diff commit or branch
git diff source_branch target_branch
git diff --staged
git diff --cached
git diff commit_id1 commit_id2
git diff --name-only commit_id1 commit_id2


git blame file_name
git grep "foo()"

Nhóm lệnh liên quan đến cấu hình

git config --list
git config --global --edit
git config --global user.name name
git config --global user.email email
git config credential.username username
git config --global core.editor "code --wait"
git config –global color.ui auto
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd "code --wait $MERGED"


Nhóm lệnh liên quan đến việc thay đổi nội dung

git init
git init repo_name

git clone repo_url
git clone -b branch_name --single-branch repo_url

git add .
git add file_name
git add *.cs

git rm file_to_remove_name
git mv old_file_name new_file_name


git rm filename

git commit -m "mesage"
git commit -am "message"
git commit --amend --no-edit

git checkout filename
git checkout -- filename
git checkout commit_id file_name
git checkout HEAD file_name
git checkout HEAD~2 file_name

git reset --hard origin/master

Nhóm lệnh liên quan đến remote repository

git fetch
git fetch origin
git fetch origin master

git pull
git pull origin
git pull origin master
git pull --rebase

git push
git push -f
git push --all origin
git push -u origin
git push origin :
git push origin -u
git push origin :
git push remote_name local_branch:remote_branch
git push origin master:master

git remote add origin
git remote rename old_name new_name
git remote remove remote_name
git remote set-url upstream new_repo_url

Nhóm lệnh làm việc với nhánh

Hiển thị thông tin nhánh
git branch
git branch -v
git branch --merged
git branch --no-merged

Tạo nhánh mới từ nhánh hiện tại và trên commit hiện tại
git checkout -b branch_name

Đổi tên nhánh
git branch -m old_branch_name new_branch_name

Xóa local branch
git branch -d branch_name
git branch -D branch_name

Xóa remote branch lưu ở local
git branch --delete --remotes remote_name/branch_name
git branch -d -r remote_name/branch_name

Xóa remote branch
git push --delete remote_name branch_name
git push remote_name --delete branch_name

Đẩy một nhánh từ local lên remote
git push remote_name local_branch_name:remote_branch_name

Gắn một remote branch với một local branch
git branch --set-upstream-to=origin/remote_branch local_branch
git branch -u origin/remote_branch local_branch

Reset

Đưa thay đổi về Working Directory
git reset --soft commit_id/HEAD~n
Đưa thay đổi về Staging
git reset --mixed commit_id/HEAD~n
git reset commit_id/HEAD~n
Xóa sạch các thay đổi
git reset --hard commit_id/HEAD~n

Tổng hợp code giữa các nhánh

Merge

Tích hợp theo kiểu merge từ nhánh có tên branch_source vào branch_destination
(Dùng 2 bản commit cuối cùng của từng nhánh rồi tích hợp lại với nhau tạo thành 1 commit mới theo kiểu hình thoi)
git checkout branch_destination
git merge branch_source

git merge master feature
git merge feature --no-ff

Rebase

Tổng hợp nhiều commit
git rebase -i HEAD~N
git rebase --continue
git rebase --abort

Tích hợp theo kiểu rebase từ nhánh có tên branch_source vào branch_destination
(Lấy code từ branch_source sau đó từ những commit ở đó tạo ra những commit tương tự lên branch_destination)
git checkout branch_destination
git rebase branch_source


Cherry Pick

git cherry-pick commit_id

Revert

 git revert commit_id
 git revert HEAD^
 git revert develop~4..develop~2

Stash

git stash
git stash push message
git stash list
git stash list -p
git stash pop
git stash apply stash@{index}
git stash show stash@{index}
git stash drop stash@{index}
git stash clear

Tag

git tag
git tag -a tag_name -m "Tag's mesage"
git show tag_name
git push origin tag_name
git push origin --tags
git push --delete origin tag_name
git tag -d tag_name


Flow 

git flow init
git flow feature start feature_name
git flow feature finish feature_name
git flow feature pull remote_name feature_name
git flow release start version
git flow release finish version
git flow hotfix start hotfix_name

Patch

git diff > patch-issue-1.patch
git diff --staged > patch-issue-2.patch
git diff HEAD > patch-issue-2.patch
git format-patch HEAD~n (Ví dụ: n=7, git sẽ tạo ra 7 file patch, mỗi file tương ứng với một commit kể từ commit hiện tại (HEAD). Đồng thời các file cũng được đánh số 0001-commit-message*.patch)
git apply --stat file.patch
git apply --check file.patch
git apply -v patch-name.patch
git am --signoff < file.patch
git am  < *.patch


Một số câu lệnh theo các tác vụ cụ thể

Xóa hoàn toàn một file khỏi Git và Git history
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch path_file_to_delete" --prune-empty --tag-name-filter cat -- --all
git push origin --force --all

Hủy tất cả các thay đổi và lấy về code mới nhất
git fetch origin
git reset --hard origin/master

Gộp các commit vào thành một commit duy nhất
git log --online
git reset --soft commit_id
git add
git commit -m "mesage"

Chỉnh sửa lại message của commit gần nhất
git commit --amend

Đưa một file hoặc folder từ Staging về Working Directory
git reset file_name/folder_name
git restore --staged file_name/folder_name

Khôi phục lại branch đã bị xóa
git reflog
git branch branch_backup commit_id
git checkout branch_backup
git reset --hard commit_id
git checkout master
git merge branch_backup

Xóa sạch một commit khỏi lịch sử
git rebase -i commit_id
pick chuyển thành drop

Khôi phục lại một commit
git reflog
git reset --hard commit_id

Khôi phục về trạng thái trước khi pull từ remote
git pull origin master
git reset --hard ORIG_HEAD

Xóa tất cả các file untracked
git clean  -d  -fx .

Khôi phục về trạng thái trước đó 1 commit
git reset HEAD~

Loại bỏ một file trong git nhưng vẫn tồn tại trong file system
C1 : git reset file_name
C2 : Đưa file_name vào .gitignore

Xem những files thay đổi tại một commit bất kỳ
git diff-tree -no-commit-id --name-only -r commit_id
git show --pretty="format:" --name-only commit_id

Xem thay đổi trước khi push
git diff --cached origin/master

Undo các commits chưa push
git reset origin/master

Xoá toàn bộ các files chưa đc track
Xóa files
git clean -f
Xóa cả files và folders
git clean -f -d
Xem trước khi xóa
git clean -n -f -d

git rebase --onto commit-id^ commit-id

Tham khảo thêm
https://git-scm.com/docs
https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet
http://ndpsoftware.com/git-cheatsheet.html
https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf
https://education.github.com/git-cheat-sheet-education.pdf
https://about.gitlab.com/images/press/git-cheat-sheet.pdf
https://www.atlassian.com/git
https://viblo.asia/p/tap-hop-nhung-cau-lenh-git-huu-dung-dWrvwWr2vw38
https://kipalog.com/posts/Cach-phan-nhanh-va-chia-viec-trong-nhom-voi-Git


Tiếp tục cập nhật ...