Git使用教程(二) — Git安装和使用
使用目的:通过git管理github托管的项目代码
一、下载安装
-
GIt官网下载:https://www.git-scm.com/download/win
或者https://gitforwindows.org
-
双击安装
-
选择安装的工作目录
-
选择组件
-
开始菜单目录名设置
-
选择使用命令行环境
-
以下三步默认,直接点击next
-
等待安装
-
检验是否安装成功
二、 Git基本工作流程
- Git工作区域
- 向仓库中添加文件流程
三、Git初始化及仓库创建和操作
-
基本信息设置
设置用户名:git config --global user.name 'William-github-hub'
设置用户名邮箱:git config --global user.email '3050845604@qq.com'
查看设置:git config --list
注:该设置在github仓库主页显示谁提交了该文件(--global
参数,有了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然你也可以对某个仓库指定的不同的用户名和邮箱) -
初始化一个新的Git仓库
创建文件夹
在文件内初始化git(创建git仓库)
使用cd First
和git init
命令
-
向仓库添加文件
创建a1.c文件到工作目录:touch a1.c
添加a1.c到暂存区:git add a1.c
添加a1.c到仓库:git commit -m 'add a1.c'
-
修改仓库文件
-
删除仓库文件
四、Git管理远程仓库
- 使用远程仓库
使用目的:备份,实现代码共享集中化管理
- Git克隆操作
目的:将远程仓库(github对应的项目)复制到本地
获得仓库地址:
使用git clone +仓库地址
进行克隆操作
- 将本地仓库同步到git远程仓库中
使用git push
命令
添加成功
不想重复输入用户名和密码,将.git/config
文件进行修改
将[remote "origin"]
url = https://github.com/用户名/仓库名.git
修改为:
[remote "origin"]
url = https://用户名:密码@github.com/用户名/仓库名.git
五、优化使用
我们可以添加如下设置来优化我们git的使用
git config --global user.name "william"
git config --global user.email 472853046@qq.com
git config --global color.ui true
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch
git config --global alias.l "log -10 --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.ll "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"