🐶 Git常用操作
# Git常用操作
[廖雪峰Git教程](https://www.liaoxuefeng.com/wiki/896043488029600)
## 自定义Git
### 设置自动创建上游分支
```
git config --global --add --bool push.autoSetupRemote true
```
### 设置查看提交树
```
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
```
## 生成密钥和公钥
```
# b参数指的是生成密钥的位数,显然越大越安全
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 测试能否连接上
ssh -T git@gitee.com
```
## 协同开发
首先要切换回master分支,然后使用`pull`同步远程代码
#### 切换分支
```
git checkout master
```
#### 新建分支
```
git branch new-branch-name
```
#### 推送分支
因为新建的分支没有`上游分支`,需要加入参数`--set-upsteam`,这样会在远程自动创建`上游分支`
```
git push --set-upstream origin master
```
## 上传项目
在Gitee上创建好项目之后,将本地的代码上传至代码仓库。
#### 初始化仓库
```
git init
```
#### 查看仓库状态
```
git status
```
#### 连接远程仓库
>w ssh协议需要将公钥添加到Gitee中
```
git remote add origin https://gitee.com/ying_hao_dang/reptile_picture.git
git remote add origin git@gitee.com:ying_hao_dang/reptile_picture.git
```
#### 同步远程代码
```
git pull origin master
```
#### 添加代码到暂存区
```
git add .
```
#### 提交信息
```
git commit -m "your message"
```
### 推送代码到远程仓库
```
git push origin master
```
## 其他操作
### git 使用代理
```
git config --global http.proxy http://127.0.0.1:7890
git clone http://xxx.git -c http.proxy http://127.0.0.1:7890
```
### git自动记住账号密码
正常操作是将自己的公钥上传到gitlab上,然后自己的代码仓库就可以不用输入密码。
别人的代码仓库,就需要将自己的公钥上传到代码仓库。
但是这个操作有时候会不太方便。但是每次git pull 都需要输入账号密码也忒烦了,这时候
```
git config --global credential.helper store
```
就派上用场了,这个设置可以使得git自己记住曾经输入过的账号密码。