第一步: 安装Git,使用命令 “sudo apt-get install git”
第二步: 创建GitHub帐号
第三步: 生成ssh key,使用命令 “ssh-keygen -t rsa -C "your_email@youremail.com"”,your_email是你的email
第四步: 回到github,进入Account Settings,左边选择SSH Keys,Add SSH Key,title随便填,粘贴key。
第五步: 测试ssh key是否成功,使用命令“ssh -T git@github.com”,如果出现You’ve successfully authenticated, but GitHub does not provide shell access 。这就表示已成功连上github。
第六步: 配置Git的配置文件,username和email
git config --global user.name "your name" //配置用户名
git config --global user.email "your email" //配置email
# 在当前目录新建一个 Git 代码库
git init
# 下载一个项目和它的整个代码历史
# url 格式:https://github.com/[userName]/reposName
git clone [url]
添加删除文件
# 添加指定文件到暂存区
git add [file1] [file2]
# 删除工作区文件,并且将这次删除放入暂存区
git rm [file1] [file2]
# 改名文件,并且将这个改名放入暂存区
git mv [file-origin] [file-renamed]
代码提交
# 提交暂存区到仓库
git commit –m [message]
# 直接从工作区提交到仓库
# 前提该文件已经有仓库中的历史版本
git commit –a –m [message]
查看信息
# 显示变更信息
git status
# 显示当前分支的历史版本
git log
git log --oneline
说明:
- Workspace:工作区
- Index / Stage:暂存区
- Repository:仓库区(或本地仓库)
- Remote:远程仓库,例如:Github
同步远程仓库
# 增加远程仓库,并命名
git remote add [shortname] [url]
# 将本地的提交推送到远程仓库
git push [remote] [branch]
# 将远程仓库的提交拉下到本地
git pull [remote] [branch]