Git 学习笔记
老李 Lv4

设置邮箱和用户名

1
2
3
4
5
6
# 为某个项目设置
git config user.email "mail address"
git config user.name "user name"
# 全局设置
git config --global user.email "mail address"
git config --global user.name "user name"

禁止 CRLF 自动转为 CR, LF

1
git config --global core.autocrlf false

显示中文

1
git config --global core.quotepath false

添加远程仓库

1
git remote add <shortname> <url> # 例如: git remote add origin git@github.com:Thorxh/NOTE.git

git 撤销操作

1
2
git reset HEAD <file_path>  # 撤销暂存区的记录,工作区的文件不变
git checkout -- <path> # 重置工作区中的文件,如果暂存区有暂存文件,则从暂存区重置文件,否则从上次提交中重置文件

查看远程仓库信息

1
2
3
git remote                  # 列出你指定的每一个远程服务器的简写
git remote -v # 会显示需要读写远程仓库使用的 Git 保存的简写与其对应的 URL
git remote show origin # 要查看某一个远程仓库的更多信息

远程仓库的移除与重命名

1
2
git remote rename pb paul   # 修改你的远程分支名字
git remote rm paul # 移除一个远程仓库

推送到远程仓库

1
2
git push origin master                  # 将本地 master 分支推送到远程仓库 origin 上的 master 分支
git push origin localbranch:serverfix # 将本地 localbranch 分支推送到远程仓库 origin 上的 serverfix 分支

从远程仓库中抓取与拉取

1
2
git fetch [remote-name]     # 从远程仓库中获得数据,它并不会自动合并或修改你当前的工作
git pull [remote-name] # 通常会从最初克隆的服务器上抓取数据并自动尝试合并到当前所在的分支

Git 别名

1
2
git config --global alias.unstage 'reset HEAD --'   # 取消暂存文件别名
git config --global alias.last 'log -1 HEAD' # 轻松地看到最后一次提交

创建分支

1
git branch testing          # 创建分支

查看分支

1
git branch                  # 创建分支

切换分支

1
git checkout master         # 切换到 master 分支

创建分支并且切换分支

1
git checkout -b <branchname>

gitbash 配置代理

HTTP 协议的配置

1
2
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'

SSH 协议的配置

1
2
3
4
5
6
7
8
9
10
11
1. 新建文件 C:\Users\{user_name}\.ssh\config
2. 编辑 config 文件
3. 添加(127.0.0.1:1080 为本地代理地址):
Host github.com
ProxyCommand connect -H 127.0.0.1:1080 %h %p
HostName %h
Port 22
User git
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
4. 将 connect.exe 文件复制到 gitbash 安装目录下的 bin 目录中

查看分支相关信息

1
git reflog show --date=iso <branch name>

分支操作

合并分支

1
git merge origin/serverfix  # 合并 serverfix 分支到当前分支

删除本地存在远程不存在的远程分支

1
git remote prune origin     # 清理远程分支,把本地还存在远程已经不存在的远程分支删除

删除远程分支

1
git push origin --delete serverfix      # 删除远程分支

删除本地分支##

1
git branch -d <branchname>

查看哪些分支已被并入当前分支

1
git branch --merged

查看哪些分支未被并入当前分支

1
git branch --no-merged