Git笔记

字数 1481 · 2018-04-09

#git

初始化

基本配置

1
2
# 解决命令行中不显示中文的问题
git config --global core.quotepath false
1
2
3
4
5
# 显示配置
git config -l
# 配置
git config user.name "username"
git config user.email "username@example.com"

提交代码

1
2
3
4
# 初始化本地仓库
$ cd myproject
$ git init
$ git remote add origin git@gitserver:/home/git/project.git
1
2
3
4
# 提交并推到远端
$ git add .
$ git commit -m 'initial commit'
$ git push origin master

设置Git服务器

配置用户

1
2
3
4
5
$ sudo adduser git
$ su git
$ cd
$ mkdir .ssh && chmod 700 .ssh
$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys

添加 authorized_keys

在自己的机器上获取公钥

1
2
3
4
5
6
7
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCB007n/ww+ouN4gSLKssMxXnBOvf9LGt4L
ojG6rs6hPB09j9R/T17/x4lhJA0F3FR1rP6kYBRsWj2aThGw6HXLm9/5zytK6Ztg3RPKK+4k
Yjh6541NYsnEAZuXz0jTTyAUfrtU3Z5E003C4oxOj6H0rfIF1kKI9MAQLMdpGW1GYEIgS9Ez
Sdfd8AcCIicTDWbqLAcU4UpkaX8KyGlLwsNuuGztobF8m72ALC/nLF6JLtPofwFBlgc+myiv
O7TCUSBdLQlgMVOFq1I2uPWQOkOWQAHukEOmfjy2jctxSDBQ220ymjaNsHT4kgtZg2AYYgPq
dAv8JggJICUvax2T9va5 gsg-keypair

将输出复制到服务器上的 .ssh/authorized_keys 文件中。

生成公钥

1
ssh-keygen -t rsa -b 4096

创建仓库

1
2
3
4
$ cd /home/git
$ mkdir project.git
$ cd project.git
$ git init --bare

其他

查看最后修改

1
git blame

如果有文件漏提交了,可以这么操作

1
2
git add 要提交的文件
git commit -m "新的message" --amend
1
2
# 合并一个指定的commit
git cherry-pick <commit hash>
1
2
3
4
5
# 删除untracked的数据
# -n dry run - 演习
# -f force - 强制删除,默认不加-f会警告且不删除
# -d directory - 同时删除目录,默认不删除目录
git clean
1
2
3
4
# 显示代码增删信息
git log --stat
# 显示指定路径的具体更改
git log --full-diff -p <path>

合并 commit:

1
2
# 合并 commit
git rebase -i <hash> [end]

接下来会进入文本编辑:

  1 pick 656230bd commit1
  2 pick 83a8609d commit2
  3
  4 # Rebase dcc948d8..83a8609d onto dcc948d8 (2 commands)
  5 #
  6 # Commands:
  7 # p, pick <commit> = use commit
  8 # r, reword <commit> = use commit, but edit the commit message
  9 # e, edit <commit> = use commit, but stop for amending
 10 # s, squash <commit> = use commit, but meld into previous commit
 11 # f, fixup <commit> = like "squash", but discard this commit's log message
 12 # x, exec <command> = run command (the rest of the line) using shell
 13 # d, drop <commit> = remove commit
 14 # l, label <label> = label current HEAD with a name
 15 # t, reset <label> = reset HEAD to a label
 16 # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
 17 # .       create a merge commit using the original merge commit's
 18 # .       message (or the oneline, if no original merge commit was
 19 # .       specified). Use -c <commit> to reword the commit message.
 20 #
 21 # These lines can be re-ordered; they are executed from top to bottom.`

Tag

1
2
3
4
5
6
7
8
9
10
11
12
13
# 添加一个 tag
git tag -a "tag-name" <ref> -m "message"
# 删除一个 tag
git tag -d <tag-name>
#重命名一个 tag
git tag "new-tag-name" <old-tag-ref>
git tag -d <old-tag-ref>
# 推送 tag
git push —tags
# 拉取 tag
git fetch --all --tags
# 回到一个tag版本
git checkout <tag>

Git 的一些符号

@HEAD 的别名。
~^ 的意义相同。

参考资料

服务器设置 - git-scm.com

Submodule

1
2
3
4
5
6
# 添加子模块
git submodule add https://github.com/chaconinc/DbConnector DbConnector
#
git submodule update --init
# 改变地址
git config submodule.DbConnector.url PRIVATE_URL
1
2
# 从 origin 更新
git submodule update --remote
1
2
# copy the new URL to your local config
git submodule sync --recursive

Tips

还原

1
2
3
git checkout -- <path>
git reset HEAD <path>
git clean -df

符号

@ is an alias for HEAD. And since ~ and ^ are the same.

比较

1
2
3
4
5
6
7
8
# 比较 working tree 和 staged 的不同
git diff 
# 比较暂存区和本地仓库的不同
git diff --cached
# 比较本地仓库和远端仓库的不同
git diff master origin/master
# 具体修改
git diff -p <path>

Alias

1
2
3
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch

支持 utf-8

1
git config --global core.quotepath false  

提交

1
2
# push 本地的 dev 到远端的 test
git push upstream dev:test

pull vs fetch

git pull does a git fetch followed by a git merge.

1
2
3
# Merge remote branch origin/foo into local branch foo,
# without having to checkout foo first:
git fetch origin foo:foo

搜索

search all of Git history for a string

1
git log -Spassword

清理分支

1
2
3
4
# 列出已合并的分支
git branch --merge  
# 删除
git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

忽略文件

忽略已提交的文件:

1
git update-index --skip-worktree filepath

查看已忽略的文件:

1
git ls-files -v . | grep "^S" 

还原忽略的文件:

1
git update-index --no-skip-worktree skip.txt

还原 stash

1
git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk --grep=WIP
1
git stash apply <stash>

rebase upstream

1
2
3
git fetch upstream
git rebase upstream/master
git push origin master --force

Commit message

1
type(scope): message

type

  • feat: 新功能
  • fix: 修补BUG
  • docs: 文档
  • style: 格式
  • refactor: 重构
  • test: 测试
  • chore: 构建过长或辅助工具变动

chore

  1. a task that you do regularly
  2. an unpleasant or boring task

contributors

1
2
# 列出所有贡献者,按 commit 数量排列
git shortlog -sn --no-merges
1
2
3
# 遍历仓库,输出全部 commit 数量
# Command + D when "(reading log message from standard input)"
for d in *; do if [ -d "$d/.git" ]; then git -C $d shortlog -ns | grep <contributor> | xargs echo $d;fi; done | awk '{s+=$2; print $1,$2}END{print "total",s}'

Tools

Tig - text-mode interface for Git