天天看点

linux下git的安装和使用(转)

在linux下我仅使用了文本界面,所以安装了个文本形式的git客户来连接github。

1. 安装git

2. 使用git连接github

1

<code>ssh-keygen -t rsa -C</code><code>"[email protected]"</code>

然后系统提示输入文件保存位置等信息,连续敲三次回车即可,生成的SSH key文件保存在中~/.ssh/id_rsa.pub文件中。

用文本编辑工具打开该文件,在linux下可以用cat命令显示id_rsa.pub中的内容(cat  ~/.ssh/id_rsa.pub),让后复制其内容。

接着拷贝.ssh/id_rsa.pub文件内的所以内容,将它粘帖到github帐号管理中的添加SSH key界面中。

注意,使用vim读取git_home/.ssh/id_rsa.pub中的公钥内容时,可能会有较多的空格和换行,复制到github网站上时必需删除。所以建议使用cat来读取ssh公钥。将ssh公钥成功加入github后,可使用命令ssh -T [email protected]来验证是否成功。如果出现象:hi xxx. You've successfully authenticated, but GitHub does not provide shell access.则说明连接成功。

如:Warning: Permanently added the RSA host key for IP address '*.*.*.*' to the list of known hosts.

Enter passphrase for key '/home/git/.ssh/id_rsa': 

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

如果未能连接成功。可使用命令ssh -Tv [email protected]来查找failure的原因。通过详细的debug过程,我发现象我把自己的ssh密钥信息放到了/home/admin/.ssh/下,而测试时使用的账户是root,寻找ssh密钥的路径为root/.ssh,所以permission denied的啦。su到admin下,就可以连接成功啦~~

 3. 使用git与github管理代码

3.1 新建一个repository

3.2 修改repo中的代码

<code>$git clone https:</code><code>/</code><code>/</code><code>github.com</code><code>/</code><code>username</code><code>/</code><code>Spoon</code><code>-</code><code>Knife.git</code>

<code>$cd Spoon</code><code>-</code><code>Knife</code>

<code>$git add filename.py                          </code><code>#添加文件到版本库</code>

<code>$git commit</code><code>-</code><code>m</code><code>'add filename.py to src'</code> <code>              </code><code>#提交,产生版本记录,注意代码依然在本地</code>

<code>$vim README.md                             </code><code>#修改Spoon-Knife中的README.md文件内容</code>

<code>$git commit</code><code>-</code><code>m</code><code>'modify the README.md'</code> <code>                </code><code>#提交,产生版本记录,注意代码依然在本地</code>

<code>$git [remote] rm filename1.py                    </code><code>#删除repo中的filename1.py文件</code>

<code>$git commit</code><code>-</code><code>m</code><code>'delete filename1.py'</code> <code>                 </code><code>#提交,产生版本记录,注意代码依然在本地</code>

<code>$git push origin                            </code><code>#将修改提交到github上&lt;br&gt;</code>

 3.3 常用git命令

linux下git的安装和使用(转)
linux下git的安装和使用(转)
linux下git的安装和使用(转)
linux下git的安装和使用(转)

 4. 如何删除github上的repository

github页面上删除repo的功能比较隐蔽,得在这里表一表。比如,想删除了一个名为python的repo。则需先点击进入“python”,单击“Settings”,找到“Delete this repository”,确认删除即可。注意,github上的repo删除后就不能恢复了哦~~

linux下git的安装和使用(转)
linux下git的安装和使用(转)

5. git clone/push时出现错误提示:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed while accessing ...

这是由于ssl认证出问题引起的错误。有两种简单的解决方法:

1. 使用命令,成功执行后,便可正常使用git clone和git push了

<code>git config --global http.sslVerify</code><code>false</code>

 2. 使用命令,但每次clone 和 push时都需要带上env的部分。

<code>env GIT_SSL_NO_VERIFY=</code><code>true</code> <code>git clone https:</code><code>//github.com/XXXX/xxxxx.git</code>

当要push代码到git时,出现提示:

error:failed to push some refs to ...

Dealing with “non-fast-forward” errors

From time to time you may encounter this error while pushing:

$ git push origin master  

To ../remote/  

 ! [rejected]        master -&gt; master (non-fast forward)  

error: failed to push some refs to '../remote/'  

To prevent you from losing history, non-fast-forward updates were rejected

Merge the remote changes before pushing again.  See the 'non-fast forward'

section of 'git push --help' for details.

This error can be a bit overwhelming at first, do not fear. Simply put, git cannot make the change on the remote without losing commits, so it refuses the push. Usually this is caused by another user pushing to the same branch. You can remedy this by fetching and merging the remote branch, or using pull to perform both at once.

In other cases this error is a result of destructive changes made locally by using commands like git commit --amend or git rebase. While you can override the remote by adding --force to the push command, you should only do so if you are absolutely certain this is what you want to do. Force-pushes can cause issues for other users that have fetched the remote branch, and is considered bad practice. When in doubt, don’t force-push.

linux下git的安装和使用(转)

问题(Non-fast-forward)的出现原因在于:git仓库中已经有一部分代码,所以它不允许你直接把你的代码覆盖上去。于是你有2个选择方式:

1,强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容

git push -f

2,先把git的东西fetch到你本地然后merge后再push

$ git fetch

$ git merge

这2句命令等价于

$ git pull  

可是,这时候又出现了如下的问题:

linux下git的安装和使用(转)

上面出现的 [branch "master"]是需要明确(.git/config)如下的内容

[branch "master"]

    remote = origin

    merge = refs/heads/master

这等于告诉git2件事:

1,当你处于master branch, 默认的remote就是origin。

2,当你在master branch上使用git pull时,没有指定remote和branch,那么git就会采用默认的remote(也就是origin)来merge在master branch上所有的改变

如果不想或者不会编辑config文件的话,可以在bush上输入如下命令行:

$ git config branch.master.remote origin  

$ git config branch.master.merge refs/heads/master  

之后再重新git pull下。最后git push你的代码吧。it works now~

本文转自茄子_2008博客园博客,原文链接:http://www.cnblogs.com/xd502djj/p/4448491.html,如需转载请自行联系原作者。

继续阅读