目录

Git

Commands

worktree

Config

Lookup order

Git 使用一系列的配置文件来存储你定义的偏好,它首先会查找 /etc/gitconfig 文件, 该文件含有 对系统上所有用户及他们所拥有的仓库都生效的配置值(译注: gitconfig 是全局配置文件), 如果传递 --system 选项给 git config 命令, Git 会读写这个文件。

接下来 Git 会查找每个用户的 ~/.gitconfig 文件,你能传递 --global 选项让 Git 读写该文件。

最后 Git 会查找由用户定义的各个库中 Git 目录下的配置文件( .git/config ), 该文件中的值只对属主库有效。 以上阐述的三层配置从一般到特殊层层推进,如果定义 的值有冲突,以后面层中定义的为准,例如:在 .git/config/etc/gitconfig 的较量中, .git/config 取得了胜利。虽然你也可以直接手动编辑这些配置文件,但 是运行 git config 命令将会来得简单些1

autocrlf

FlashExpressAssistant 为例, 以前在 Windows 上开发, 换行符为 CRLF, 现在我 需要在 Linux 上对代码进行一些小修复, 我可以把 core.autocrlf 设置成 input, 具体的解释可以参见1里的 core.autocrlf 一节.

git config core.autocrlf input

Services

  • Gitee
  • CODING

GitLab

GitHub

Profile README

Tools

Gitmoji

npm i -g gitmoji-cli

Tree Viewer

Issues

Diffs contain control sequences in magit

Because you set color.ui to always2, 3. Just set it to true, and this is the Git’s default option.

How to

Change the author of one specific commit

Reference4:

  1. Rebase all commits: git rebase -i --root
  2. Change pick to edit for commits you need to edit
  3. git commit --amend --reset-author --no-edit
  4. git rebase --continue
  5. Repeat step 3 and step 4
git commit --amend --author="John Doe <[email protected]>"

More methods see this5.

Extract subdir to new repo

mkdir new-repo
cd new-repo
git init --bare

cd ../big-repo
git subtree split --prefix subdir -b split
git push ../new-repo split:master
git branch -d split

# Push to remote
git remote add ...

原理:

使用 subtree split 命令将一定前缀的提交记录放到新分支上去, 然后将这个分支推送到一 个新初始化的 git 仓库里就行了(没想到还能 push 到本地仓库, 👍)

参考:

Find when a file was deleted

Reference6

# see the changes of a file, works even
# if the file was deleted
git log -- [file_path]

# limit the output of Git log to the
# last commit, i.e. the commit which delete the file
# -1 to see only the last commit
# use 2 to see the last 2 commits etc
git log -1 -- [file_path]

# include stat parameter to see
# some statics, e.g., how many files were
# deleted
git log -1 --stat -- [file_path]

Get current branch name

git rev-parse --abbrev-ref HEAD

Manage large files

Recover a deleted branch

Just create a new branch by using a branch name, a commit-id, or a tag.

git branch <branchname> <start-point>

Recover a deleted file

Reference7

Method1:

  1. git log --diff-filter=D --summary: Get all the commits which have deleted files and the files deleted(use less `/` command to find target commit)

  2. git checkout $commit~1 filename: Restore the deleted file Example: git checkout e4cf499627~1 problem/models.py

    Method2: Use git rev-list to find target commit

Recover a dropped stash

If you have only just popped it and the terminal is still open, you will still have the hash value printed by git stash pop on screen.

Just run git stash apply $stash_hash.8

Rename a branch

Rename current branch:

git branch -m new-name

Rename a target branch:

git branch -m old-name new-name

Revert initial commit

git update-ref -d HEAD

From: https://stackoverflow.com/a/6637891

You just need to delete the branch you are on. You can’t use git branch -D as this has a safety check against doing this. You can use update-ref to do this.

Set editor

git config --global core.editor "vim"

Set proxy

With SSH:

Host gitlab.com
    User git
    ProxyCommand nc -x localhost:1080 %h %p

With HTTP(S):

git config --global https.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080

Specify ssh key for repo

Steps

  1. 修改 SSH 配置文件,加入如下内容
  2. 然后就可以愉快的提交了
Host gitlab.com
    User git
    IdentityFile ~/.ssh/gitkraken_rsa
    IdentitiesOnly yes

Attention

在 WSL 下,记得修改权限,不然可能会出现如下错误:

  • Permissions 0777 for '/home/Hope/.ssh/gitkraken_rsa' are too open
  • Bad owner or permissions on /home/Hope/.ssh/config

Run chmod 600 ~/.ssh/config and chmod 600 ~/.ssh/gitkraken_rsa

Reference

Syncing a fork

REF 保持 fork 之后的项目和上游同步 · staticblog/wiki Wiki · GitHub

git remote add upstream https://github.com/staticblog/staticblog.github.io.git
# Check result with `git remote -v`

git fetch upstream
git checkout master
git merge upstream/master

git push

Misc

.gitkeep

git 无法追踪一个空的文件夹,当用户需要追踪(track)一个空的文件夹的时候,按照惯例,大家会 把一个称为 .gitkeep 的文件放在这些文件夹里。


  1. unix - bash HISTSIZE vs. HISTFILESIZE? - Stack Overflow ↩︎

  2. Magit fails, status contains weird characters - Emacs Stack Exchange ↩︎

  3. Magit User Manual: Diffs contain control sequences ↩︎

  4. How to change the commit author for one specific commit? - Stack Overflow ↩︎

  5. How can I change the author (name / email) of a commit? | Learn Version Contr… ↩︎

  6. Git - Tutorial 29. See which commit deleted a file ↩︎

  7. Find and restore a deleted file in a Git repository - Stack Overflow ↩︎

  8. How to recover a dropped stash in Git? - Stack Overflow ↩︎