Git 运行配置(git config、gitk、git gui)

良好的配置有助于提高效率,减少不必要的错误。把运行配置的相关内容单拎出来放到这里,查找和更新方便。

设置运行环境
git config --global user.name <your name>
git config --global user.email <your_email@example.com>
git config --global push.default simple
git config --global core.quotepath false
git config --global core.editor /usr/bin/vim
git config --global credential.helper 'cache --timeout=8640000'
git config --global core.ignorecase false
git config --global core.pager 'less -x1,5'
  • user.name;设置你的称呼;
  • user.email;设置你的邮箱;
  • push.default simple;This is the safest option and is suited for beginners. 在 git push 时不必指定 [<repository> [<refspec>...]];
  • 8640000 表示有效期 100天
  • git config --global credential.helper wincred 在 Win 下设置;
  • less -x1,5 设置 git diff 和 git show 时的 tab 为 4(初始为8);
  • core.autocrlf;Win Git Bash 时设置,见 git replacing LF with CRLF
  • git config -l;查看所有的(name.key)/(value) -l | --list
  • git config --unset;Remove the line matching the key from config file.
  • git config --unset-all:Remove all lines matching the key from config file.
  • git config --global core.quotepath false:解决 Windows Git Bash、Linux 下的中文转码问题;
  • git config --global core.editor /usr/bin/vimOS X 下 merge 命令 vi error 问题;通常 core.editor=vim。
  • git config --global credential.helper wincred:Win Git Bash 启用 http/https 协议时设置。
  • git config --global core.ignorecase false
    设置大小写敏感,保持 Mac/Win/Linux一致性;在目录名大小写修改时,git可正常提交;
关于 rebase 和 merge 的设置
  • 为了代码 log 易读,请使用 rebase
git config --global pull.rebase true
git config --global branch.autoSetupRebase always
关于 CRLF 问题的通常做法
  • Windows 下
git config --global core.autocrlf true
  • Linux & Mac
git config --global core.autocrlf input
  • 库 repo 中一定是只用 LF 做行结束符的;因此,完全在 Mac 下开发通常不会带入,但难免和 Win 环境下的同学或者外界交流,还是需要设置 input 的;
设置 .gitattributes 文件指定 CRLF 规则
  • .sh shell 文件是需要 LF 作为行结束符的
    否则使用 scp 在从 Win 向 Linux 传输 shell 文件时,shell 执行有问题,还得使用 dos2unix 小工具进行 额外修复(要么传输前修复,要么传输后修复)。
  • 在 git 库根目录下新增 .gitattributes 文件
*.sh text eol=lf
git add . -u
git commit -m "Saving files before refreshing line endings"
git rm --cached -r .
git reset --hard
git add .
# It is perfectly safe to see a lot of messages here that read
# "warning: CRLF will be replaced by LF in *file*."
git commit -m "Normalize all the line endings"
Git asks for username every time I push

git config credential.helper store
不再需要每次输入账号密码。
查看 /.git-credential、/.gitconfig、每个工程下的 .git/config 文件可了解更多。.git-credential 文件记录的都是明文,比较危险。建议使用:git config credential.helper cache,具体查看 git help credentials
git config --global credential.helper 'cache --timeout=8640000' # 100天 在 linux下使用。

如何重新输入账号密码?

使用 http 方式访问 git repo 时,假如你密码忘记了,或者服务端不小心清空了你的账号密码,那么 git 会返回 403,但并没有如你所期弹出输入账号密码的提示框,怎么办

建议
  • Win 下同学,如果只使用 Git Bash 环境开发,完全可以参照 Linux 配置,省却一切麻烦;
  • 开发工具:不论 Zend,Sublime,WebStorm,对于跨平台文件格式的支持都很好;这倒不必担心;
git config 配置文件
  1. 系统级文件 $(prefix)/etc/gitconfig
    本文即 /usr/etc/gitconfig 文件。
    git config --system 用来指定读写系统级文件。初始不存在,若不存在则无影响。
  2. 用户级文件 ~/.gitconfig
    git config --global 指定只操作用户级文件。初始不存在,若不存在则无影响。
  3. Repository 级文件 .git/config
    git config --local 对写操作,则只写入 Repository 级文件(默认行为);对读操作,则只从 Repository 级文件读。
  4. git config --file config-file 则指定 config-file。
  5. ~/.config/git/gitk
    这是 gitk 的配置文件;
  6. git config --get <keyname>:列出 <keyname> 的值;
    注:如果不特别指定哪个配置文件,则依 system/global/local 顺序读取,最后的值覆盖前面的,多值的则合并。
  7. git config --get-all <keyname>
备注
gitk 和 git gui

Git 有很多 GUI 客户端软件。git 包中通常包含 git guigitk 这两个内置 tools。在 gitk 中配合使用 git gui。

  • gitk 界面,可以看到树状图、作者、搜索区、变更说明及变更内容、变更文件列表。


    在《内容变更对照区》,鼠标右键弹出《Run git gui blame on this line》
  • 在gitk界面,打开 File>Start git gui 即可 打开 git gui 界面
    File>Start git gui
  • git gui 界面图


    git gui 界面
  • git gui 的 Edit > Option 将 Default File Contents Encoding 由 cp936 改为 UTF-8


    git gui 的 Edit > Option 将 Default File Contents Encoding 由 cp936 改为 UTF-8

gitee显示未加入企业?

windows git bash 使用 git config --unset 原有user.email和user.name,--add 新的之后,windows下 git clone 提示:

$ git clone https://gitee.com/xxx/sapi_bootstrap.git
Cloning into 'sapi_bootstrap'...
remote: You hasn't joined this enterprise!
fatal: unable to access 'https://gitee.com/xxx/sapi_bootstrap.git/': The requested URL returned error: 403

原因:“Windows 凭据”记录了原账号/密码,而该账号确实未加入该企业。windows 搜索“凭据”,修改即可。


Windows 凭据
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容