logo头像

技术引领生活!

github傻瓜的食用方法

本文于1715天之前发表,文中内容可能已经过时。

github 作为全球最大的同性交友网站,是作为程序员的我们必备网站之一,本文就简单的使用做个介绍

git

配置 Git

  1. 首先在本地创建 ssh key;
1
$ ssh-keygen -t rsa -C "your_email@youremail.com"

后面的 your_email@youremail.com 改为你在 github 上注册的邮箱,之后会要求确认路径和输入密码,我们这使用默认的一路回车就行.成功的话会在~/下生成.ssh 文件夹,进去,打开 id_rsa.pub,复制里面的 key.

  1. 回到 github 上,进入 Account Settings(账户配置),左边选择 SSH Keys,Add SSH Key,title 随便填,粘贴在你电脑上生成的 key.

  2. 为了验证是否成功,在 git bash 下输入:

1
$ ssh -T git@github.com

如果是第一次的会提示是否 continue,输入 yes 就会看到:You’ve successfully authenticated, but GitHub does not provide shell access .这就表示已成功连上 github.

  1. 接下来我们要做的就是把本地仓库传到 github 上去,在此之前还需要设置 username 和 email,因为 github 每次 commit 都会记录他们.
1
2
$ git config --global user.name "your name"
$ git config --global user.email "your_email@youremail.com"

新建项目

1
2
3
4
5
6
7
echo "# 某某项目" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/spygg/goweb.git
git push -u origin main

将一个已有的工程上传到 github 的方法

  1. 克隆法(较为简单)注意建立的时候不要添加license 和readme
  • 到 github 上去新建一个当前工程命名的仓库
  • 执行 git clone 新建的仓库地址
1
git clone git@github.com:spygg/xxxx.git
  1. 常规方法

    1
    2
    3
    git remote add origin https://github.com/用户名/项目名
    git branch -M main
    git push -u origin main
  2. 强制推送

    1
    git push -f origin main

黑科技

加上拷贝深度后只保留最新的

1
git clone --depth=1 git@github.com:spygg/xxxx.git
  • 将工程下的所有文件拷贝到上一步 xxx 目录中
  • 执行命令三部曲
1
2
3
git add .
git comment -m "说明xxxxxxxxxx"
git push

强制覆盖本地版本

1
2
git  fetch --all
git reset --hard origin/master

ps

  • 更新仓库还是命令三部曲
  • 如果系统是 windows 请自行安装 git(我是 ubuntu 自带的,自豪一下(^__^)

高级用法

新增分支

  1. 查看所有分支
1
git branch -a
  1. 创建本地分支
1
git branch [branch name]
  1. 切换分支
1
git checkout [branch name]
  1. 更新到指定分支
1
git push origin [branch name]

克隆分支到本地

1
git clone -b [branch name] git@github.com:spygg/xxxx.git

文本回车换行

1
2
git config --global core.autocrlf false
git config --global core.safecrlf true

AutoCRLF(自动换行)

  • 提交时转换为 LF,检出时转换为 CRLF
    git config –global core.autocrlf true

  • 提交时转换为 LF,检出时不转换
    git config –global core.autocrlf input

  • 提交检出均不转换
    git config –global core.autocrlf false

SafeCRLF(安全换行)

  • 拒绝提交包含混合换行符的文件
    git config –global core.safecrlf true

  • 允许提交包含混合换行符的文件
    git config –global core.safecrlf false

  • 提交包含混合换行符的文件时给出警告
    git config –global core.safecrlf warn

支付宝打赏 微信打赏

您的支持是我前行的动力!