首页 > 技术文章 > 配置一个高效快速的Git环境

whenyd 2017-11-05 01:04 原文

可以直接修改~/.gitconfig文件,也可以用命令配置一个可以实际使用的高效的Git环境。

username and email

这两项是必须的。

git config --global user.name gituser
git config --global user.email email@git.com

or

[user]
    name = gituser
    email = email@git.com

editor

git config --global core.editor "code -n -w"

or

[core]
    editor = /usr/bin/code -n -w 

指定外部编辑器用于编辑多行commit,比如vscode。

  • -n(new)打开新窗口编辑commit
  • -w(wait)使git等待用户退出编辑器后才继续执行,以免执行git commit之后出现Aborting commit due to empty commit message.的错误

difftool and mergetool

using meld, for ubuntu:

sudo apt install meld
git config --global diff.tool meld
git config --global merge.tool meld
git config --global mergetool.keepBackup false

or

[diff]
    tool = meld
[merge]
    tool = meld
[mergetool]
    keepBackup = false
  • keepBackup = false成功合并后可以自动删除备份文件。

alias

Git命令别名,更高效更快速更专业...a-ha......

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st "status --short"
  • co作为checkout的别名,以此类推
  • st作为status --short的别名,输出信息更精简(虽然不符合linux命令越短事越大的风格)

配置文件如下:

[alias]
    br = branch
    brd = branch -d
    ck = checkout
    ckb = checkout -b
    ckf = checkout --
    cm = commit
    cmm = commit -m
    df = diff
    lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
    last = log -1 HEAD
    mg = merge
    ss = stash
    st = status --short
    stt = status
    unstage = reset HEAD --

进一步,在linux的别名文件~/.bash_aliases中定义如下别名,这样可以更快的执行命令:

# git
alias gg='gitg'             # gitg
alias gadd='git add'
alias gbr='git br'          # branch
alias gbrd='git brd'        # branch -d
alias gck='git ck'          # checkout
alias gckb='git ckb'        # checkout -b
alias gckf='git ckf'        # checkout --
alias gcm='git cm'          # commit
alias gcmm='git cmm'        # commit -m
alias gdf='git df'          # diff
alias gdfa='git diffall'    # git-diffall
alias gdfah='git diffall HEAD'    # git-diffall
alias gdfal='git diffall HEAD HEAD^1'
alias glg='git lg'          # colorful oneline git log
alias glast='git last'      # log -1
alias gmg='git mg'          # merge
alias gss='git ss'          # stash
alias gst='git st'          # status --short
alias gstt='git stt'        # status

推荐阅读