Ubuntu 18.04 Git配置及连接Github

配置Git

1.安装:使用命令
sudo apt install git

2.本地生成ssh key:使用命令
ssh-keygen -t rsa -C "[email protected]"
默认生成的key在/home/.ssh/id_rsa.pub(可由vim打开复制)

3.在github中添加SSH key:登录github-Account Settings-SSH Keys-Add SSH Key,title随意,粘贴SSH key

4.测试ssh key是否成功:使用命令
ssh -T [email protected]

  • 若出现You’ve successfully authenticated, but GitHub does not provide shell access。表示已经成功连接github(warning忽略)。
  • 若出现Agent admitted failure to sign using the key.Permission denied (publickey)。使用命令
    ssh -add

5.配置Git配置文件:username和email——本地用户名和联系email(随意,github每次commit都会记录)

git config --global user.name "your name"
git config --global user.email "your email"
  • 1
  • 2

其中,--global参数代表这台机器上所有的git仓库都会用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。

6.在github上添加一个repository

7.利用git上传本地库
在个人目录下创建要上传的本地库——文件夹,cd到此,使用命令
git init初始化本地库。
添加远程仓库(连接),使用命令

git remote add origin [email protected]:yourName/yourRepo.git
  • 1

yourName是github的用户名,yourRepo是建立的repository.git。
在本地库中,.git/config,会有一个remote “origin”内容,可以直接修改来配置远程地址。

  • 工作流
    一共由三个阶段:工作目录——暂存区(Index,保存临时改动)——HEAD(最后一次提交的结果)
    ->git add <filename>或者git add *自动判断添加哪些文件,添加文件至暂存区。可以多次反复使用,添加多个文件。
    ->git commit -m "代码提交信息(更新日志等)"提交文件至HEAD。
    ->git push origin master提交文件至远端仓库master分支(可改)。
    (图片转自知乎)

8.克隆远端仓库及wiki

git clone [email protected]:myname/respository.git
git clone [email protected]:myname/respository.wiki.git
  • 1
  • 2

9.添加其他远程仓库:使用命令
git remote add <shortname> <url>
shortname指定一个轻松使用的简写
url可在git的仓库右边复制”HTTPS clone url”
克隆此仓库使用命令
git clone <shortname>or<url>

分支

分支是用来将特性开发绝缘开来的。在创建仓库时,master是“默认的”分支。在其他分支上进行开发,完成后再将它们合并到主分支上。

创建一个分支:
git checkout -b feature-x
切换回主分支:
git checkout master
删除分支:
git branch -d feature-x
除非你将分支推送到远端仓库,否则就是不为他人所见的:
git push origin <branch>

更新与合并

要更新你的本地仓库至最新改动,执行:
git pull
合并其他分支到当前分支,执行:
git merge <branch>
在合并之前,可以使用如下命令预览差异:
git diff <source_branch> <target_branch>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章