【git】Git的使用

一、安装git
1、windows下安装一个Git
2、lInux下yum(apt-get) install git
二、使用git连接github
使用git连接github时,需要将linux下产生的一个ssh公钥放到github上。
1)、生成公钥
ssh-keygen -t rsa -C"mail@mail.com"
然后系统提示输入文件保存位置等信息,连续敲三次回车即可,生成的SSH key文件保存在中~/.ssh/id_rsa.pub文件中。
2)获取公钥
linux下可以用cat命令显示id_rsa.pub中的内容(cat  ~/.ssh/id_rsa.pub),然后复制其内容。Windows下用文本编辑工具打开该文件,然后复制其内容。
3)粘贴到Github上

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

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

注2:生成的ssh公钥要放在使用Git的账户下(非常不幸,我未能连接成功。可使用命令ssh -Tv git@github.com来查找failure的原因。通过详细的debug过程,我发现象我把自己的ssh密钥信息放到了/home/admin/.ssh/下,而测试时使用的账户是root,寻找ssh密钥的路径为root/.ssh,所以permission denied的啦。su到admin下,就可以连接成功啦~~)

 3. 使用git与github管理代码

3.1 新建一个repository

这里就使用github官网上的教程吧。请保证git的版本至少为1.7.10,否则可能无法成功。详细如何使用,请参见:https://help.github.com/articles/set-up-git。linux下无法新建一个repo,只能对github中已有的repo进行修改。所以,当要新建一个repo时,必须在github.com上新建,再通过linux下的git向此repo中新添内容。

3.2 修改repo中的代码

github的官网上也有修改repo代码的教程。详情请参见:https://help.github.com/articles/fork-a-repo。简要步骤如下:

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

$cd Spoon-Knife

$git add filename.py                          #添加文件到版本库

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

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

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

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

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

$git push origin                            #将修改提交到github上<br>

3.3 常用git命令
git help                                 #可查看git的常用命令
git config --global user.name "Your Name Here"           #设置commit的署名
git config --global user.email "your_email@example.com"      #设置commit的email
git config [--local|--global|--system] --list/-l          #查看本地的global信息
git config [--local|--global|--system] --unset[-all] user.name  #删除user.name信息。如果user.name对应多个值,可用unset-all来删除
git remote add XXX https://github.com/username/repo_name.git    #设置github的连接
git clone git://github.com/your_account/aimed_repo.git       #复制一个repo到本地 
git remote -v                               #查看本地设置的url连接信息 
git status                                 #查看当前工作的
branch git branch                             #查看本地所有的
branch git branch -a                           #查看远程的所有分支 
git branch -d branch_name                          #删除本地branch_name这一分支 
git push origin --delete branch_name                   #删除名为branch_name的远程分支 
git checkout branch_name                         #切换到名为branch_name的分支上 
git chechout -b branch_name                        #在本地新建一个名为branch_nam的分支 
git diff test_branch_name                         #查看当前branch与test_branch_name中代码的区别 
git mv filename newfilename                      #文件重命名 
git push XXX branch_name                        #上传指定的branch到远端 
git pull                                  #将远程上的版本与本地版本进行合并,相当于get fetch + git merge 
git reset --hard                             #将刚才进行的git pull所进行的操作取消,恢复本地版本合并前的原貌

复制代码

 4. 如何删除github上的repository

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


原文地址:https://www.cnblogs.com/haxianhe/p/9271212.html