#5 Git 与换行符

2017-12-07

CR:Carriage Return 回车
LF:Line Feed 换行
EOL: End Of Line

平台 代码 数值 转义字符
Windows CRLF 13 10 \r\n
Linux/Unix LF 10 \n
Mac OS CR 13 \r
  • Mac OS X 开始,也使用 LF 做换行符。

Git 相关配置项

  • core.eol,换行符,可选:lf,crlf,native(根据系统判断,默认)
  • core.safecrlf,是否接受非 LF 换行,可选:true(拒绝),false(允许),warn(警告,默认)
  • core.autocrlf,是否自动转换换行符,可选:true(push lf,pull crlf),false(默认),input(push lf)

Linux 上,这三个配置项的默认值就非常恰当了,不用修改。
代码中的换行符应该由开发者自己判断、处理,工具提醒一下就行了。

如果项目组有共识,那么使用一个共同的配置也可以,比如:

git config --global core.eol lf
git config --global core.safecrlf true
git config --global core.autocrlf input
# 如果 CRLF 转换,会有警告提示:
# warning: in the working copy of 'README.md', CRLF will be replaced by LF the next time Git touches it

#4 使用 git-daemon

2017-03-15

有时需要临时分享一个仓库给朋友,我们可以用 SSH 协议:

git clone ssh://markjour@192.168.64.234/home/markjour/Projects/Mine/lego

其实 git-daemon 是一个更好的方法。

#3 Git: matches more than one

2016-09-01
$ git push origin v1.1.2 --delete
error: 目标引用规格 v1.1.2 匹配超过一个
error: 无法推送一些引用到 'gitee.com:markjour/markjour'

$ git push origin v1.1.2 --delete
error: dst refspec v1.1.2 matches more than one
error: failed to push some refs to 'gitee.com:markjour/markjour'

解决办法:

# 如果要删除的是分支
git push origin refs/heads/v1.0.32 --delete
To gitee.com:markjour/markjour
 - [deleted]         v1.0.32

# 如果要删除的是 Tag
git push origin refs/tags/v1.0.32 --delete

#2 Git: 远程引用不存在

2016-01-31

删除远程分支时报错:

git push --delete origin new
error: 无法删除 'new':远程引用不存在
error: 无法推送一些引用到 'gitee.com:markjour/django-admin'

如果是英文环境就是报:

git push --delete origin new
error: unable to delete 'new': remote ref does not exist
error: failed to push some refs to 'gitee.com:markjour/django-admin'

一般是这个分支已经被别人删除了。

Solution

git branch -d -r origin/new

#1 Git 基础

2013-12-03

Linus 为了托管内核代码的方便,创建了 Git。
之前,Linux 开发者使用的是 BitKeeper,不过社区对这个私有工具总是不放心,然后 BitKeeper 方面对于部分开发者试图逆向工程不满。

对于大多数公司来说, Git 的设计颇为复杂。
但是没办法,现在 Git 已经成为大多数开发者的选择。

基础命令

  • git config 配置
  • git init 初始化当前目录为 git 仓库
  • git clone 克隆一个远程仓库到本地
  • git add 添加变更(提交之前的挑选)
  • git commit 提交变更
  • git push 推送变更到远程仓库
  • git pull 拉取远程仓库变更,并合并到本地;等于 git fetch + git merge
  • git status 查看变更状态
  • git log 查看提交历史
  • git branch 分支管理
  • git checkout 切换分支
  • git fetch 拉取远程仓库变更
  • git merge 合并分支

示例

git config --global user.name "Your Name"
git config --global user.email "your@email.com"

git clone git@github.com:your-username/example
cd example

# 创建新文件
echo "hello" > newfile
git add newfile
git commit -m "Add newfile"

git push origin master

参考资料与拓展阅读