赞
踩
1) git config | git基本配置 5) git init | 初始化Git仓库
2) git clone | 克隆远程代码 6) git remote | 远程源的设置
3) git fetch | 将远程更新拉取到本地 7) git commit | 提交缓存区修改到仓库
4) git rebase | 分支合并 8) git push | 将本地代码推送到远程
[xiaokang@localhost ~]$ sudo yum install git
1)生成SSH KEY
查看是否有公钥和私钥
- [xiaokang@localhost tmp]$ cd ~/.ssh/ # 切换到 .ssh目录
- [xiaokang@localhost .ssh]$ pwd
- /home/xiaokang/.ssh
- [xiaokang@localhost .ssh]$ ls
- known_hosts # 没有id_rsa和id_rsa.pub文件
生成公钥和私钥
- # 执行ssh-keygen 命令生成公私秘钥,遇到输入直接回车,直到完成退出
- [xiaokang@localhost .ssh]$ ssh-keygen
- [xiaokang@localhost .ssh]$ ll
- total 12
- -rw------- 1 xiaokang xiaokang 1679 Jun 20 02:42 id_rsa # 私钥
- -rw-r--r-- 1 xiaokang xiaokang 412 Jun 20 02:42 id_rsa.pub # 公钥
- -rw-r--r-- 1 xiaokang xiaokang 185 Jun 20 02:39 known_hosts
- [xiaokang@localhost .ssh]$ cat id_rsa.pub
2)访问码云配置SSH Key
点击个人中心选择设置-----》点击SSH公钥----》添加Linux系统中生成的公钥到里面。
3)git clone克隆码云中的远程仓库到本地 通过SSH
- [xiaokang@localhost tmp]$ git clone git@gitee.com:AllardZhao/xiaokang_test.git
- Cloning into 'xiaokang_test'...
- remote: Enumerating objects: 4, done.
- remote: Counting objects: 100% (4/4), done.
- remote: Compressing objects: 100% (4/4), done.
- remote: Total 4 (delta 0), reused 0 (delta 0)
- Receiving objects: 100% (4/4), done.
4)git remote远程源的设置,添加源和删除源,同一个系统允许多个源的设置
- [xiaokang@localhost tmp]$ cd xiaokang_test/ # 切换到克隆下来的目录中
- [xiaokang@localhost xiaokang_test]$ ls
- README.en.md README.md
- [xiaokang@localhost xiaokang_test]$ ll -al
- total 8
- drwxrwxr-x 3 xiaokang xiaokang 55 Jun 20 03:08 .
- drwxrwxrwt. 9 root root 277 Jun 20 03:08 ..
- drwxrwxr-x 8 xiaokang xiaokang 163 Jun 20 03:08 .git
- -rw-rw-r-- 1 xiaokang xiaokang 940 Jun 20 03:08 README.en.md
- -rw-rw-r-- 1 xiaokang xiaokang 1308 Jun 20 03:08 README.md
-
- [xiaokang@localhost xiaokang_test]$ git remote -v # 查看远程源
- origin git@gitee.com:AllardZhao/xiaokang_test.git (fetch)
- origin git@gitee.com:AllardZhao/xiaokang_test.git (push)
- # 添加远程源newyuan
- [xiaokang@localhost xiaokang_test]$ git remote add newyuan git@gitee.com:AllardZhao/xiaokang_test.git
- [xiaokang@localhost xiaokang_test]$ git remote -v 查看远程源
- newyuan git@gitee.com:AllardZhao/xiaokang_test.git (fetch)
- newyuan git@gitee.com:AllardZhao/xiaokang_test.git (push)
- origin git@gitee.com:AllardZhao/xiaokang_test.git (fetch)
- origin git@gitee.com:AllardZhao/xiaokang_test.git (push)
- # 删除远程源newyuan
- [xiaokang@localhost xiaokang_test]$ git remote rm newyuan
- [xiaokang@localhost xiaokang_test]$ git remote -v
- origin git@gitee.com:AllardZhao/xiaokang_test.git (fetch)
- origin git@gitee.com:AllardZhao/xiaokang_test.git (push)
- [xiaokang@localhost xiaokang_test]$
(一)如何将克隆到本地仓库中修改内容push远程仓库 和 获取远程仓库修改内容到本地仓库(远程------>本地)
(此时本地是没有仓库的,先通过git clone命令将远程仓库克隆到本地来,然后进行的以下操作)
(1)查看修改状态
- [xiaokang@localhost xiaokang_test]$ git status
- # 第一次提交会提示你需要设置个人信息才能查看
- [xiaokang@localhost xiaokang_test]$ git config --global user.email "AllardZhao@163.com"
- [xiaokang@localhost xiaokang_test]$ git config --global user.name "xiaokang"
提交修改内容到本地主分支master上并push到码云管理库
- [xiaokang@localhost xiaokang_test]$ git commit -am "vincent -- modify readme"
- [master 50a6f21] vincent -- modify readme
- 1 file changed, 4 insertions(+), 1 deletion(-)
-
- # 将本地代码master分支推送到远程master分支, origin是远程仓库的别名
- [xiaokang@localhost xiaokang_test]$ git push origin master:master
(2)获取码云管理库中修改内容到本地
- [xiaokang@localhost xiaokang_test]$ git fetch # 拉取远程仓库新增内容到本地
- 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)
- Unpacking objects: 100% (3/3), done.
- From gitee.com:AllardZhao/xiaokang_test
- 50a6f21..1934e6f master -> origin/master
-
- # 拉取内容合并到origin中主分支master上
- [xiaokang@localhost xiaokang_test]$ git rebase origin/master
- First, rewinding head to replay your work on top of it...
- Fast-forwarded master to origin/master.
(二)如何在本地创建仓库 并 将本地库中内容push远程管理仓库中(本地------>远程)
(先在本地创建文件夹,接着初始化成仓库,此时本地已有仓库。然后进行的如下操作)
- [xiaokang@localhost tmp]$ mkdir xiaokang_test2 # 创建文件夹xiaokang_test2
- [xiaokang@localhost tmp]$ cd xiaokang_test2
- [xiaokang@localhost xiaokang_test2]$ ll -al
- total 4
- drwxrwxr-x 2 xiaokang xiaokang 6 Jun 20 03:47 .
- drwxrwxrwt. 10 root root 4096 Jun 20 03:47 ..
- # 初始化xiaokang_test2为仓库
- [xiaokang@localhost xiaokang_test2]$ git init
- Initialized empty Git repository in /tmp/xiaokang_test2/.git/
- [xiaokang@localhost xiaokang_test2]$ ll -al
- total 4
- drwxrwxr-x 3 xiaokang xiaokang 18 Jun 20 03:48 .
- drwxrwxrwt. 10 root root 4096 Jun 20 03:47 ..
- drwxrwxr-x 7 xiaokang xiaokang 119 Jun 20 03:48 .git
- # 文件夹中有.git文件就是Git仓库
- [xiaokang@localhost xiaokang_test2]$ touch a.py # 创建a.py文件
- [xiaokang@localhost xiaokang_test2]$ ls -al
- total 4
- drwxrwxr-x 3 xiaokang xiaokang 30 Jun 20 03:50 .
- drwxrwxrwt. 10 root root 4096 Jun 20 03:47 ..
- -rw-rw-r-- 1 xiaokang xiaokang 0 Jun 20 03:50 a.py
- drwxrwxr-x 7 xiaokang xiaokang 119 Jun 20 03:48 .git
- [xiaokang@localhost xiaokang_test2]$ git status # 查看仓库状态
- [xiaokang@localhost xiaokang_test2]$ git add . # 添加所有修改内容缓存区
- # 把添加到缓存区内容提交到本地仓库
- [xiaokang@localhost xiaokang_test2]$ git commit -am "vincent --init"
- [master (root-commit) c5d4756] vincent --init
- 1 file changed, 0 insertions(+), 0 deletions(-)
- create mode 100644 a.py
-
- # 添加远程仓库地址即源
- [xiaokang@localhost xiaokang_test2]$ git remote add origin git@gitee.com:AllardZhao/xiaokang_test2.git
- [xiaokang@localhost xiaokang_test2]$ git remote -v
- origin git@gitee.com:AllardZhao/xiaokang_test2.git (fetch)
- origin git@gitee.com:AllardZhao/xiaokang_test2.git (push)
- # push本地master主干分支内容到远程仓库master中
- [xiaokang@localhost xiaokang_test2]$ git push origin master:master
- To git@gitee.com:AllardZhao/xiaokang_test2.git
- ! [rejected] master -> master (fetch first)
- error: failed to push some refs to 'git@gitee.com:AllardZhao/xiaokang_test2.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 merge the remote changes (e.g.,
- hint: 'git pull') before pushing again.
- hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# 根据已上报错得知需要先把远程仓库中的内容合并到本地,然后再push
- # 根据已上报错得知需要先把远程仓库中的内容合并到本地,然后再push
- [xiaokang@localhost xiaokang_test2]$ git fetch # 获取远程仓库修改内容
- warning: no common commits
- remote: Enumerating objects: 4, done.
- remote: Counting objects: 100% (4/4), done.
- remote: Compressing objects: 100% (4/4), done.
- remote: Total 4 (delta 0), reused 0 (delta 0)
- Unpacking objects: 100% (4/4), done.
- From gitee.com:AllardZhao/xiaokang_test2
- * [new branch] master -> origin/master
- # 将远程仓库内容合并到 origin/master主干分支
- [xiaokang@localhost xiaokang_test2]$ git rebase origin/master
- First, rewinding head to replay your work on top of it...
- Applying: vincent --init
- # push本地内容到远程仓库
- [xiaokang@localhost xiaokang_test2]$ git push origin master:master
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。