Git的安装与简单配置-Git详解(1)

Git 的安装与简单配置

安装官网
https://git-scm.com/

Git 配置

windows 下载安装包安装即可

linux 的安装 (ubuntu)

1
$ sudo apt-get install git

配置系统当前git(如系统安装了多个git)

1
2
$ which -a git # /usr/bin/git 返回只有一个
$ git --version

如果有多个git,可以指定git版本

1
$ vim ~/.bashrc #linux mac修改 ~/.bash_profile

加入,修改PATH环境变量,ubuntu 暂不需要修改

1
2
#~/.bashrc 增加
export PATH=/usr/local/git/bin:$PATH

使用source 使设置生效

1
$ source ~/.bashrc

基本配置

系统每次提交的时候会使用以下信息

1
2
$ git config --global user.name = shuncle
$ git config --global user.email = shuncle@outlook.com

配置的3个级别 (优先级 local > global > system)

  • local
  • global
  • system

查看git文档的三种方式

1
2
3
$ git config --help
$ git help config
$ man git-config

config 的增删改查
增或改

1
2
$ git config --global --add user.name eoe
$ git config user.name eoe


1
2
$ git config user.name
$ gti config --get user.name

查看所有global的键值对

1
$ git config --list --global

删除

1
$ git config --global --unset user.name eoe

只有一个值的时候,可以这样

1
$ git config --global --unset user.name

用git config 给 git 子命令取别名

1
2
3
$ git config alias.lol 'log --oneline --decorate --all'
# 使用
$ git lol

为linux 命令行添加自动完成(apt 安装已支持) 和 命令行分支提示功能
访问网站 http://github.com/git/git

1
2
3
$ cd ~
$ wget https://github.com/git/git/tree/master/contrib/completion/git-prompt.sh
$ vim ~/.bashrc

在文件 ~/.bashrc 中添加

1
2
3
. ~/lab/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

使用source 命令使设置生效

1
$ source ~/.bashrc

git 升级

1
2
3
$ sudo add-apt-repository ppa:git-core/ppa
$ sudo apt-get update
$ sudo apt-get install git

配置 CRLF

CRLF – Carriage-Return Line-Feed 回车换行
就是回车(CR, ASCII 13, \r) 换行(LF, ASCII 10, \n)。
这两个ACSII字符不会在屏幕有任何输出,但在Windows中广泛使用来标识一行的结束。而在Linux/UNIX系统中只有换行符。
也就是说在windows中的换行符为 CRLF, 而在linux下的换行符为:LF
当执行git add .时,系统提示:LF 将被转换成 CRLF

1
git config --gobal core.autocrlf false