Git

这里记录我容易忘记的Git命令

9. 按目录配置用户名和邮箱信息

可能你会有这种需求,在某一个目录下放置多个项目,所有项目共享一个用户名和密码。在其他项目,用默认全局的用户名和密码,怎么做呢?includeIf 可能帮的上忙。先在全局的 ~/.gitconfig 文件中加入:

[includeIf "gitdir:~/Projects/another-project-path/"]
    path = .gitconfig-another-project-name

再在 ~/.gitconfig-another-project-name 中为 another-project-folder 配置特别的用户名和密码即可。

参考:https://stackoverflow.com/questions/21307793/set-git-config-values-for-all-child-folders/37167110

8. 寻找丢失的commit

checkout或者rebase后,可能会丢失之前的commit,使用git reflog可列出所有提交的commit。

$ git reflog
$ git checkout -b newbranch commit-id

7. long path name on windows

$ git config --system core.longpaths true

6. git sha1

$ echo hello | git hash-object --stdin
ce013625030ba8dba906f756967f9e9ca394464a

$ printf 'blob 6\0hello\n' > test.txt

or

$ echo -en "blob 6\0hello\n" > test.txt

$ openssl sha1 test.txt
SHA1(test.txt)= ce013625030ba8dba906f756967f9e9ca394464a

5. git log

$ git log v2.5..        # commits since (not reachable from) v2.5
$ git log test..master  # commits reachable from master but not test
$ git log master..test  # commits reachable from test but not master
$ git log master...test # commits reachable from either test or
                        #    master, but not both
$ git log --since="2 weeks ago" # commits from the last 2 weeks
$ git log Makefile      # commits that modify Makefile
$ git log fs/           # commits that modify any file under fs/
$ git log -S'foo()'     # commits that add or remove any file data
                        # matching the string 'foo()'
$ git log --no-merges   # dont show merge commits

$ git log --pretty=format:'%h was %an, %ar, message: %s'

$ git log --pretty=format:'%h : %s' --graph

# sort
$ git log --pretty=format:'%h : %s' --topo-order --graph
$ git log --pretty=format:'%h : %s' --date-order --graph

4. 问责

$ git blame filename
$ git blame -L 160,+10 filename

3. 克隆一个裸仓库

$ git clone --bare ~/proj proj.git

2. 更改author信息

$ git commit --amend --author "Author <zddhub@gmail.com>"

1. Git 配置

# 更改你的编辑器
$ git config --global core.editor vim
# 添加别名
$ git config --global alias.last 'cat-file commit HEAD'
# 添加颜色
$ git config color.branch auto
$ git config color.diff auto
$ git config color.interactive auto
$ git config color.status auto
Or
$ git config color.ui true
# 日志格式
$ git config format.pretty oneline
# 提交者信息
$ git config --global user.name zdd
$ git config --global user.email zddhub@gmail.com

如果你喜欢这篇文章,欢迎赞赏作者以示鼓励