mvn-quangtran-dn / GIT-PHP

0 stars 0 forks source link

G1_Nguyen_Viet_Hai #3

Open ghost opened 6 years ago

ghost commented 6 years ago

Khởi tạo Repository

1. Tạo một repo trên github

image

2. Copy URL của repo

# example: git@github.com:at-hainguyen5/git-exercise.git

image

3. Git init

Tạo một repo ở local, git sẽ tạo folder .git ở thư mục hiện tại.

$ git init

image

4. Push lên remote repository

$ echo "#this is my git exercise" >> README.md
$ git add .
$ git commit -m "init commit"
$ git remote add origin git@github.com:at-hainguyen5/git-exercise.git
$ git push origin master

image

git add

Add file vào staging area, để chuẩn bị commit

$ git add <file>
# git add index.html
# git add . (add hết các file được thay đổi)

git commit

Khi dùng lệnh này những file trong staging area sẽ được lưu lại

$ git commit -m <your-comment>
# git commit -m "add avatar for user"

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 remote add <name> <url>
# git remote add origin git@github.com:at-hainguyen5/git-exercise.git

git push

Push những thay đổi của bạn từ local branch lên remote server

$ git push <remote> <branch>
# git push origin master

Sao chép Repository có sẵn về local

git clone

$ git clone <your-repository-url>
# git clone git@github.com:at-hainguyen5/git-exercise.git

image

git remote

$ git remote -v

Với lệnh git clone remote đã được add sẵn, dùng lệnh git remote -v để xem remote hiện tại

image

Branch

Xem các nhánh hiện tại

$ git branch

image

Tạo nhánh mới

$ git branch <branch-name>
# hoặc tạo một nhánh mới và chuyển sang nhánh đó
$ git checkout -b <branch-name>

image

Xóa nhánh

$ git branch -D <branch-name>

image

Chuyển nhánh

$ git checkout <branch-name>

image

Đẩy nhánh mới lên remote repository

$ git push <remote> <branch>

image

Kéo các nhánh mới ở remote repository về local repository

$ git fetch <remote> <branch>
$ git branch -r # xem các nhánh mới đươc kéo về ở remote repository
$ git checkout <branch> # chuyển qua nhanh mới

image

Một số lệnh khác

git log

Xem lịch sử commit

$ git log
$ git log --oneline (xem log dưới dạng giản lược)

image

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 commit --amend

image

git reset HEAD

Loại bỏ file ra khỏi khỏi staging area

$ git reset HEAD <file-name>
# git reset HEAD index.html

image

Quay về một commit trước đó

$ git reset HEAD~<number-of-commit> <option>
# git reset HEAD~1 --hard

image

git 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 checkout -- <file-name>

image

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 revert <commit-hash>
# git revert 5212cb6

image

git diff

Xem những thay đổi so với trước đó

$ git diff

image

git checkout 1 file

Đưa file về trang thái commit trước đó

$ git checkout <hash-id> <path-to-file>

image

Resovle conflict

Nhánh master có file README.md có nội dung như sau

# this is my git exercise
second line

image

Tạo nhánh branch-A và chuyển nhánh, sau đó edit file README.md với nội dung như sau

# this is my git exercise
# this line will make a conflict 

image

Sau đó commit lại image

Quay trở về master và edit lại file README.md rồi commit

image

Tiến hành merge nhánh branch-A vào master, sẽ gây conflict

image

Mở file README.md, bắt đầu giải quyết conflict

# this is my git exercise
<<<<<<< HEAD
second line
third line
=======
# this line will make conflict

>>>>>>> branch-A

image Ở đây ta sẽ giữ lại code của cả 2 nhánh nên edit lại như sau:

# this is my git exercise
second line
third line
# this line will make conflict

Sau khi giải quyết conflict, dùng lệnh git status để xem trạng thái sau đó dùng lệnh git commit để tiến hành merge.

image Vậy ta đã xử lý conflict thành công.

mvn-quangtran-dn commented 6 years ago

Khá đầy đủ, rõ ràng , còn thiếu git reset commit, git checkout -- file

ghost commented 6 years ago

em đã update.

mvn-quangtran-dn commented 6 years ago