GitHub和Git的常用操作

    对于代码的管理我们一般使用GitHub+Git或SVN+Redmine,下面主要接介绍一下GitHub和Git的常用操作:

1、GitHub 创建开源项目
  先登录其官网注册一个免费使用的账号,依次填写用户名, 有效邮箱地址以及登录密码, 点击下方的“Create an account”就会自动创建一个用户,进入选择服务的页面,这里我们选择免费使用的条目。选择了免费服务以后就要公开整个项目源码, 如果不想公开源码又要使用 Git 托管服务, 就需要支付一定的费用给 GitHub。

2、验证邮箱地址
  登陆邮箱利用 GitHub 给的链接激活用户之前注册的账号。 在点击激活链接以后,会弹出 GitHub 设置邮箱的页面,这时候不需要额外设置,直接点击右上角的加号(+)按钮,然后选择“New Repository”。

  然后在新建 repository 的页面中,依次填入 repository 名称以及对项目的描述和选择项目是否公开,这里要选择公开,还可以选择项目遵循的许可协议,我们选择的是 GPLv3(可以不选),最后点击“Create repository”完成开源项目的创建.
  创建完毕以后,里面只有许可协议文件和 README 文件的空项目(空版本库),还只是一些初始化的文件,需要用户从本地版本库同步到该远程的 GitHub 版本库。
3、上传代码到 GitHub
gaohui@ubuntu:~$ git config --global user.name "cnhui"
gaohui@ubuntu:~$ git config --global user.email "[email protected]"
然后使用如下命令生成 SSH Key:
gaohui@ubuntu:~/easypr$ ssh-keygen -t rsa -C "[email protected]"

  一开始让你确认是否将 SSH Key 文件存放到~/.ssh 目录,如果已经存在该目录,且该目录已经有了 SSH Key, 会询问用户是否覆盖。 接下来让你输入密码, 注意输入密码的时候没有回显, 接着是再次输入密码, 两次密码都是注册 GitHub 时候的登陆密码, 如果一切正常, 然后在~/.ssh 目录会生成 id_rsa 和 id_rsa.pub 两个文件, 它们是 SSH Key 的秘钥对。 id_rsa是私钥,不能泄露出去; id_rsa.pub 是公钥,可以放心地告诉任何人!
  用文本编辑器打开 id_rsa.pub 文件, 将里面的内容全部复制(注意不要修改任何内容, 包括空格和空行和制表符, 什么都不要改), 这里笔者使用 gedit 打开,使用 ctr+ A 即可全选, 使用 ctr +C 即可复制。找到设置 SSH Key 的入口,即在右上角点击下拉箭头

gaohui@ubuntu:~/easypr$ gedit ~/.ssh/id_rsa.pub
  然后点击 seetings,在新页面左侧选择 SSH Keys,在右侧点击“New SSH key”,在下方输入 Title 以及 Key,Title 随便填写,Key 选项的输入框就粘贴刚刚从 id_rsa.pub 复制的内容。如图 4-9 所示,然后单击“Add SSH key”按钮即可添加一个 public key。
设置完 SSH key 以后, 可用如下命令检测公钥、 秘钥以及上述步骤的设置是否正确:
如果提示用户如下正确信息:
gaohui@ubuntu:~/easypr$ ssh -T [email protected]
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
Hi cnhui! You've successfully authenticated, but GitHub does not provide shell access.

使用 git remote 命令设置项目在 GitHub 的 URL:
gaohui@ubuntu:~/easypr$ git remote add origin [email protected]:cnhui/EasyPR-Chinese-license-plate-recognition-system.git
fatal: Not a git repository (or any of the parent directories): .git

gaohui@ubuntu:~/easypr$ git init
Initialized empty Git repository in /home/gaohui/easypr/.git/
gaohui@ubuntu:~/easypr$ git remote add origin [email protected]:cnhui/EasyPR-Chinese-license-plate-recognition-system.git
其中 origin 表示的是远程代码库的仓库名字, 可以自己定义,同时这个命令将本地代码库和 GitHub 远程代码库关联起来。还需要使用如下命令使得远程代码库的 README 等文件和本地代码库一致,否则不能执行 git push。

gaohui@ubuntu:~/easypr$ git pull --rebase origin master
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
remote: Counting objects: 12, done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 12 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (12/12), done.
From github.com:cnhui/EasyPR-Chinese-license-plate-recognition-system
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master

gaohui@ubuntu:~/easypr$ git push -u origin master
Branch master set up to track remote branch master from origin.
Everything up-to-date

gaohui@ubuntu:~/easypr$ git add ARM\ Linux下EasyPR中文车牌识别系统开发.doc 
gaohui@ubuntu:~/easypr$ git commit -m 'cnhui'
[master f55a27c] cnhui
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100755 "ARM Linux\344\270\213EasyPR\344\270\255\346\226\207\350\275\246\347\211\214\350\257\206\345\210\253\347\263\273\347\273\237\345\274\200\345\217\221.doc"

使用下述命令将本地版本库主分支(master) 中的文件上传到 GitHub,其中-u 选项是第一次 push 的时候使用,将本地和远程的 master 分支关联起来,并同步文件。
其中 master 表示的是远程版本库(origin)的主分支,如果上传成功会有如下提示信息:
gaohui@ubuntu:~/easypr$ git push -u origin master
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects:  66% (2/3)   
Writing objects: 100% (3/3), 1.25 MiB | 1.71 MiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:cnhui/EasyPR-Chinese-license-plate-recognition-system.git
   c130c31..f55a27c  master -> master
Branch master set up to track remote branch master from origin.
4、补充
如果从 GitHub 下载源代码: 
git clone [email protected]:cnhui/EasyPR-Chinese-license-plate-recognition-system.git
如果只是想获取某一个分支的最新内容,只需使用如下命令,表示或其远程版本库 origin 的 master 分支的最新内容。 
git pull origin master
 
如果git在本地创建了一个仓库并添加了文件,现在这个本地仓库不想要了。可以先清除本地文件夹下的git文件,不会删除仓库里面的文件,然后在重新初始化新建的git仓库。
find . -name ".git" | xargs rm -Rf
接着add,commit等操作即可

删除github上文件
sudo git rm read.txt
git push origin master
 
恢复误删出的git上文件
git log
sudo git reset --hard "4acb59aa4f8474a2afcdde237827796b4c19f6f9"
 
恢复github上的误删的文件:
sudo git add *
sudo git commit -m "linux pcie driver run"
git push origin master
git push -f origin master
(git push origin master --force 》 ! [rejected] master -> master (non-fast-forward))
  Git还有比较常用的用法和技巧:分支管理和搭建Git服务器,推荐廖雪峰老师的博客:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000


猜你喜欢

转载自blog.csdn.net/u010872301/article/details/80530494