Git 基礎命令

一、基礎使用

1.克隆遠程倉庫(clone)

git clone https://github.com/hanhanyh/test.git
$ git clone https://github.com/hanhanyh/test.git
Cloning into 'test'...
remote: Enumerating objects: 33, done.
remote: Counting objects: 100% (33/33), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 33 (delta 0), reused 24 (delta 0), pack-reused 0
Unpacking objects: 100% (33/33), 3.77 KiB | 28.00 KiB/s, done.

2.新建文件 test3.txt 然後 查看狀態(status)

git status

 

On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test3.txt

nothing added to commit but untracked files present (use "git add" to track)

3.添加到暫存區 (add)

git add test3.txt

4.提交(commit)

git commit -m "提交測試"
[master f15ec71] 提交測試
 1 file changed, 1 insertion(+)
 create mode 100644 test3.txt

5.推送到遠程倉庫(push)

git push

二、分支

1.查看當前分支信息 (branch)

 git branch

2.創建一個分支 brtest

git branch brtest

3.切換分支到 brtest

 git checkout brtest

4.推送到遠程分支(沒有則創建分支)

 git push origin brtest

三、合併分支

1.切換分支brtest,創建並提交test4.txt

$ git add test4.txt

Administrator@WIN-07 MINGW64 ~/Desktop/gittest/test (brtest)
$ git commit -m "測試"
[brtest f418a42] 測試
 1 file changed, 1 insertion(+)
 create mode 100644 test4.txt

2.切換分支到master,並執行合併操作(merge)

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

Administrator@WIN-07  ~/Desktop/test (master)
$ git merge brtest
Updating f15ec71..f418a42
Fast-forward
 test4.txt | 1 +
 1 file changed, 1 insertion(+)

3.推送主分支到遠程倉庫(push)

$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 280 bytes | 280.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/hanhanyh/test.git
   f15ec71..f418a42  master -> master

四、解決衝突

如果自己先clone代碼,並在本地倉庫修改了一部分文件,在此期間其他合作開發者對相應的文件也進行了修改,並推送到服務器,此時去push代碼,就會有衝突conflict。需要先pull服務器倉庫代碼,解決衝突後,再次commit,push。

1.先pull拉取服務端最新brtest分支,完成後另一邊(我是直接在GitHub頁面上進行的)對test3.txt進行更改

 git pull origin brtest

2.提交代碼到brtest分支

 git push origin brtest

此時服務端拒絕提交

 ! [rejected]        brtest -> brtest (fetch first)
error: failed to push some refs to 'https://github.com/hanhanyh/test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

3.pull拉取brtest分支並解決衝突

$ git pull origin brtest
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 675 bytes | 51.00 KiB/s, done.
From https://github.com/hanhanyh/test
 * branch            brtest     -> FETCH_HEAD
   6acfea0..0000f86  brtest     -> origin/brtest
Auto-merging test3.txt
CONFLICT (content): Merge conflict in test3.txt
Automatic merge failed; fix conflicts and then commit the result.

3.打開test3.txt解決衝突

<<<<<<< HEAD
54654561654651123
=======
7456465456465dsdc
csdcsd4v56sdv465
csdcsdv45665csdcds
>>>>>>> 0000f86dc82bb60e278599584dc116bc4f0d3def

4.解決好後重新 add,commit ,push


$ git add test3.txt

Administrator@WIN-07 MINGW64 ~/Desktop/test (brtest|MERGING)
$ git commit -m "解決brtest分支衝突"
[brtest ed2b44a] 解決brtest分支衝突

Administrator@WIN-07 MINGW64 ~/Desktop/test (brtest)
$ git push origin brtest
Enumerating objects: 14, done.
Counting objects: 100% (14/14), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (8/8), 847 bytes | 423.00 KiB/s, done.
Total 8 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 1 local object.
To https://github.com/hanhanyh/test.git
   0000f86..ed2b44a  brtest -> brtest

此時成功提交

五、其它

1.列出遠程倉庫信息(remote)

$ git remote -v
origin  https://github.com/hanhanyh/test.git (fetch)
origin  https://github.com/hanhanyh/test.git (push)

原創文章,純個人理解,如有錯誤歡迎討論,勿噴。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章