LearningGit

diff:比较两个文件或目录之间的差异

1
diff -u left.c right.c

patch:diff的反向操作

1
2
patch left.c diff.txt // left.c -> right.c
patch -R right.c diff.txt // right.c -> left.c

Git特点:分布式,记录照流流,本地操作&远程同步,不适合二进制文档版本控制

Git安装与配置

Linux

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 1. 包管理器安装
# Ubuntu
sudo apt install git
sudo apt install git-doc git-svn git-email gitk
# Others
yum install git
yum install git-svn git-email gitk

# 2. 从源代码安装
http://git-scm.com/
# 下载,如:git-2.19.0.tar.gz
tar -jxvf git-2.19.0.tar.bz2
cd git-2.19.0
# 安装文档再INSTALL文件中
make prefix=/usr/local all
sudo make prefix=/usr/local install
# 安装Git文档
make prefix=/usr/local doc info
sudo make prefix=/usr/local install-doc install-html install-info
# 将Git源码中的命令补全脚本复制到bash-completion对于的目录中
cp contrib/completion/git-completion.bash /etc/bash_completion.d/
# 重新加载
./etc/bash_completion
# 为自动加载脚本,修改配置文件~/.bash_profile或/etc/bashrc,在末尾加上
if [ -f /etc/bash_completion ]; then
./etc/bash_completion
fi

Windows

1
2
3
4
5
6
7
8
http://git-scm.com/download/win
# 不勾选git-lfs
# Use Git Bash Only
# 测试:git version
# 图形界面工具推荐:TortoiseGit
http://code.google.com/p/tortoisegit
# 建议勾选TortoisePLink作为SSH客户端
# SSH client选择Git提供的客户端:C:\Program Files\Git\usr\bin\ssh.exe

Git基本配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 系统配置(对所有用户有效)
git config --system core.autocrlf
# 用户配置(对该用户有效)
git config --global user.name
# 仓库配置(只对该项目有效)
git config --local remote.origin.url

# 配置个人身份
git config --global user.name "ZhangXujia"
git config --global user.email xujiaz2000@163.com

# 文本换行符配置(Windows)
git config --global core.autocrlf true
# (Linux)
git config --global core.autocrlf false

# 文本编码配置
# 中文编码支持
git config --global gui.encoding utf-8
git config --global i18n.commitencoding utf-8
git config --global i18n.logoutputencoding utf-8
# 中文路径支持
git config --global core.quotepath false

# 与服务器认证配置
# 1. http/https协议认证
# 设置口令缓存
git config --global credential.helper store
# 添加HTTPS证书认证
git config http.sslverigy false
# 2. SSH协议认证
# 生成公钥(Git Bash)
ssh-keygen -t rsa -C xujiaz2000@163.com
cat ~/.ssh/id_rsa.pub
# 添加公钥到代码平台:Profile Setting -> SSH Keys -> Add SSH Key -> 复制公钥到Public Key,保存
ssh -T git@github.com # 验证

Git基本命令

Git工程区域

  • 版本库(Repository),存放版本数据,目录:.git
  • 工作区(Working Directory),存放代码
  • 暂存区(stage),索引,目录:./git/index

文件的三种状态

  • 已提交(committed),文件已保存到本地数据库
  • 已修改(modified),修改但未提交保存
  • 已暂存(staged),把已修改的文件放在下次提交时要保存的清单中

Git常用命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 在本地项目目录LocalProject下,新建git项目仓库
git init LocalProject
# 克隆远程工程到本地
git clone [url] # 二进制项目:git lfs clone [url]
# 增加/删除/移动文件到暂存区
git add [fileName]
git rm [fileName]
git mv [srcfilePath] [dstfilePath]
# 显示工作目录和暂存区状态
git status
# 比较差异
git diff 423b7e8 f2efb8f # 比较两个节点
git diff master ..lin/develop/framework # 比较两个分支
git diff --cached # 比较当前所有和上次提交
--name-status # 参数,只比较文件列表差异
# 提交
git commit fileName -m "commit message"
git commit -am "commit message"
# 查看提交记录
git log # 参数:--name-status, -p, --pretty, --graph等等
# 推送分支branchName到远端,origin为远端仓库的默认别名,推送的分支名可以新取[:newBranchName]
git push origin [branchName[:newBranchName]]
# 查看本地工程的所有git分支名称
git branch
git branch -r # 查看远端服务器分支
git branch -a # 查看远端和本地分支
# 新建分支(基于当前分支节点创建)
git branch newBranchName # 新建不切换
git checkout -b newBranchName # 新建并切换
# 删除分支,-D强制删除
git branch -d branchName # 删除本地分支
git branch -d -r localBranchName # 删除远端分支(步骤1)
git push origin localBranchName # 删除后需推送远端(步骤2)
# 切换分支,-f强制切换
git checkout branchName # 参数也可以是节点下的文件名
# 拉取远端分支更新,并与本地分支合并
git pull origin remoteBranch:localBranch
git pull origin remoteBranch # 分支名相同可缺省
# 拉取但不合并
git fetch origin remoteBranch:localBranch
git fetch origin remoteBranch # 分支名相同可缺省
# 分支合并到当前分支(合并分支前的差异节点)
git merge branchName
git rebase branchName # 改变提交历史(线性化),不建议!
# 撤销add/commit等操作,回退到历史节点
git reset commitID [--mixed/hard/soft]
git checkout -- fileName # 回退文件fileName未提交的改动
git checkout . # 回退本地所有修改而未提交的文件内容,用暂存区的所有文件覆盖本地文件

Ref

  1. Git官方文档
comments powered by Disqus