zoukankan      html  css  js  c++  java
  • 本地Git仓库与GitHub/GitLab仓库同步

      本地仓库即为在你的电脑上的项目文件,远程仓库即为服务器仓库,如GitHub、GitLab或其他等。此处以GitHub介绍本地仓库与远程仓库的同步。可先创建本地仓库,也可先创建GitHub仓库,但都需要创建一个GitHub仓库。若先创建了GitHub仓库,则使用前述git clone命令创建本地仓库。

    一、创建一个GitHub仓库

      在GitHub(https://github.com/)上创建账号,并按下图创建GitHub仓库:

    二、创建Git本地仓库

      安装最新版的Git,并设置Git的username和email address后,使用<创建Git本地仓库>部分的内容创建Git本地仓库:使用git init初始化一个已有项目目录或使用git clone一个远程仓库。

    三、使用HTTPS授权GitHub

      如果以HTTPS与GitHub链接,可使用一个credentials helper来在Git中cache你的GitHub用户名和密码。

    1、先打开该credentials helper,以便Git保存你的用户名和密码。

      默认情况下Git会在15min内cache你的用户名和密码。

    $ git config --global credential.helper cache
    # Set git to use the credential memory cache

    2、再更改默认的密码cache时间:

    $ git config --global credential.helper 'cache --timeout=3600'
    # Set the cache to timeout after 1 hour (setting is in seconds)

    四、使用SSH授权GitHub

      如果以SSH与GitHub链接,在每个从GitHub使用push或pull的电脑上都必须生成SSH keys。

    1、生成一个新的SSH key

      ①、使用以下命令在终端生成一个新的ssh key,注意以你自己的GitHub email address代替:

    $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

      ②、当提示“Enter a file in which to save the key,"时,按下Enter键,即以默认文件位置保存。

      ③、当提示”type a passphrase"时,键入安全密码:

      以上就生成了所需的public ssh key.

     2、将你的SSH key添加到ssh-agent

      如果不想每次使用你的SSH key时重复输入你的密码,可以将你的SSH key添加到SSH agent,该agent将管理你的SSH key并保存你的密码。

      ①、使用以下命令在终端打开该ssh-agent:

    $ eval "$(ssh-agent -s)"

      ②、将你的SSH private key添加到该ssh-agent。

    $ ssh-add ~/.ssh/id_rsa

      ③、将该SSH key添加到你的GitHub账户。

    • 复制你的SSH key.
    $ sudo apt-get install xclip
    # Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`)
    $ xclip -sel clip < ~/.ssh/id_rsa.pub
    # Copies the contents of the id_rsa.pub file to your clipboard
    • 在GitHub页面,在右上角点击你的头像,再点击Settings:

    • 在用户设置面板,点击SSH and GPG keys,再点击New SSH key或Add SSH key:

    • 输入该新的SSH key的名称,粘贴你的SSH key到“Key",再点击Add SSH key即可(可能需要输入密码验证)。

      

      注意:使用HTTPS或SSH均可链接到GitHub,一般使用HTTPS会快一些,推荐使用HTTPS。因此只需要设置其中一种即可,即只执行<三>或<四>之一即可。

    五、将本地仓库与GitHub仓库进行关联

      在命令行,切换到本地仓库所在的项目目录下,使用以下指令实现本地仓库与GitHub仓库的关联:

    $ git remote add origin git@github.com:yanqingyang/learning_git.git
    $ git push -u origin master

    $ git remote add origin git@github.com:yanqingyang/learning_git.git
    $ git push -u origin master

    六、总结

      将Git本地仓库与GitHub远程仓库进行同步有以下几个步骤:

    1. 创建GitHub远程仓库;
    2. 创建Git本地仓库(1和2可互换);
    3. 授权GitHub,以HTTPS或SSH方式;
    4. 将Git本地仓库与GitHub远程仓库对应起来;

      此外,将本地Git仓库与GitLab仓库进行关联并同步,其过程与上完全一致,重点在于以HTTPS或SSH链接部分的设置。

     

  • 相关阅读:
    分水岭分割算法(watershed segmentation)的C++实现(法2)
    ubuntu16.04下安装opencv3.3
    分水岭分割算法(watershed segmentation)的C++实现(法1)
    dpkg: 处理归档 /var/cache/apt/archives/swig2.0_2.0.12-1ubuntu4_amd64.deb (--unpack)时出错:
    ubuntu16.04安装pycharm
    ImportError: liblapack.so.3: cannot open shared object file问题
    Linux下使用Opencv打开笔记本摄像头
    目标跟踪算法meanshift优缺点
    Jacobian矩阵和Hessian矩阵
    机器视觉中的目标检测
  • 原文地址:https://www.cnblogs.com/yanqingyang/p/9839664.html
Copyright © 2011-2022 走看看