Git 使用

Git 是 Linux Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的分布式版本控制软件,它不同于Subversion、CVS这样的集中式版本控制系统。

在集中式版本控制系统中只有一个仓库(repository),许多个工作目录(working copy),而像Git这样的分布式版本控制系统中(其他主要的分布式版本控制系统还有BitKeeper、Mercurial、GNU Arch、Bazaar、Darcs、SVK、Monotone等),每一个工作目录都包含一个完整仓库,它们可以支持离线工作,本地提交可以稍后提交到服务器上。

分布式系统理论上也比集中式的单服务器系统更健壮,单服务器系统一旦服务器出现问题整个系统就不能运行了,分布式系统通常不会因为一两个节点而受到影响。

安装

检查是否已安装git

首先,你可以试着输入git,看看系统有没有安装Git

$ git
The program 'git' is currently not installed. You can install it by typing:
sudo apt-get install git

Linux 下安装

sudo apt-get install git
# 或者
sudo yum install git

Windows 下安装

msysgit 是 Windows 版的 Git,从 http://msysgit.github.io/ 下载,然后按默认选项安装即可。

Mac 下安装

直接从 AppStore 安装 Xcode,Xcode 集成了 Git,不过默认没有安装,你需要运行 Xcode,选择菜单 “Xcode”->“Preferences”,在弹出窗口中找到 “Downloads”,选择 “Command Line Tools”,点 “Install” 就可以完成安装了。

设置

安装完成后,还需要最后一步设置,在命令行输入:

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

基本操作

1. 递归 clone git submodule

git clone –recursive git-remote-url

2. 从暂存区删除文件

git rm –cached

3. git stash pop/apply

pop 就是出栈了,这次记录会被删除

apply 只是从栈里拿出来,并没有删除。可以使用 git stash drop stash@{0} 这样删除。

4. 撤销上一次提交

git commit –amend

5. 比较暂存区和上一版本的差异

git diff –cached

指定特定文件 git diff –cached

6. git log -p -2

-p 展开每次提交的内容差异 -2 显示最近两次的提交,不写 -2, 默认为所有的。

7. rebase 操作的原理及注意事项。

分支冲突

创建分支会在对应的文件系统里创建文件,如果分支分隔是 /,则会创建目录,所以需要注意不能有重复(既是文件又是文件夹)。

a/b ✅ (a 是目录,b 是文件)

a/c ✅

a/b/c ❌ (b 前面已经是文件了,现在想创建同名文件夹,失败)

a/d/e ✅

a/d/f ✅

a/d ❌ (d 前面是文件夹了,现在想创建同名文件,失败)

整理 commit

git rebase -i HEAD~3

整理最近的 3 条 commit

p, pick 使用这条 commit

r, reword 使用这条 commit 但是要修改 commit message,在 :wq 结束后进入编辑模式

e, edit 使用这条 commit 但是要修改 commit messaage // 这里和 reword 使用方法一样, :wq 后要再使用 git commit –amend 进入修改

s, squash 使用这条 commit, 但是要与上一条 commit 合并,自身 commit message 也会合并进去。

f, fixup 与 squash 一样, 但是舍弃 自身 commit message

x, exec 执行命令

Git diff

git diff:是查看 workspace 与 index 的差别的。

git diff –cached:是查看 index 与 local repositorty 的差别的。

git diff HEAD:是查看 workspace 和 local repository 的差别的。(HEAD 指向的是 local repository 中最新提交的版本)

注:git diff 后跟两个参数,如果只写一个参数,表示默认跟 workspace中的代码作比较。git diff 显示的结果为 第二个参数所指的代码在第一个参数所指代码基础上的修改。如,git diff HEAD表示 workspace 在 最新commit的基础上所做的修改。

使用 git 命令时指定私钥

With git 2.10+ (Q3 2016: released Sept. 2d, 2016), you have the possibility to set a config for GIT_SSH_COMMAND (and not just an environment variable as described in Rober Jack Will’s answer)

See commit 3c8ede3 (26 Jun 2016) by Nguyễn Thái Ngọc Duy (pclouds).

(Merged by Junio C Hamano – gitster – in commit dc21164, 19 Jul 2016)

A new configuration variable core.sshCommand has been added to specify what value for GIT_SSH_COMMAND to use per repository.

core.sshCommand:

If this variable is set, git fetch and git push will use the specified command instead of ssh when they need to connect to a remote system.

The command is in the same form as the GIT_SSH_COMMAND environment variable and is overridden when the environment variable is set.

It means the git pull can be:

cd /path/to/my/repo/already/cloned
git config core.sshCommand 'ssh -i private_key_file' 
# later on
git pull

You can even set it for just one command like git clone:

git -c core.sshCommand="ssh -i private_key_file" clone host:repo.git

This is easier than setting a GIT_SSH_COMMAND environment variable, which, on Windows, as noted by Mátyás Kuti-Kreszács, would be

set "GIT_SSH_COMMAND=ssh -i private_key_file"

git log 乱码,无法显示中文

现象

git status 乱码

new file:   "\350\247\243\345\206\263-conda-sslerror-\351\227\256\351\242\230.md"

git log 乱码

<E4><BD><A0><E5><A5><BD><E4><B8><96><E7><95><8C>

git log 乱码解决

git config --global i18n.commitencoding utf-8

git config --global i18n.logoutputencoding utf-8

在 /etc/profile 或者 .zshrc 配置文件中添加

export LESSCHARSET=utf-8

git status 乱码解决

git config --global core.quotepath false

参考

git log 无法正常显示中文,怎么解决?

默认配置

参考:https://blog.gitbutler.com/how-git-core-devs-configure-git/

参考

https://stackoverflow.com/a/38474137