用户信息配置

1
2
3
4
5
$ git config --global user.name "Martin"
$ git config --global user.email "user_name@outlook.com"

# 常用配置:禁止LF换行符自动转换为CRLF换行符
$ git config --global core.autocrlf false

作为身份信息,在第一次是配置就可以,后续可修改。

配置查看

1
2
3
4
5
6
7
8
9
10
11
# 查看全局配置
$ git config --global --list
user.name=your_name
user.email=user_name@outlook.com
credential.helper=manager
color.diff=auto
color.status=auto
color.branch=auto

$ git config --local --list # 查看本地配置
$ git config --system --list # 查看系统配置

生成公钥,免密访问

1
2
3
4
5
6
$ ssh-keygen -t rsa [-b 4096] -C "user_name@outlook.com"  # -b:指定秘钥长度,-C为备注信息,可自定义
# 拷贝到远端,实现对远端的免密访问
$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.135.1 # 这里需要输入用户名和密码
$ ssh root@192.168.135.1 # 测试能否免密登录
# 移除系统中已知的指定主机的SSH密钥缓存
$ ssh-keygen -R [IP/主机名]

​ 通过ssh-keygen生成后,可以在user/.ssh目录下看到id_rsa、id_rsa.pub文件,打开公钥id_rsa.pub,拷贝到github或gitee上的SSH公钥处,后面就可以免密推送到remote端。

新建仓库

1
2
3
4
5
6
# 在当前目录新建一个Git代码库
$ git init
# 新建一个目录,将其初始化为Git代码库
$ git init [project-name]
# 下载一个远端项目和它的整个代码历史
$ git clone [url]

远程仓库相关操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 添加远程仓库源
git remote add origin ssh://git@xxx.cn/BugFix.git
# 查看远程分支
git remote -v
git remote show orgin # 查看指定远程仓库信息

# 获取、下载远程内容 (不合入)
git fetch origin master:master // git merge origin
git pull origin master # 本质:fetch + merge
git pull --rebase origin master

# 推送至远程
git push # 已关联指定远程分支
git push -u origin master
# 推送本地分支到远程指定分支
git push -f origin develop_local:develop

本地分支与远程分支的关联跟踪

1
2
3
4
5
6
7
8
9
10
11
12
13
1. 从指定远程分支拉取到本地, 会自动关联
git checkout -b local_branch origin/develop

2. 手动关联
git branch --set-upstream-to=origin/develop local_branch
git branch -u origin/develop local_branch # 同上

3. 解除关联
git branch --unset-upstream [local_branch]
git branch -u [local_branch]

4. 查看当前关联情况
git branch -vv