Ubuntu下使用Git和GitHub

时间:2023-03-08 21:52:47
Ubuntu下使用Git和GitHub

刚刚学习git和github,网上的知识太杂太乱。照着折腾了很长的时间,都没有搞出个结果,心里十分痒痒,最后终于在github上看到成果。本文适合刚刚接触github但是急于想看到效果的同学,当然git作为一种技术,并不能祈求短暂的时间内达到学会精通,后面还需继续努力

本文地址:http://www.cnblogs.com/wuyudong/p/5614718.html,转载请注明源地址。

1、安装git

sudo apt-get install git

2、注册账户以及创建仓库

要想使用github第一步当然是注册github账号了, github官网地址:https://github.com/。 之后就可以创建仓库了(免费用户只能建公共仓库),Create a New Repository,填好名称后Create,之后会出现一些仓库的配置信息,这也是一个git的简单教程。

我的github地址:https://github.com/yudongwu

3、创建本地SSH密钥

首先在本地创建ssh key;进入本地库文件夹,我的是~/mygithub/ProjectEuler

$ ssh-keygen -t rsa -C "your_email@youremail.com"

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

注意:Ubuntu下的.ssh文件夹隐藏,可以Ctrl+H使其显示

4.GitHub中设置公钥

回到github上,进入Settings(账户配置)

Ubuntu下使用Git和GitHub

选择左侧的SSH and GPG keys

Ubuntu下使用Git和GitHub

单击“New SSH Key”

Ubuntu下使用Git和GitHub

title任意填,将刚才复制的key粘贴在key这一栏,ok

5、验证SSH

为了验证是否成功,在git bash下输入:$ ssh -T git@github.com

如果是第一次的会提示是否continue,输入yes就会看到:You've successfully authenticated, but GitHub does not provide shell access 。

这就表示已成功连上github。

6、同步本地库到github

接下来我们要做的就是把本地仓库传到github上去,

在此之前还需要设置username和email,因为github每次commit都会记录他们。

wu@ubuntu:~/mygithub/ProjectEuler$ git config --global user.name "your name"

wu@ubuntu:~/mygithub/ProjectEuler$ git config --global user.email "your_email@youremail.com"

将文件添加到库

wu@ubuntu:~/mygithub/ProjectEuler$ git add .

添加文件说明
wu@ubuntu:~/mygithub/ProjectEuler$ git commit -m "add new files"

wu@ubuntu:~/mygithub/ProjectEuler$ git remote add origin git@github.com:yudongwu/ProjectEuler.git

提示错误:

fatal: remote origin already exists.

输入以下命令:

wu@ubuntu:~/mygithub/ProjectEuler$ git remote rm origin

wu@ubuntu:~/mygithub/ProjectEuler$ git remote add origin git@github.com:yudongwu/ProjectEuler.git

wu@ubuntu:~/mygithub/ProjectEuler$ git push origin master

搞定!

7.其他常用的Git命令

git init # 初始化本地Git版本库
git add # 暂存文件,如果使用.表示当前目录及其子目录
git commit -m “first commit” # 提交,-m选项后跟内容为提交所用的注释
git remote -v # 查看当前项目远程连接的是哪个版本库地址
git push origin master # 将本地项目提交到远程版本库

git fetch origin # 取得远程更新(到origin/master),但还没有合并
git merge origin/master # 把更新的内容(origin/master)合并到本地分支(master)
git pull origin master # 相当于fetch和merge的合并,但分步操作更保险