一、 全局配置
1. 配置文件
git全局配置文件.gitconfig默认在当前系统用户文件夹下,window可运行%USERPROFILE%查找,Mac系统在cd ~查找。
具体配置可参考如下,其中:
【user】: 用户提交时显示在log里的信息
【alias】: 常用git命令简写
【core】: window系统和类linux系统回车键转换
【push】: 默认对应远端(当本地分支名与远程分支名不一致有用)
[user]
name = hoby
email = hoby@github.com
[alias]
st = status
co = checkout
br = branch
ci = commit
pl = pull --rebase
ps = push
mg = merge --no-ff
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
[core]
autocrlf = input
ignorecase = false
[push]
default = upstream
若要使用git mergetool功能,再增加配置(cmd处可修改成本地文件比较工具如beyond compare):
[merge]
tool = sourcetree
[diff]
tool = sourcetree
[difftool "sourcetree"]
cmd = 'C:/Program Files/TortoiseGit/bin/TortoiseGitMerge.exe' \"$LOCAL\" \"$REMOTE\"
[mergetool "sourcetree"]
cmd = 'C:/Program Files/TortoiseGit/bin/TortoiseGitMerge.exe' -base:\"$BASE\" -mine:\"$LOCAL\" -theirs:\"$REMOTE\" -merged:\"$MERGED\"
trustExitCode = true
2. 命令修改
如果不加--global 修改的是当前项目git配置。
$ git config --global --list // 查看全局配置 $ git config --global user.name "用户名"
$ git config --global user.email "邮箱" $ git config --global alias.br branch // 修改简写 $ git config --unset alias.co // 删除配置项 $ git config --global core.ignorecase false // 关闭忽略大小写
二、 Git密钥配置
1. 以SSH方式创建Git项目
1.)检查SSH Key存在
如果存在id_rsa.pub 或 id_dsa.pub 文件,跳过此步。
$ cd /root/.ssh // linux默认在/root/.ssh下,windows在当前用户下有个.ssh文件夹
$ ls
2.)创建SSH Key
创建ssh key时会提示自定名称和push时的密码(不是git登录密码),一般推荐略过,直接三个回车,如果创建成功会出来一个有图案的小框框。
$ ssh-keygen -t rsa -C "your_email@example.com" // 此处email可任意,不一定要gitLab登录邮箱
3.)查看SSH Key
copy公钥内容到gitLab里,添加进去。
$ cat ~/.ssh/id_rsa.pub
4.)测试SSH Key
$ ssh -T git@xxx.xxx.com //ssh -T git@git.oschina.net 或 ssh -T git@githup.com
Welcome to xxxx, yourname! 出现这句话表示成功
5.)配置多个网站ssh密钥
在生成每个网站ssh-key时,自定义名称不要一样,然后在~/.ssh目录下新建一个config文件,然后配置多个网站的ssh信息,内容如下:
# gitLab
Host dev.gitLab.com
HostName dev.gitLab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# phabricator
Host 192.168.1.5
HostName 192.168.1.5
Port 22
PreferredAuthentications publickey
IdentityFile ~/.ssh/phabricator
2. 以HTTP(S)方式创建Git项目
http(s)创建的项目,操作时总是提示用户名和密码,不胜其烦,以下方案可以让Git永远记住用户名与密码:
1.)添加HOME变量
在Windows用户变量中添加一个HOME环境变量,值为%USERPROFILE%,如下图:
2.)创建_netrc文件
在"开始>运行"中打开%Home%,新建一个名为_netrc的文件,输入Git服务器名、用户名、密码保存,可放多个不同登录信息的Git项目,中间空一行即可,具体内容如下:
machine dev.github.com
login hoby
password 123456