Lệnh này sẽ đăng ký url của remote repository với name, trong những lần làm việc tiếp theo ta sẽ dùng name thay cho url sẽ thuận tiện hơn khi làm việc với git.
Trong một số trường hợp bạn commit nhưng bị quên add một số file nào đó và bạn không muốn tạo ra một commit mới thì có thể sử dụng lệnh commit kết hợp tham số --amend để gộp các file đó và bổ sung vào commit cuối cùng, vì vậy không tạo ra commit mới.
$ git commit --amend
git reset HEAD
Loại bỏ file ra khỏi khỏi staging area
$ git reset HEAD <file-name>
# git reset HEAD index.html
Khởi tạo Repository
1. Tạo một repo trên
github
2. Copy URL của repo
3. Git init
Tạo một repo ở local, git sẽ tạo folder
.git
ở thư mục hiện tại.4. Push lên remote repository
git add
Add file vào staging area, để chuẩn bị commit
git commit
Khi dùng lệnh này những file trong staging area sẽ được lưu lại
git add remote
Lệnh này sẽ đăng ký url của remote repository với name, trong những lần làm việc tiếp theo ta sẽ dùng name thay cho url sẽ thuận tiện hơn khi làm việc với git.
git push
Push những thay đổi của bạn từ local branch lên remote server
Sao chép Repository có sẵn về local
git clone
git remote
Với lệnh
git clone
remote đã được add sẵn, dùng lệnhgit remote -v
để xem remote hiện tạiBranch
Xem các nhánh hiện tại
Tạo nhánh mới
Xóa nhánh
Chuyển nhánh
Đẩy nhánh mới lên remote repository
Kéo các nhánh mới ở remote repository về local repository
Một số lệnh khác
git log
Xem lịch sử commit
git commit --amend
Trong một số trường hợp bạn commit nhưng bị quên add một số file nào đó và bạn không muốn tạo ra một commit mới thì có thể sử dụng lệnh commit kết hợp tham số --amend để gộp các file đó và bổ sung vào commit cuối cùng, vì vậy không tạo ra commit mới.
git reset HEAD
Loại bỏ file ra khỏi khỏi staging area
Quay về một commit trước đó
--hard
sẽ mất hết thay đổi ở index và working tree,--soft
giữ những thay đổi ở index và working treegit checkout -- file
Phục hồi lại tình trạng của chính file đó ở lần commit gần nhất
git revert
Trở về một commit bất kỳ trước đó, hành động này sẽ được lưu lại ở commit history
git diff
Xem những thay đổi so với trước đó
git checkout 1 file
Đưa file về trang thái commit trước đó
Resovle conflict
Nhánh
master
có fileREADME.md
có nội dung như sauTạo nhánh
branch-A
và chuyển nhánh, sau đó edit fileREADME.md
với nội dung như sauSau đó commit lại
Quay trở về master và edit lại file
README.md
rồi commitTiến hành merge nhánh
branch-A
vàomaster
, sẽ gây conflictMở file
README.md
, bắt đầu giải quyết conflictỞ đây ta sẽ giữ lại code của cả 2 nhánh nên edit lại như sau:
Sau khi giải quyết conflict, dùng lệnh
git status
để xem trạng thái sau đó dùng lệnhgit commit
để tiến hành merge.Vậy ta đã xử lý conflict thành công.