您的位置:首页 > 其它

GIT(分布式版本控制系统)

2015-06-16 20:57 330 查看

1. 简介

Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件

2. GIT和SVN的区别

GIT是分布式的,SVN不是

还有一些系统,例如Bitkeeper, Mercurial等,也是运行在分布式模式上的。但GIT在这方面做的更好,而且有更多强大的功能特征

分布式模式,如果你被困在一个不能连接网络的地方时,你仍然能够提交文件,查看历史版本记录,创建项目分支等

GIT把内容按元数据方式存储,而SVN是按文件

.git目录的体积大小跟.svn比较

它拥有中心版本库上所有的东西,例如标签,分支,版本记录等

GIT分支和SVN的分支不同

GIT没有一个全局的版本号,而SVN有

GIT的内容完整性要优于SVN

GIT的内容存储使用的是SHA-1哈希算法。这能确保代码内容的完整性,确保在遇到磁盘故障和网络问题时降低对版本库的破坏

3. 下载

http://git-scm.com/downloads

4. linux安装

//Debian/Ubuntu
$ apt-get install git
//Fedora
$ yum install git
//Gentoo
$ emerge --ask --verbose dev-vcs/git
//Arch Linux
$ pacman -S git
//openSUSE
$ zypper install git
//FreeBSD
$ cd /usr/ports/devel/git
$ make install
//Solaris 11 Express
$ pkg install developer/versioning/git
//OpenBSD
$ pkg_add git


5. git配置

Git的config命令或者直接编辑~/.gitconfig

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


$ git config --list        //查看配置


[user]
name = userid
emal = userid@heartsone.net
[color]
status = auto
branch = auto
ui = auto


color在git diff时,显示红色和绿色

6. 创建一个ssh key

$ ssh-keygen -C 'your@email.address' -t dsa

/root/.ssh/id_dsa.pub   #linux
/c/Users/administrator/.ssh/id_rsa.pub  #windows


dsa:指定加密类型

7. 拷贝id_rsa.pub内的公钥到



8. 配置域名解析

c:\windows\system32\drivers\etc\hosts       //windows
/etc/hosts                                  //linux

//添加
192.168.70.130 gerrit.hbu.com


9. 连接成功后

首先要配置name和email,否则,git review时邮箱匹配不上。邮箱需要登录验证,即自动保存



10. git常用命令

#添加
$ git add <file>           # 将工作文件修改提交到本地暂存区
$ git add .                # 将所有修改过的工作文件提交暂存区
$ git add -f               # force


#提交
$ git commit
$ git commit -a            # 可以将那些没有通过git add标识的变化一并强行提交,不建议使用这种方式
$ git commit -m "有意义的附加说明"
$ git commit --amend       # 新的提交来覆盖上一次的提交


#回退
$ git reset head~
$ git checkout a.txt       # 撤销a.txt的修改


#删除
$ git rm <file>            # 从版本库中删除文件
$ git rm <file> --cached   # 从版本库中删除文件,但不删除文件。适用于一次添加了很多文件, 却又想排除个别几个文件的情况。必须在当前路径


#查看diff
$ git diff <file>          # 比较当前文件和暂存区文件差异
$ git diff <id1><id2>      # 比较两次提交之间的差异


#查看提交记录
$ git log git log <file>   # 查看该文件每次提交记录
$ git log -p <file>        # 查看每次详细修改内容的diff
$ git log -p -2            # 查看最近两次详细修改内容的diff


#git远程分支
$ git pull = fetch + merge # 抓取远程仓库所有分支更新并合并到本地
$ git fetch origin         # 抓取远程仓库更新。不会自动merge,比Git pull更安全些
$ git merge origin/master  # 将远程主分支合并到本地当前分支


#git远程仓库
$ git remote -v                                        # 查看远程服务器地址和仓库名称
$ git remote add origin ssh://admin@gerrit.hbu.com     # 添加远程仓库地址
$ git remote set-url origin <repository>               # 设置远程仓库地址(用于修改远程仓库地址)
$ git remote rm <repository>                           # 删除远程仓库


# review代码
$ pip install git-review
$ git review   //windows
$ git-review   //linux


windows 安装python-3.4.3.msi;linux 安装pip。python安装见专题

此时,即可看到review记录



11. git的忽略机制

一些文件是不希望接受Git 管理,写到.gitignore 文件中

12. git分支

$ git branch -a
* master
remotes/origin/HEAD -> origin/master


一个叫 origin 的远程库的 master 分支

13. git换行符

Windows下开发,建议设置autocrlf为true

UTF8并且包含中文文字,那还是把autocrlf设置为false

Linux/Unix  LF  \n
MacOS       CR  \r


AutoCRLF

//提交时转换为LF,检出时转换为CR
$ git config --global core.autocrlf true
//提交时转换为LF,检出时不转换
$ git config --global core.autocrlf input
//提交检出均不转换
$ git config --global core.autocrlf false


SafeCRLF

//拒绝提交包含混合换行符的文件
$ git config --global core.safecrlf true
//允许提交包含混合换行符的文件
$ git config --global core.safecrlf false
//提交包含混合换行符的文件时给出警告
$ git config --global core.safecrlf warn


14. Change-Id错误

remote: ERROR: missing Change-Id in commit message
remote: Suggestion for commit message:
remote: Cosmetic improvements to PostreSQL updater output
remote:
remote: * Don't WARN on sequences already existing
remote: * Align dots nicely to the rest
remote: (cherry picked from commit bd7a268e4be2da23ba0b9943c3b0ba1ac88294dc)
remote:
remote: Change-Id: Ia354acd879c3dd840e7be1e3c6d6fc78d696631d
To ssh://saper@review:29418/mediawiki/core.git
! [remote rejected] HEAD -> refs/for/REL1_19/PostgreSQL (missing Change-Id in commit message)
error: failed to push some refs to 'ssh://saper@review:29418/mediawiki/core.git'


解决办法:

$ git cherry-pick -n bd7a268e4be2da23ba0b9943c3b0ba1ac88294dc
$ git commit -c bd7a268e4be2da23ba0b9943c3b0ba1ac88294dc


remote: Resolving deltas: 100% (1/1)
remote: Processing changes: refs: 1, done
remote: ERROR: missing Change-Id in commit message footer
remote: Suggestion for commit message:
remote: [*][*][*][NA]fix bug[*5]
remote:
remote: Change-Id: I53***80
remote:
remote: Hint: To automatically insert Change-Id, install the hook:
remote:   gitdir=$(git rev-parse --git-dir); scp -p -P port name@hostIp:hooks/commit-msg ${gitdir}/hooks/


解决办法:

$ gitdir=$(git rev-parse --git-dir); scp -p -P port name@hostIp:hooks/commit-msg ${gitdir}/hooks/
$ git commit --amend


15. 冲突处理

<<<<<<< HEAD

test in master

=======

test in dev

>>>>>>> dev


对于简单的合并,手工编辑,然后去掉这些标记,add再commit即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: