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)

原创文章,纯个人理解,如有错误欢迎讨论,勿喷。

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