Ubuntu16.04环境下git安装与使用

一、git的安装

    参见廖雪峰的git教程,在Linux使用sudo apt-get install git即可。

二、创建本地版本库

    在需要创建版本库进行管理的根文件夹下,输入git init命令,通过git init命令将这个文件夹变成git可以管理的仓库:

H:\>cd weekly-report
H:\weekly-report>git init
Initialized empty Git repository in H:/weekly-report/.git/

   (注:这里git init命令是在Windows系统下执行的,linux同样类似)

    可以看到:已经初始化了一个git仓库,同时在该目录下已经创建了一个.git目录,这个目录下的文件包含了所有的配置文件,注意千万别手动更改.git目录下的文件,一旦修改会引起git仓库的混乱。

   这时, 在Ubuntu系统进入U盘文件目录后,重新输入git init命令:

yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ sudo git init
重新初始化现存的 Git 仓库于 /media/vslyu-flash/weekly-report/.git/
    提示已重新初始化现存的Git仓库,使用ls -lah命令查看包括隐藏文件在内的详细信息:
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ ls -lah
总用量 104K
drwxr-xr-x  3 root root 8.0K May 16 15:48 .
drwxr-xr-x 28 root root 8.0K Jan  1  1970 ..
drwxr-xr-x  7 root root 8.0K May 16 17:25 .git
-rwxr-xr-x  1 root root  79K May  6 21:23 周报-20180506-刘宇辉.docx

    可以发现.git文件的时间信息已被改变,说明已被重新初始化。


三、同远程仓库如GitHub进行交互

    参照廖雪峰的教程,因为本地Git仓库和远程GitHub仓库之间的传输是通过SSH加密的,需要先配置SSH。

    1、创建SSH Key

    因为是第一次创建SSH Key,所以在主目录里并没有.ssh目录,验证如下:

yuhuiliu@sinc-server:/$ sudo find / -name ".ssh"
/home/xuelingzhang/.ssh
find: `/run/user/1025/gvfs': 权限不够
find: `/run/user/1000/gvfs': 权限不够
yuhuiliu@sinc-server:/$
    使用sshkeygen命令创建Key:
yuhuiliu@sinc-server:/$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yuhuiliu/.ssh/id_rsa):
Created directory '/home/yuhuiliu/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Passphrases do not match.  Try again.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/yuhuiliu/.ssh/id_rsa.
Your public key has been saved in /home/yuhuiliu/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:cqfv09Sj7vxM3JnkEGX+p2w1pg+P/d88oFXqWRdlRNk [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|               ==|
|              +.E|
|             . o.|
|              .o.|
|      . S .  oo+=|
|       o o  .*O=*|
|        .  o++X=o|
|         ..oo**o.|
|         .o++oo+O|
+----[SHA256]-----+
yuhuiliu@sinc-server:/$

    这里会有两个提示,首先提示你选择save the key的路径,默认选择即可;需要注意的是其次两次提示的ssh命令的密码(该密码是你push文件的时候要输入的密码,而不是github管理者的密码),为了方便,不设置密码也可以。

    2、登录GitHub填入client端生成的公钥

    ssh验证的过程为:server端使用rsa.pub的公钥对client发过来的rsa私钥做匹配,匹配成功就能够验证成功。因此私钥绝对不能泄露出去,否则就被别人冒充。 在这里,GitHub就是在远程的server端,只需要将上面生成的rsa公钥上传上去就可以了。

    登录GitHub,在个人账号那里找到并打开“settings”,点入“SSH and GPG keys”:


    点击右上角的New SSH Key,进入创建SSH Key:


       Title这里取个名字,作为标识,一般我都是根据client端的名字来取名以示区别的;最后,在Key那里粘贴isa.pub公钥文件的内容即可。

    可以使用文本编辑器如vi、gedit等打开isa.pub文件进行,也可以使用命令clip < ~/.ssh/id_rsa.pub进行复制:


在GitHub端粘贴对应公钥:


    点击“Add SSH Key”即可创建成功。


     3、测试一下该SSH Key

      参照https://www.cnblogs.com/ayseeing/p/3572582.html,输入ssh -T命令进行测试:

yuhuiliu@sinc-server:/$ ssh -T [email protected]
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?
    输入yes即可,如果你在创建SSH Key是还设置了密码,接下来还会提醒你输入密码;如果没有,直接“Enter”即可,
yuhuiliu@sinc-server:/$ ssh -T [email protected]
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yew
Please type 'yes' or 'no': yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
packet_write_wait: Connection to 52.74.223.119 port 22: Broken pipe
yuhuiliu@sinc-server:/$ ssh -T [email protected]
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
X11 forwarding request failed on channel 0
Hi vlsyu! You've successfully authenticated, but GitHub does not provide shell access.
yuhuiliu@sinc-server:/$

    最后,看到上面红色的欢迎字符表示你已经可以成功通过ssh连接到GitHub上。

三、同步本地库到远程库上

    应用场景:本地的仓库需要推送到远程仓库上,一是进行备份,二是方便他人的开源。

    1、在GitHub上创建一个新的仓库,建议最好要与本地同名,不然同步的话会引起麻烦。

    创建如图的远程仓库:


    

       目前在GitHub上面这个weekly-report仓库并没有内容,可以从其他的仓库克隆到这个新的仓库,也可以推送本地仓库到这个仓库。在这里,我选择推送之前在本地创建好的weekly-report仓库到远程weekly-report仓库。

    2、关联本地仓库到新的远程GitHub仓库

    首先在本地的git仓库下使用git remote add origin 命令添加对远程仓库的关联:

yuhuiliu@sinc-server:/$ cd /media/vslyu-flash/weekly-report/
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ git remote add origin git@github:vslyu/weekly-report.git
error: could not lock config file .git/config: 权限不够
fatal: Could not set 'remote.origin.url' to 'git@github:vslyu/weekly-report.git'
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ sudo git remote add origin git@github:vslyu/weekly-report.git
[sudo] yuhuiliu 的密码:
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$

    添加成功后,在本地使用远程仓库的话远程仓库的名字就叫origin,当然也可以取其他名字,但是默认都取origin这个名字,这样人一看就知道是远程库的名字。

    3、推送本地仓库到新的远程GitHub仓库上:

yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ git push -u origin mastererror: src refspec master does not match any.
error: 无法推送一些引用到 'git@github:vslyu/weekly-report.git'
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ sudo git push -u origin master
error: src refspec master does not match any.
error: 无法推送一些引用到 'git@github:vslyu/weekly-report.git'
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$
    报了一个error,参照https://www.cnblogs.com/jeremylee/p/5715289.html,应该是git init后没有往本地仓库里面添加文件,使得无法提交一个空的仓库。接下来往本地的库里添加文件:
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ sudo git add 周报-20180506-刘宇辉.docx
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$
    添加成功,继续推送:
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ sudo git push -u origin master
error: src refspec master does not match any.
error: 无法推送一些引用到 'git@github:vslyu/weekly-report.git'
    发现还是报错,使用git status查看一下本地仓库的状态:
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ git status
位于分支 master

初始提交

要提交的变更:
  (使用 "git rm --cached <文件>..." 以取消暂存)

        新文件:   "\345\221\250\346\212\245-20180506-\345\210\230\345\256\207\350\276\211.docx"

    本地仓库已经不是一个空库,再仔细检查一下报的error,发现远程仓库中的名字“git@github:vslyu/weekly-report.git”拼错了。(补充一个小技巧:远程仓库的名字一般可以直接从远程项目主页上进行copy)

    移除绑定的origin,重新绑定:

  •  修改命令(git  remote set-url --add origin URL)
  • 先移除后绑定
  • 修改config配置文件
    使用修改命令(git  remote set-url --add origin URL):
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ sudo git remote set-url --add origin [email protected]:vlsyu/weekly-report.git
[sudo] yuhuiliu 的密码:
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ sudo git push -u origin master
error: src refspec master does not match any.
error: 无法推送一些引用到 'git@github:vslyu/weekly-report.git'
error: src refspec master does not match any.
error: 无法推送一些引用到 '[email protected]:vlsyu/weekly-report.git'
    这时再仔细查看问题,发现add后没有commit,commit一下:
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ sudo git commit -m "weekly-report@Week10,20180506"

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@sinc-server.(none)')
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$
    根据提示配置一下email和name:
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ sudo git config --global user.email "[email protected]"
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ sudo git config --global user.name  "vslyu"
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$
    重新commit一下:
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ sudo git commit -m "weekly-report@Week10,20180506"
[master (根提交) b59f287] weekly-report@Week10,20180506
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 "\345\221\250\346\212\245-20180506-\345\210\230\345\256\207\350\276\211.docx"
yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$

    这下子终于提交了,再来push一下:

yuhuiliu@sinc-server:/media/vslyu-flash/weekly-report$ sudo git push -u origin master
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
    

    



    

猜你喜欢

转载自blog.csdn.net/vslyu/article/details/80337675