美文网首页
Github及Git Linux 教程

Github及Git Linux 教程

作者: YanyZhao | 来源:发表于2019-01-22 16:35 被阅读0次

Git:即Linux 内置的GitHub

  • 本文包括三个内容:
    -第一部分:GIthub
    -第二部分:GIt
    -第三部分:上传本地项目到github
    -第四部分:上传GitHub官方教程
    -第五部分:GitHub账户文件删除

1、网页:GIthub

1.1 创建库(create a new repository)

1.2创建分支(Branch)

  • 一次处理不同版本存储库的方法
  • master:主分支
  • feature:次分支(主分支的副本)——在master 分支的副本上做修改后的。

1.3 打开 Pull Request

  • Pull Request : 用于合并项目(社会化编程)——自己修改代码、上传,请求对方的仓库采纳。
  • 更改 : 在Master分支基础上,进行的修改的feature分支
    -增加 : 显示绿色
    -减少 : 显示红色
  • 支持Markdown语法

1.4 合并(merge) Pull Request

  • 将次分支"featrue" 合并到“master”主分支里

1.5 GitHub步骤:

    1.创建一个开源库
    2.创建并管理新分支
    3.修改文件并提交修改到GitHub
    4.打开并合并一个Pull Request

2、Linux PC端 — Git

2.1 查看Linux Git版本:

git --version

2.2 设置用户名和邮箱

git config --global user.name '用户名'     #将*用户名*设为author
git config --global user.email '邮箱地址'   #将用户邮箱设为
#--global :全局配置——作用于本机的每个Git项目

拓展:

查看git配置:
git config --list
#Git全局配置文件存放在"~/.gitconfig"(用户目录下的.gitconfig)文件
cat ~/.gitconfig   #查看当前用户名或邮箱
[user]
name = author
email = author@corpmail.com
#项目配置:针对不同的项目,设定不同的配置信息——用户名、邮箱
#进入Git项目所在目录:
$ git config user.name '用户名' #将用户名设为
$ git config user.email '邮箱地址' #将用户邮箱设为
#项目配置文件是存放在Git项目所在目录的".git/config"文件中
cat .git/config
[user]
name = nickname
email = nickname@gmail.com

2.3 为Github账户设置SSH key

  • 1、检查SSH公钥
cd ~/.ssh
  • 2、生成SSH公钥
ssh-keygen -t rsa -C "your_email@youremail.com" 

Generating public/private rsa key pair.
Enter file in which to save the key (/home/you/.ssh/id_rsa):
#默认回车:
Enter passphrase (empty for no passphrase):
Enter same passphrase again: 

可以输入你在github上设置的密码,但每次git操作都必须输入密码,如果直接回车就不用了,生成公私钥成功:

Your identification has been saved in /home/you/.ssh/id_rsa. 
Your public key has been saved in /home/you/.ssh/id_rsa.pub.
The key fingerprint is: 
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@youremail.com
  • 3、 添加SSH公钥到github
    打开github,找到账户里面添加SSH,把id_rsa.pub文件内容复制到key里面。
  • 4、 测试是否生效
ssh -T git@github.com
Hi YanyZhao! You've successfully authenticated, but GitHub does not provide shell access.
#看到这个内容放入时候,说明就成功了。

3、 上传本地项目到github

mkdir demo
cd demo
echo "# demo" >> README.md
git init    #把这个目录变成Git可以管理的仓库
git add README.md #  文件添加到仓库
git add .  
#不但可以跟单一文件,还可以跟通配符,更可以跟目录。一个点就把当前目录下所有未追踪的文件全部add了(空目录不会被添加)

git status   #查看当前工作区的状态(需提交的变更)
git commit -m "first commit"    #把文件提交到仓库
git remote add origin git@github.com:hxf0663/demo.git #关联远程仓库
git push -u origin master   #将本地主分支推到远程(如无远程主分支则创建,用于初始化远程仓库)
git push origin master  #将本地主分支推到远程主分支
下载操作:
git pull origin master  #把远程库更改拉到本地仓库
git clone git@github.com:hxf0663/demo.git   #克隆远程仓库到本地
git clone https://github.com/hxf0663/demo.git  #克隆远程仓库到本地
git clone https://github.com/hxf0663/demo  #克隆远程仓库到本地

4、GitHub官方上传教程:

Get started by creating a new file or uploading an existing file. We recommend every repository include a README, LICENSE, and .gitignore.

…or create a new repository on the command line

<pre id="empty-setup-new-repo-echo" class="f5">echo "# Pour_youbot_OptiTrack" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/YanyZhao/Pour_youbot_OptiTrack.git
git push -u origin master
</pre>

…or push an existing repository from the command line

<pre id="empty-setup-push-repo-echo" class="f5">
git remote add origin https://github.com/YanyZhao/Pour_youbot_OptiTrack.git
git push -u origin master</pre>

…or import code from another repository

You can initialize this repository with code from a Subversion, Mercurial, or TFS project.

5、GitHub文件删除

注:
git push -u origin master可能会出现一些异常

错误描述
解决办法如下
git push -u origin +master
1

删除远程仓库不想要的文件

在上传项目后如果上传了不想要的文件,在github上是不能直接删除仓库的文件,我们只能通过终端命令来删除我们我想要的文件或者目录。

git rm --cached -r useless    ## -r参数删除目录
git commit -m "remove directory from remote repository"
git push

删除文件

git rm --cached filename    ##git rm --cached a.txt删除的是本地仓库中的文件
git commit -m "delete filename"
git push 

注:用-r参数删除目录,git rm --cached a.txt删除的是本地仓库中的文件,且本地工作区的文件会保留且不再与远程仓库发生追踪关系,如果本地仓库中的文件也要删除则用git rm a.txt

[第2、3节摘自CSDN:疯跑蜗牛] (https://blog.csdn.net/hxf0663/article/details/79527453 )

[第5节摘自CSDN:Tyella] (https://blog.csdn.net/ty13572053785/article/details/82709443)
转载请声明

相关文章

网友评论

      本文标题:Github及Git Linux 教程

      本文链接:https://www.haomeiwen.com/subject/smaljqtx.html