当前位置:   article > 正文

Linux下Git版本管理工具使用及远程仓库操作_remote: enumerating objects: 48091, done. remote:

remote: enumerating objects: 48091, done. remote: counting objects: 100% (46

1.Git常用命令

     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 | 将本地代码推送到远程

2.安装: yum install git

[xiaokang@localhost ~]$ sudo yum install git

3.Git使用

  1)生成SSH KEY

       查看是否有公钥和私钥

  1. [xiaokang@localhost tmp]$ cd ~/.ssh/ # 切换到 .ssh目录
  2. [xiaokang@localhost .ssh]$ pwd
  3. /home/xiaokang/.ssh
  4. [xiaokang@localhost .ssh]$ ls
  5. known_hosts # 没有id_rsa和id_rsa.pub文件

    生成公钥和私钥

  1. # 执行ssh-keygen 命令生成公私秘钥,遇到输入直接回车,直到完成退出
  2. [xiaokang@localhost .ssh]$ ssh-keygen
  3. [xiaokang@localhost .ssh]$ ll
  4. total 12
  5. -rw------- 1 xiaokang xiaokang 1679 Jun 20 02:42 id_rsa # 私钥
  6. -rw-r--r-- 1 xiaokang xiaokang 412 Jun 20 02:42 id_rsa.pub # 公钥
  7. -rw-r--r-- 1 xiaokang xiaokang 185 Jun 20 02:39 known_hosts
  8. [xiaokang@localhost .ssh]$ cat id_rsa.pub

 2)访问码云配置SSH Key

        点击个人中心选择设置-----》点击SSH公钥----》添加Linux系统中生成的公钥到里面。

3)git clone克隆码云中的远程仓库到本地 通过SSH

  1. [xiaokang@localhost tmp]$ git clone git@gitee.com:AllardZhao/xiaokang_test.git
  2. Cloning into 'xiaokang_test'...
  3. remote: Enumerating objects: 4, done.
  4. remote: Counting objects: 100% (4/4), done.
  5. remote: Compressing objects: 100% (4/4), done.
  6. remote: Total 4 (delta 0), reused 0 (delta 0)
  7. Receiving objects: 100% (4/4), done.

4)git remote远程源的设置,添加源和删除源,同一个系统允许多个源的设置

  1. [xiaokang@localhost tmp]$ cd xiaokang_test/ # 切换到克隆下来的目录中
  2. [xiaokang@localhost xiaokang_test]$ ls
  3. README.en.md README.md
  4. [xiaokang@localhost xiaokang_test]$ ll -al
  5. total 8
  6. drwxrwxr-x 3 xiaokang xiaokang 55 Jun 20 03:08 .
  7. drwxrwxrwt. 9 root root 277 Jun 20 03:08 ..
  8. drwxrwxr-x 8 xiaokang xiaokang 163 Jun 20 03:08 .git
  9. -rw-rw-r-- 1 xiaokang xiaokang 940 Jun 20 03:08 README.en.md
  10. -rw-rw-r-- 1 xiaokang xiaokang 1308 Jun 20 03:08 README.md
  11. [xiaokang@localhost xiaokang_test]$ git remote -v # 查看远程源
  12. origin git@gitee.com:AllardZhao/xiaokang_test.git (fetch)
  13. origin git@gitee.com:AllardZhao/xiaokang_test.git (push)
  1. # 添加远程源newyuan
  2. [xiaokang@localhost xiaokang_test]$ git remote add newyuan git@gitee.com:AllardZhao/xiaokang_test.git
  3. [xiaokang@localhost xiaokang_test]$ git remote -v 查看远程源
  4. newyuan git@gitee.com:AllardZhao/xiaokang_test.git (fetch)
  5. newyuan git@gitee.com:AllardZhao/xiaokang_test.git (push)
  6. origin git@gitee.com:AllardZhao/xiaokang_test.git (fetch)
  7. origin git@gitee.com:AllardZhao/xiaokang_test.git (push)
  8. # 删除远程源newyuan
  9. [xiaokang@localhost xiaokang_test]$ git remote rm newyuan
  10. [xiaokang@localhost xiaokang_test]$ git remote -v
  11. origin git@gitee.com:AllardZhao/xiaokang_test.git (fetch)
  12. origin git@gitee.com:AllardZhao/xiaokang_test.git (push)
  13. [xiaokang@localhost xiaokang_test]$

4.Git远程仓库操作

(一)如何将克隆到本地仓库中修改内容push远程仓库 和 获取远程仓库修改内容到本地仓库(远程------>本地)

(此时本地是没有仓库的,先通过git clone命令将远程仓库克隆到本地来,然后进行的以下操作)

(1)查看修改状态

  1. [xiaokang@localhost xiaokang_test]$ git status
  2. # 第一次提交会提示你需要设置个人信息才能查看
  3. [xiaokang@localhost xiaokang_test]$ git config --global user.email "AllardZhao@163.com"
  4. [xiaokang@localhost xiaokang_test]$ git config --global user.name "xiaokang"

 

    提交修改内容到本地主分支master上并push到码云管理库

  1. [xiaokang@localhost xiaokang_test]$ git commit -am "vincent -- modify readme"
  2. [master 50a6f21] vincent -- modify readme
  3. 1 file changed, 4 insertions(+), 1 deletion(-)
  4. # 将本地代码master分支推送到远程master分支, origin是远程仓库的别名
  5. [xiaokang@localhost xiaokang_test]$ git push origin master:master

 

(2)获取码云管理库中修改内容到本地

  1. [xiaokang@localhost xiaokang_test]$ git fetch # 拉取远程仓库新增内容到本地
  2. remote: Enumerating objects: 5, done.
  3. remote: Counting objects: 100% (5/5), done.
  4. remote: Compressing objects: 100% (3/3), done.
  5. remote: Total 3 (delta 1), reused 0 (delta 0)
  6. Unpacking objects: 100% (3/3), done.
  7. From gitee.com:AllardZhao/xiaokang_test
  8. 50a6f21..1934e6f master -> origin/master
  9. # 拉取内容合并到origin中主分支master上
  10. [xiaokang@localhost xiaokang_test]$ git rebase origin/master
  11. First, rewinding head to replay your work on top of it...
  12. Fast-forwarded master to origin/master.

 

(二)如何在本地创建仓库 并 将本地库中内容push远程管理仓库中(本地------>远程)

    (先在本地创建文件夹,接着初始化成仓库,此时本地已有仓库。然后进行的如下操作)

  1. [xiaokang@localhost tmp]$ mkdir xiaokang_test2 # 创建文件夹xiaokang_test2
  2. [xiaokang@localhost tmp]$ cd xiaokang_test2
  3. [xiaokang@localhost xiaokang_test2]$ ll -al
  4. total 4
  5. drwxrwxr-x 2 xiaokang xiaokang 6 Jun 20 03:47 .
  6. drwxrwxrwt. 10 root root 4096 Jun 20 03:47 ..
  7. # 初始化xiaokang_test2为仓库
  8. [xiaokang@localhost xiaokang_test2]$ git init
  9. Initialized empty Git repository in /tmp/xiaokang_test2/.git/
  10. [xiaokang@localhost xiaokang_test2]$ ll -al
  11. total 4
  12. drwxrwxr-x 3 xiaokang xiaokang 18 Jun 20 03:48 .
  13. drwxrwxrwt. 10 root root 4096 Jun 20 03:47 ..
  14. drwxrwxr-x 7 xiaokang xiaokang 119 Jun 20 03:48 .git
  15. # 文件夹中有.git文件就是Git仓库
  1. [xiaokang@localhost xiaokang_test2]$ touch a.py # 创建a.py文件
  2. [xiaokang@localhost xiaokang_test2]$ ls -al
  3. total 4
  4. drwxrwxr-x 3 xiaokang xiaokang 30 Jun 20 03:50 .
  5. drwxrwxrwt. 10 root root 4096 Jun 20 03:47 ..
  6. -rw-rw-r-- 1 xiaokang xiaokang 0 Jun 20 03:50 a.py
  7. drwxrwxr-x 7 xiaokang xiaokang 119 Jun 20 03:48 .git
  8. [xiaokang@localhost xiaokang_test2]$ git status # 查看仓库状态
  9. [xiaokang@localhost xiaokang_test2]$ git add . # 添加所有修改内容缓存区
  1. # 把添加到缓存区内容提交到本地仓库
  2. [xiaokang@localhost xiaokang_test2]$ git commit -am "vincent --init"
  3. [master (root-commit) c5d4756] vincent --init
  4. 1 file changed, 0 insertions(+), 0 deletions(-)
  5. create mode 100644 a.py
  6. # 添加远程仓库地址即源
  7. [xiaokang@localhost xiaokang_test2]$ git remote add origin git@gitee.com:AllardZhao/xiaokang_test2.git
  8. [xiaokang@localhost xiaokang_test2]$ git remote -v
  9. origin git@gitee.com:AllardZhao/xiaokang_test2.git (fetch)
  10. origin git@gitee.com:AllardZhao/xiaokang_test2.git (push)
  1. # push本地master主干分支内容到远程仓库master中
  2. [xiaokang@localhost xiaokang_test2]$ git push origin master:master
  3. To git@gitee.com:AllardZhao/xiaokang_test2.git
  4. ! [rejected] master -> master (fetch first)
  5. error: failed to push some refs to 'git@gitee.com:AllardZhao/xiaokang_test2.git'
  6. hint: Updates were rejected because the remote contains work that you do
  7. hint: not have locally. This is usually caused by another repository pushing
  8. hint: to the same ref. You may want to first merge the remote changes (e.g.,
  9. hint: 'git pull') before pushing again.
  10. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

   # 根据已上报错得知需要先把远程仓库中的内容合并到本地,然后再push

  1. # 根据已上报错得知需要先把远程仓库中的内容合并到本地,然后再push
  2. [xiaokang@localhost xiaokang_test2]$ git fetch # 获取远程仓库修改内容
  3. warning: no common commits
  4. remote: Enumerating objects: 4, done.
  5. remote: Counting objects: 100% (4/4), done.
  6. remote: Compressing objects: 100% (4/4), done.
  7. remote: Total 4 (delta 0), reused 0 (delta 0)
  8. Unpacking objects: 100% (4/4), done.
  9. From gitee.com:AllardZhao/xiaokang_test2
  10. * [new branch] master -> origin/master
  11. # 将远程仓库内容合并到 origin/master主干分支
  12. [xiaokang@localhost xiaokang_test2]$ git rebase origin/master
  13. First, rewinding head to replay your work on top of it...
  14. Applying: vincent --init
  15. # push本地内容到远程仓库
  16. [xiaokang@localhost xiaokang_test2]$ git push origin master:master

 

以上是关于Git使用的基本操作步骤已经很详细,配有操作说明帮助理解

 

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/510987
推荐阅读
相关标签
  

闽ICP备14008679号