分享

GitHub的使用方法

 迷途中小小书童 2018-09-30

GitHub简介

github是目前世界上最大的代码存放网站和开源社区。我们可以利用github创建自己的代码仓库,同时访问追踪其他人的代码仓库,还可以多人协同完成工作项目。

使用方法总结

github有很多作用,对于常见的作用:

  • 存放代码,更新仓库;
## 上传本地仓库
git add filename
git commit -m 'update'
git push
#**************
## 若远程仓库有改动,需要先git pull, git add . 点表示加入所有变化,也可以指定具体的filename, 可以多次git add,一次git commit
git pull
git add .
git commit -m 'update'
git push

具体使用教程

1. 下载安装git,R,Rstudio,并注册github账号

这些准备工作很简单,这里不做介绍。

2. 创建github资源库(repository)

打开https://github.com/new
输入资源库的名字(库的名字与本地的dir_name可以不同),添加描述(可选择),设置是公开还是私秘的(私密的需要付费),勾选创建readme文件(这里最好勾选,如果是空目录的话,本地同步github会出现错误),创建。

图1 创建github资源库

3.通过Rstudio关联本地文件和github

  • Rstudio的配置
    首先打开File,创建New Project, 如果路径下没有创建就选择New diresctory,如果已经有目标路径,就选择Existing Directory;

    image.png

然后打开Tools,Globe Options,Git/SVN, 选择git安装路径,同时点击View public key, 复制;
然后打开github settings的 keys, 创建New SSH key, 将复制的key粘贴进去

图3

4.本地文件夹与github关联

打开Tools的shell窗口,首先设置用户名和邮箱

git config --global user.email "renyunxiao16@big.ac.cn" ##设置邮箱
git config --global user.name "yunxiao" ##设置用户名

添加本地文件或文件夹

# 初始化
git init
# 添加远程 Git 仓库,空白仓库需要origin, 后面是仓库的http或git地址
 git remote add origin https://github.com/lassefolkersen/ren-folkersen-deconvolution.git
# 提交
git commit -m 'first commit'
git push origin master

若是已知仓库,添加更新文件

## 如果远程仓库有变化,先将远程文件拉下来,用git pull 
git pull
git add .
## git add specific_file, .是所有文件,多次添加的文件可以一次提交
git commit -a -m "update"
## update 注释变动的原因和内容
git push origin master
## git push 将文件提交远端仓库

常见错误及解决办法

git push origin master
To https://github.com/lassefolkersen/ren-folkersen-deconvolution.git
! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/lassefolkersen/ren-folkersen-deconvolution.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

git pull origin master
git push -u origin master -f

其他常见操作

删除仓库或文件夹

删除仓库从settings下删除,删除文件从命令行即可
git remote rm test

恢复历史版本

#查询显示
git reflog
10e2ff3 (HEAD -> master, origin/master) HEAD@{0}: reset: moving to 10e2
10e2ff3 (HEAD -> master, origin/master) HEAD@{1}: commit (initial): add folder
#恢复
$ git reset --hard 10e2
HEAD is now at 10e2ff3 add folder

git 学习资源

廖雪峰git教程

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约