Skip to content

Config

把VS Code设置为Git的编辑器

git config --global core.editor "code --wait"

git config --global -e # 打开设置文件

添加如下内容,设置VSCode为Git的合并比较工具:

[diff]
    tool = default-difftool
[difftool "default-difftool"]
    cmd = code --wait --diff $LOCAL $REMOTE
[merge]
    tool = code

个人信息配置

# 项目配置文件,只针对当前项目: 项目/.git/.gitconfig
git config --local user.email "for@example.com"
git config --local user.name "Your Name"

# 全局配置文件,只针对当前用户: ~/.gitconfig
git config --global user.email "for@example.com"
git config --global user.name "Your Name"

# 系统配置文件,包含系统上每一个用户及他们仓库的通用配置: /etc/.gitconfig
git config --system user.email "for@example.com"
git config --system user.name "Your Name"

查看设置

git config --list --system
git config --list --global
git config --list --local
# `git config`默认是local

提交模板

git config --global commit.template <path_to_file> # exp: $HOME/.gitcommitmsg.txt

When Git creates a new repository, either via init or clone, it will copy the files from the template directory (the default location is /usr/share/gitcore/templates) to the new repository when creating the directory structure.

The template directory can be defined either by a command-line argument, an environment variable, or a configuration option. If nothing is specified, the default template directory will be used

别名设置

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.last 'log -1 HEAD'
git config --global alias.unstage 'reset HEAD --'

git config --global alias.graph "log --all --graph -pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ci) %C(boldblue)<%an>%Creset'"

git config --global alias.ll "log --pretty=format:'%C(yellow)%h%Cred%d %Creset%s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --numstat"

# To create an alias method with an external command, the alias must start with an exclamation mark !.

rebase和merge设置

git config pull.rebase true # 默认值是false
# This configuration, when set to true, will pull to rebase the current branch on top of the fetched one when performing a git pull

git config branch.autosetuprebase always # 默认值是never
# When this configuration is set to always, any new branch created with <git branch or git checkout that tracks another branch will be set up to pull to rebase (instead of merge). 
# The valid options are as follows: 
#  never: This is set to pull to rebase (default) 
#  local: This is set to pull to rebase for local tracked branches 
#  remote: This is set to pull to rebase for remote tracked branches 
#  always: This is set to pull to rebase for all tracked branches 

git config branch.<name>.rebase true
# This configuration, when set to true, applies only to the <name> branch and tells Git to pull to rebase when performing git pull on the given branch

垃圾回收

# gc.reflogExpire
# gc.<pattern>.reflogExpire

git config --global gc.reflogExpire 180.days.ago
git config --global gc.reflogExpire never

git reflog expire removes reflog entries older than this time; defaults to 90 days. The value "now" expires all entries immediately, and "never" suppresses expiration altogether.

With "<pattern>" (e.g. "refs/stash") in the middle the setting applies only to the refs that match the <pattern>.

# gc.reflogexpireunreachable
git config gc./refs/remote/*.reflogexpireunreachable "2 months"

# gc.pruneexpire

自动更正

git config help.autocorrect 5
# By setting the configuration to help.autocorrect, you can control how Git will behave when you accidentally send a typo to it. 
# By default, the value is 0 and it means to list the possible options similar to the input (if statisis given, status will be shown). 
# A negative value means to immediately execute the corresponding command. 
# A positive value means to wait the given number of deciseconds (0.1 sec) before running the command (so there is an amount of time in which to cancel it).

免密码登陆

URL中体现

git remote add origin https://user:password@仓库地址
git push origin master

SSH实现

ssh-keygen # 生成公钥和私钥(默认放在`~/.ssh`目录下, `id_rsa.pub`公钥, `id_rsa`私钥)
# 拷贝公钥的内容, 设置到GitHub
git remote add origin git@github.com:地址 # 在本地设置ssh地址
git push origin master