Ubuntu系统如何安装和配置Git

一、Git安装:

  1、 二进制方式安装:

  $ sudo apt-get install git-core

  安装完成后,在终端中输入 git 就可以看到相关的命令了。如果只是需要使用git来管理本地的代码,那么现在 就 可 以使用了。如果需要和github上的项目结合,还需要做其他的一些操作。

  2、github帐号的申请

  如果只是需要将github上感兴趣的代码拷贝到本地,自己进行修改使用,而不打算共享发布的话,其实不申请 帐号也没有关系,只需要 git clone 代码到本地就可以了。 $ git clone git:// IP work(工作目录名)。

我下载的是https://github.com/narendraprasath/Word-Segmentation-in-NLP-Python,只要这个命令就可以:git clone git://github.com/narendraprasath/Word-Segmentation-in-NLP-Python

  毕竟使用 github 就是为了开源的目的,首先去 github.com 上注册一个帐号。

  3、在本地建立一个文件夹,然后做一些全局变量的初始化

  $ git config --global user.name = “用户名或者用户ID”

  $ git config --global user.email = “邮箱“

  这两个选项会在以后的使用过程中自动添加到代码中。

  4、创建验证用的公钥

  这个是比较复杂和困扰大多数人的地方,因为 git 是通过 ssh 的方式访问资源库的,所以需要在本地创建验证 用的文件。使用命令:$ ssh-keygen -C ”you email address@gmail.com“ -t rsa会在用户目录 ~/.ssh/ 下建立相应 的密钥文件。可以使用 $ ssh -v git@github.com 命令来测试链接是否畅通。

  5、上传公钥

  在 github.com 的界面中 选择右上角的 Account Settings,然后选择 SSH Public Keys ,选择新加。

  Title 可以随便命名,Key 的内容拷贝自 ~/.ssh/id_rsa.pub 中的内容,完成后,可以再使用 ssh -v git@github.com 进行测试。

我的连不上github,出现下面错误:

OpenSSH_7.2p2 Ubuntu-4ubuntu1, OpenSSL 1.0.2g-fips 1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to github.com [192.30.252.131] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu1
debug1: Remote protocol version 2.0, remote software version libssh-0.7.0
debug1: no match: libssh-0.7.0
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:5
Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: 3422002301056739724@qq.com342200231
debug1: Server accepts key: pkalg ssh-rsa blen 279
sign_and_send_pubkey: signing failed: agent refused operation
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).

然后我在知乎上找到问题:

如果你曾经出现过这种问题,我有一个解决方案
首先,清除所有的key-pair
ssh-add -D
rm -r ~/.ssh
删除你在github中的public-key

重新生成ssh密钥对
ssh-keygen -t rsa -C "xxx@xxx.com"
chmod 0700 ~/.ssh
chmod 0600 ~/.ssh/id_rsa*

接下来正常操作
在github上添加公钥public-key:
1、首先在你的终端运行 xclip -sel c ~/.ssh/id_rsa.pub将公钥内容复制到剪切板
2、在github上添加公钥时,直接复制即可
3、保存

测试:
在终端 ssh -T git@github.com
成功,出现下面信息

Hi 1056739724! You've successfully authenticated, but GitHub does not provide shell access.

第二次,又出现问题:

Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
sign_and_send_pubkey: signing failed: agent refused operation
Permission denied (publickey).

这时是用这个答案解决的

https://help.github.com/articles/error-agent-admitted-failure-to-sign/

  二、Git配置与使用

  利用 github 来管理自己的项目,可以按照下面的步骤进行

 1建立远端仓库

    在 Github 创建账号后,点击 New Repository,填写一些本仓库相关的信息,如仓库名称、描述、是否公开。设置完毕后,点击 Create repository 即可创建新仓库。如,建立olsr。

  2、建立本地仓库

  在需要建立项目的文件夹中,使用 git init 进行仓库的建立。完成后,可以看到文件家中多了一个 .git 隐藏目 录。

  2、添加文件

  使用 git add 。 来进行初始文件的添加。这里 。 表示将文件夹下所有的文件都添加进去,我们也可以指定文件进 行添 加。

     如果输入$ Git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git 

    提示出错信息:fatal: remote origin already exists.

    解决办法如下:

    1、先输入$ git remote rm origin

    2、再输入$ git remote add origin git@github.com:djqiang/gitdemo.git 就不会报错了!

  3、提交文件

  使用 git commit -m ‘comment’ 提交,可以将编辑的内容进行提交。

  4、删除或增加github远程来源

  git remote add origin https://github.com/Git-Elite/CodeBase.git //蓝色部分为github托管的仓库地址

  5、提交至github仓库

  git push -u origin master

//报错了:
提示:更新被拒绝,因为远程版本库包含您本地尚不存在的提交。这通常是因为另外
提示:一个版本库已向该引用进行了推送。再次推送前,您可能需要先整合远程变更
提示:(如 'git pull ...')。
提示:详见 'git push --help' 中的 'Note about fast-forwards' 小节。

//强制推送
git push origin +master

  上面就是Ubuntu安装和配置Git的方法介绍了,在使用Git前,需要先建立一个github仓库,之后就能使用git管理Linux项目了。

原文地址:https://www.cnblogs.com/honeybusybee/p/5534894.html