Dưới đấy là một vài ba ví dụ về những câu lệnh Git nhưng tôi thông thường sử dụng.
Git config
git config -global user.name “John Doe” git config -global user.email “john@example.com”
-global được dùng để vận dụng cho toàn bộ những projects. Nếu khách du lịch ko dùng -global thì settings sẽ chỉ sử dụng cho riêng project đấy.
Hỗ trợ Git bỏ qua file modes
cd project/ git config core.filemode false
Câu lệnh trên hữu dụng lúc chúng ta ko cần sử dụng rộng rãi tới quyền truy vấn files (ví dụ như lúc dùng Windows).
Liệt kê những settings đang dùng
git config -list
Khởi tạo Git repo cho code sở hữu sẵn
cd existing-project/ git init
Clone một remote repo
git clone https://github.com/user/repository.git
Câu lệnh trên sẽ tạo một thư mục thế hệ sở hữu tên tương tự trên của repo.
Clone một remote repo tại thư mục hiện nay
git clone https://github.com/user/repository.git .
Coi thông tin trợ tạo điều kiện cho một câu lệnh git
git help clone
Update và merge branch hiện nay với một remote repo
cd repo/ git pull origin master
với origin là remote repo, master là remote branch.
Nếu khách du lịch ko mong muốn merge những thay đổi của khách du lịch, hãy dùng git fetch
Liệt kê những remote urls
git remote -v
Thay đổi origin url
git remote set-url origin https://github.com/repo.git
Thêm remote repo
git remote add remote-name https://github.com/user/repo.git
Coi thay đổi (chưa được add) của những file hiện nay
git diff
Coi thay đổi (đang được add, chưa commit)
git diff -cached
Coi thay đổi thân local nhưng master
git diff origin/master
Coi thay đổi thân nhị commits
git diff COMMIT1_ID COMMIT2_ID
Coi những files thay đổi thân nhị commits
git diff -name-only COMMIT1_ID COMMIT2_ID
Coi những files thay đổi tại một commit ngẫu nhiên
git diff-tree -no-commit-id -name-only -r COMMIT_ID
hoặc
git show -pretty=”format:” -name-only COMMIT_ID
Coi thay đổi trước lúc push
git diff -cached origin/master
Coi thông tin rõ ràng của một commit
git show COMMIT_ID
Rà soát status của working tree
git status
Tạo vài ba thay đổi, rồi commit
git add changed_file.txt git add folder-with-changed-files/ git commit -m “Commiting changes”
Đổi tên/Vận chuyển/Xoá files
git rm removeme.txt tmp/crap.txt git mv file oldname.txt file_newname.txt git commit -m “deleteing 2 files, renaming 1”
Đổi message của commit cuối
git commit -amend -m “New commit mesage”
Push local commits quý phái nhánh remote
git push origin master
Coi commit history
git log
Coi commit history cho nhị commits sắp nhất
git log -2
Coi commit history cho nhị commits sắp nhất, bao hàm cả thay đổi
git log -p -2
Coi commit history dưới dạng một loại
git log -pretty=oneline
Revert một commit rồi push
git revert COMMIT_ID git push origin master
Revert tới thời khắc trước một commit
git reset COMMIT_ID git reset -soft HEAD@{1} git commit -m “Revert to COMMIT_ID” git reset -hard
Undo commit sắp nhất, vẫn giữ thay đổi ở local
git reset -soft HEAD~1
Undo commit sắp nhất, ko giữ thay đổi ở local
git reset -hard HEAD~1
Undo commit sắp nhất, vẫn giữ thay đổi ở index
git reset -mixed HEAD~1
hoặc
git reset HEAD~1
Undo commits chưa push
git reset origin/master
Reset về tình hình của remote
git fetch origin git reset -hard origin/master
Coi những nhánh local
git branch
Coi toàn bộ những nhánh
git branch -a
Tạo một patch
git diff > patch-issue-1.patch
Thêm một file rồi tạo patch
git add newfile git diff -staged > patch-issue-2.patch
Thêm một file, thay đổi rồi tạo patch
git add newfile git diff HEAD > patch-issue-2.patch
Tạo patch từ một commit
git format-patch COMMIT_ID
Tạo patch từ nhị commit cuối
git format-patch HEAD~2
Tạo patch từ toàn bộ những commits chưa push
git format-patch origin/master
Tạo patch chứa data nhị phân
git format-patch -binary -full-index origin/master
Apply một patch
git apply -v patch-name.patch
Apply một patch được tạo bằng format-patch
git am patch1.patch
Tạo một tag
git tag 7.x-1.3
Push một tag
git push origin 7.x-1.3
Tạo một nhánh
git checkout master git branch new-branch-name
Xem xét với nhị câu lệnh trên thì chúng ta chưa gửi quý phái nhánh thế hệ, nhưng vẫn ở nhánh master. Phải dùng thêm git checkout new-branch-name để gửi nhánh.
Ngoài ra sở hữu thể tạo nhánh thế hệ và gửi quý phái luôn luôn bằng 1 câu git checkout -b new-branch-name
Đưa nhánh
git checkout new-branch-name
Coi commit history so sánh với branch hiện nay
git cherry -v master
master ở đấy là branch nhưng khách du lịch mong muốn so sánh sánh
Merge commit từ branch khác
git checkout master git merge branch-name
Tại đây chúng ta merge những commits của branch-name vào master.
Merge branch nhưng ko commit
git merge branch-name -no-commit -no-ff
Coi thay đổi thân state hiện nay và một branch
git diff branch-name
Coi thay đổi trong một file, thân state hiện nay và một branch
git diff branch-name path/to/file
Xoá branch
git branch -d branch-name
Push lên một branch
git push origin branch-name
Lấy toàn bộ những branches
git fetch orgin
Lấy thư mục root
git rev-parse -show-toplevel
Xoá những file bị xoá ở local trên repo
git rm $(git ls-files -deleted)
Xoá toàn thể những files chưa được track
git clean -f
xoá cả thư mục:
git clean -f -d
coi những file trước lúc xoá:
git clean -n -f -d
Unstage những files
git reset HEAD file.txt
Coi tag sắp nhất
git describe -tags `git rev-list -tag -max-count=1`
Liệt kê những nhánh theo trình tự dùng sắp nhất
git for-each-ref -sort=-committerdate refs/heads/ | head
Tar cả project, ngoại trừ thư mục .git
cd .. tar cJf project.tar.xz project/ -exclude-vcs
Tar toàn bộ những files bị thay đổi ở local
git diff -name-only | xargs tar -cf project.tar -T –
Tìm conflict
grep -H -r “<<<” * grep -H -r “>>>” * grep -H -r ‘^=======$’ *
Apply một patch ko dùng git
patch < file.patch
Source: The most useful git commands