Git添加SSh公钥以及多人协作冲突问题解决方案

来源:互联网 发布:河南奔驰网络办公平台 编辑:程序博客网 时间:2024/06/15 16:17

现在加入有两台机器想访问gitee(与github类似,csdn的代码托管平台,很好用^_^,推荐一波)

将两台电脑的SSH公钥添加到gitee中

这里我们用两种系统分别为Windows和Centos

Window

如果装过git,右键桌面,选择如下的红色标记的地方,没装的话。。(还用说么,下载安装)
这里写图片描述
这里会进入bash 命令框

  1. 配置全局的用户名和邮箱,这个必须要配置,不然git没法commit,当然也可以单独配置每个工程的用户和邮箱这里就不说了。

    git config --global user.name "YiwenDong"git config --global user.email "xxxx1.dong.ext@nokia.com"
  2. 生成ssh秘钥

     $ ssh-keygen -t rsa -C "xxxx.1.dong.ext@nokia.com"

    这里生成时会出现如下的信息,直接按Enter建就好,直接放入默认位置下

    Creates a new ssh key using the provided email # Generating public/private rsa key pair.Enter file in which to save the key (/home/you/.ssh/id_rsa):

    还会有一个类似下面的一句话,意思让我们输密码,输不输都行,这里我输入了密码,还会让你在输入一遍,输入时密码不可见,注意。输入完秘钥应该就生成了。

    Enter same passphrase again: [Type passphrase again]
  3. 查看秘钥是否生成

    $ cd ~/.ssh    $ ls

    如果出现如下所示的秘钥(一个公钥一个私钥),恭喜,成功。
    这里写图片描述

  4. 将公钥在gitee(或者github)中添加。

    首先查看一下公钥

    cat id_rsa.pub

    出现和下面相似的一串字符,复制它

    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCcc3aFD6+QdySTy9b5O5p26JCVF86a00h530XX9mVD3QUkSG0gdEldDMrgef5N6rrHuApx13hYBuiviJj9Vp6y+6w2b6ncGnHrdPmhCOiBVQ2k1tr0DIclDb6b44QbxNU4q8D0yF+69Pj7OajUOU3JLRs9oovHXjYVXPBi11xDBYSVB3hVh9ocdVYuevCgBVBttie/wQu4ajib6zQC9984+5GSDpWLeG5bh5H7rhAm/fzpcZv+dxDGsjbCwbymQv/eBbWhECPXNb60ZIbtaR0s1vgCF9KcjAjEaBXHlvjd/KpUWX3fJVhAD87Y9+m+pRzcawHmUwTpCo/ThejkL5h9 xxxx.1.dong.ext@nokia.com
  5. 打开你的gitee,按照图中顺序,先点击setting,找到SSH Key Setting,将Key复制到Key中,Title随便起。
    这里写图片描述

完成,这样你就可以向

Centos

。。不知如何写起。一句话和上面操作一模一个样,就是操作起来更舒服点,哈哈。

多人协作冲突问题

假如我们在gitee中有一个项目,项目名叫Demo,现在两台电脑(A B)都将该项目克隆下来了,A现在提交了一个名字叫index.html的文件,并且push到gitee中了,而B此时也修改了index文件,在push时出现如下问题:

git push origin devEnter passphrase for key '/root/.ssh/id_rsa': To git@gitee.com:YiwenDong/Demo.git ! [rejected]        dev -> dev (fetch first)error: failed to push some refs to 'git@gitee.com:YiwenDong/Demo.git'hint: Updates were rejected because the remote contains work that you dohint: not have locally. This is usually caused by another repository pushinghint: 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.

这是因为A提交的index.html与B提交的产生了冲突,此时应该先从gitee 重新pull一下,如下所示:

[root@CivenOS Demo]# git pullEnter passphrase for key '/root/.ssh/id_rsa': remote: Counting objects: 9, done.remote: Compressing objects: 100% (6/6), done.remote: Total 9 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (9/9), done.From gitee.com:YiwenDong/Demo   64236f8..35eecf5  dev        -> origin/devAuto-merging index.htmlCONFLICT (add/add): Merge conflict in index.htmlAutomatic merge failed; fix conflicts and then commit the result.

这里pull成功,但是提示有冲突,需要修改
此时我们进入index.html后会看到如下结果

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <link href="css/login-all.css" rel="stylesheet"></head><body><div id="login-container">    <div class="login-bg"></div>    <div class="login-nav"></div>    <div class="login-content"></div></div><div id="login-keeparea"></div><div id="login-footer">@2017 yiwen</div><<<<<<< HEAD<div id="test"></div>=======<div>this is test</div>>>>>>>> 7a447f3a3ff4f05f4867658bc60f994930c00beb</body></html>

如下所示的地方就是有冲突的地方,用=====隔开了,你现在可以合并它们,然后重新add,commit,push一下就可以了。

<<<<<<< HEAD<div id="test"></div>=======<div>this is test</div>>>>>>>> 7a447f3a3ff4f05f4867658bc60f994930c00beb

修改后的index.html如下所示:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <link href="css/login-all.css" rel="stylesheet"></head><body><div id="login-container">    <div class="login-bg"></div>    <div class="login-nav"></div>    <div class="login-content"></div></div><div id="login-keeparea"></div><div id="login-footer">@2017 yiwen</div><div id="test"></div><div>this is test</div></body></html>

从add到push的操作如下所示:

[root@CivenOS Demo]# git add index.html[root@CivenOS Demo]# git commit -m "fixed conflicts about index.html"[dev 2a6fe7d] fixed conflicts about index.html[root@CivenOS Demo]# git pushwarning: push.default is unset; its implicit value is changing inGit 2.0 from 'matching' to 'simple'. To squelch this messageand maintain the current behavior after the default changes, use:  git config --global push.default matchingTo squelch this message and adopt the new behavior now, use:  git config --global push.default simpleSee 'git help config' and search for 'push.default' for further information.(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode'current' instead of 'simple' if you sometimes use older versions of Git)Enter passphrase for key '/root/.ssh/id_rsa': Counting objects: 17, done.Compressing objects: 100% (11/11), done.Writing objects: 100% (12/12), 1.12 KiB | 0 bytes/s, done.Total 12 (delta 6), reused 0 (delta 0)To git@gitee.com:YiwenDong/Demo.git   7a447f3..2a6fe7d  dev -> dev

查看一下log,可以看到如下的日志

*   2a6fe7d fixed conflicts about index.html|\  | * 7a447f3 update index.html* | f46f2ea linux update index.html* |   ef193a1 merge & fix in index|\ \  | |/  

OK,这样就解决了冲突了

原创粉丝点击