Skip to content
On this page

Git

记录 Git 使用记录。

Flight rules for Git

配置 Git

中文正常显示

默认情况下,git 会将中文显示为八进制的字符编码,调整为中文显示

git config --global core.quotepath false
git config --global core.quotepath false

分离公司及个人的 gitconfig

让 git 公私分明

~/.gitconfig 末尾添加下面两行

[includeIf "gitdir:**/company/**"]
    path = ~/.gitconfig.company
[includeIf "gitdir:**/company/**"]
    path = ~/.gitconfig.company

表示执行 git 命令时,若目录匹配到 /company/ ,则加载 ~/.gitconfig.company 来覆盖 ~/.gitconfig 中的对应配置。

比如公司的私有仓库,通常需要使用公司分配的用户名和邮箱,那么只需要添加如下内容到 ~/.gitconfig.company 即可。

[user]
    name = yourname
    email = yourname@company.com
[user]
    name = yourname
    email = yourname@company.com

Bash 显示 Git 信息

Bash 显示git分支

将以下内容写入 ~/.bashrc

bash
function git_branch {
    ref=$(git symbolic-ref HEAD 2> /dev/null) || return;
    echo "("${ref#refs/heads/}") ";
}

PS1="[\u@\h \w][[\e[1;31m\]\$(git_branch)\[\e[0m\]\\$ "
function git_branch {
    ref=$(git symbolic-ref HEAD 2> /dev/null) || return;
    echo "("${ref#refs/heads/}") ";
}

PS1="[\u@\h \w][[\e[1;31m\]\$(git_branch)\[\e[0m\]\\$ "

Proxy

设置 Proxy ,参见 Configure Git to use a proxy

sh
# Set http proxy for all
$ git config --global http.proxy socks5://server:port

# Set http proxy for https://github.com
$ git config --global http.https://github.com.proxy socks5://server:port

# Unset http proxy for https://github.com
$ git config --global --unset http.https://github.com.proxy
# Set http proxy for all
$ git config --global http.proxy socks5://server:port

# Set http proxy for https://github.com
$ git config --global http.https://github.com.proxy socks5://server:port

# Unset http proxy for https://github.com
$ git config --global --unset http.https://github.com.proxy

Git 命令记录

仅获取仓库中某个文件/目录

参见 How to sparsely checkout only one single file from a git repository?

sh
git clone -n git://path/to/the_repo.git --depth 1
cd the_repo
git checkout HEAD name_of_file_or_directory
git clone -n git://path/to/the_repo.git --depth 1
cd the_repo
git checkout HEAD name_of_file_or_directory

Track remote branch

Track a new remote branch created on GitHub

创建一个本地的 branch ,以追踪某个 remote branch

sh
git fetch <remote>
git branch --track|-t <local_branch> <remote>/<remote_branch>
git checkout --branch|-b <local_branch> <remote>/<remote_branch>
git checkout --track|-t <remote>/<remote_branch>
git fetch <remote>
git branch --track|-t <local_branch> <remote>/<remote_branch>
git checkout --branch|-b <local_branch> <remote>/<remote_branch>
git checkout --track|-t <remote>/<remote_branch>

示例

sh
$ git fetch upstream

$ git branch -t upstream/master upstream/master
Branch 'upstream/master' set up to track remote branch 'master' from 'upstream'.

$ git checkout -b upstream/develop upstream/develop
Branch 'upstream/develop' set up to track remote branch 'develop' from 'upstream'.
Switched to a new branch 'upstream/develop'
$ git fetch upstream

$ git branch -t upstream/master upstream/master
Branch 'upstream/master' set up to track remote branch 'master' from 'upstream'.

$ git checkout -b upstream/develop upstream/develop
Branch 'upstream/develop' set up to track remote branch 'develop' from 'upstream'.
Switched to a new branch 'upstream/develop'

Create and delete remote branch

在 remote 创建一个新的 branch 。 首先在本地创建一个 branch ,然后将该 branch push 到 remote 。

How do you create a remote Git branch?

sh
# Create a local branch
$ git checkout -b develop

# Do some changes and commit

# Push local branch to remote origin branch develop
$ git push -u origin develop
# Create a local branch
$ git checkout -b develop

# Do some changes and commit

# Push local branch to remote origin branch develop
$ git push -u origin develop

删除 remote 上的 branch 。

How do I delete a Git branch both locally and remotely?

sh
git push --delete <remote name>

# Delete branch fix/issue#1 on remote origin
$ git push --delete origin fix/issue#1
git push --delete <remote name>

# Delete branch fix/issue#1 on remote origin
$ git push --delete origin fix/issue#1

Change commit user and email

注意这种方式只能是在该 commit 没有推送到 remote 之前可用。

sh
git commit --amend --author='myname <myemail@abcd.com>'
git commit --amend --author='myname <myemail@abcd.com>'

查看文件修改

想查看从某一时间到现在,每一次的修改

sh
git log --since=<yyyy-mm-dd> -p <file>
git log --since=<yyyy-mm-dd> -p <file>

想查看从某一 commit 到现在,文件总的修改情况

sh
git diff <commit>..HEAD <file>
git diff <commit>..HEAD <file>

submodule

Remove submodule

Copy from https://gist.github.com/myusuf3/7f645819ded92bda6677#gistcomment-2986654

sh
git rm --cached <path_to_submodule>
rm -rf <path_to_submodule>
git commit -m "Removed <path_to_submodule> submodule "
rm -rf .git/modules/<path_to_submodule>
git config -f .gitmodules --remove-section submodule.<path_to_submodule>
git config -f .git/config --remove-section submodule.<path_to_submodule>
git rm --cached <path_to_submodule>
rm -rf <path_to_submodule>
git commit -m "Removed <path_to_submodule> submodule "
rm -rf .git/modules/<path_to_submodule>
git config -f .gitmodules --remove-section submodule.<path_to_submodule>
git config -f .git/config --remove-section submodule.<path_to_submodule>