配置

查看配置

1
2
3
git config --global --list
# or
git config --global -l

修改配置方法

命令行修改

使用命令行添加或修改配置:

PS: 有3种级别的配置

1
2
3
4
5
6
# system %Git/etc/gitconfig
git config --system core.autocrlf xxx
# user ~/.gitconfig
git config --global user.name xxx
# local workspace/.git/config
git config --local remote.origin.url xxx

使用命令行删除配置:

1
git config --global --unset alias.别名

配置文件修改

使用如下命令可以快速打开config文件

1
git config --global --edit

也可以手动打开文件,config文件内容类似下文,修改后保存即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[core]
editor = \"C:\\coding\\Sublime Text 3\\sublime_text.exe\" -w
autocrlf = true
[user]
name = username
email = example@example
[http]
version = HTTP/1.1
[alias]
st = status
br = branch
co = checkout
ci = commit -m
l = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
# pr = pull --rebase origin master
# pru = pull --rebase upstream master
# r = remote -v
# rau = remote add upstream
# pf = push origin master --force
[i18n]
commitencoding = utf-8
logoutputencoding = utf-8

个人身份

1
2
git config --global user.name "username"
git config --global user.email "example@example"

换行符

Windows

1
git config --global core.autocrlf true

Linux or Max

1
git config --global core.autocrlf input

only Windows

1
git config --global core.autocrlf false

中文编码

1
2
3
4
5
6
# zn encoding
git config --global gui.encoding utf-8
git config --global i18n.commitencoding utf-8
git config --global i18n.logoutputencoding utf-8
# show path in Chinese
git config --global core.quotepath false

与服务器认证

1
2
3
4
5
6
7
# key
git config --global credential.helper store
# https
git config http.sslverify false

ssh-keygen -t rsa -C example@example
# copy ssh key to plat

常用基本指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 工程准备
git init
git clone
# 新增/删除/移动 文件到暂存区
git add
git rm
git mv
# 查看工作区
git diff
git status
# 提交更改的文件
git commit
# 查看日志
git log
# 推送远端仓库
git push
# 分支管理
git branch
git checkout
git branch -d
git pull
# 分支合并
git merge
git rebase
# 强制回退到历史节点
git reset
git checkout

别名设置

添加git的alias

1
git config --global alias.别名 "命令全称"

个人配置

1
2
3
4
5
git config --global alias.st "status"
git config --global alias.br "branch"
git config --global alias.co "checkout"
git config --global alias.ci "commit"
git config --global alias.l "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

config文件相关内容如下

1
2
3
4
5
6
[alias]
st = status
br = branch
co = checkout
ci = commit -m
l = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

修改commit

修改最后一次提交 commit 的信息

1
2
3
4
5
6
7
8
# 修改最近提交的 commit 信息
git commit --amend --message="modify message" --author="username <example@example>"

# 仅修改 message 信息
git commit --amend --message="modify message"

# 仅修改 author 信息
git commit --amend --author="username <example@example>"

修改历史提交 commit 的信息

操作步骤:

  • git rebase -i 列出 commit 列表
  • 找到需要修改的 commit 记录,把 pick 修改为 edite:wq 保存退出
  • 修改 commit 的具体信息git commit --amend,保存并继续下一条git rebase --continue,直到全部完成
  • 中间也可跳过或退出git rebase (--skip | --abort)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 列出 rebase 的 commit 列表,不包含 <commit id>
git rebase -i <commit id>
# 最近 3 条
git rebase -i HEAD~3
# 本地仓库没 push 到远程仓库的 commit 信息
git rebase -i

# vi 下,找到需要修改的 commit 记录,```pick``` 修改为 ```edit``` 或 ```e```,```:wq``` 保存退出
# 重复执行如下命令直到完成
git commit --amend --message="modify message" --author="username <example@example>"
git rebase --continue

# 中间也可跳过或退出 rebase 模式
git rebase --skip
git rebase --abort

修改完成后,强制推送到远程服务器

1
git push --force