阅读更多
1 基本概念

1.1 工作区(Workspace)
代表你正在工作的那个文件集,也就是git管理的所有文件的集合
下文用Workspace
来表示工作区
1.2 版本库(Repository)
工作区有一个隐藏目录.git
,这个不算工作区,而是Git的版本库
Git的版本库里存了很多东西,其中最重要的就是称为stage
(或者叫index
)的暂存区,还有Git为我们自动创建的第一个分支master
,以及指向master
的一个指针叫HEAD
下文用Index
来表示暂存区,用HEAD
表示当前分支的最新提交,用Repository
表示提交区
2 配置
1 2 3 4 5 6 7 8 9 10 11 12
| git config --list
git config -e [--global]
git config [--global] user.name "[name]" git config [--global] user.email "[email address]"
git config [--global] --unset http.proxy
|
3 增加/删除文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| git add [file1] [file2] ...
git add [dir]
git add .
git add -u
git add -p
git rm [file1] [file2] ...
git rm --cached [file]
git rm -r --cached .
git ls-files
git mv [file-original] [file-renamed]
|
3.1 彻底删除文件
如何查找仓库记录中的大文件:
1
| git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"
|
步骤1:通过filter-branch
来重写这些大文件涉及到的所有提交(重写历史记录,非常危险的操作):
1 2
| git filter-branch -f --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch <your-file-name>' --tag-name-filter cat -- --all
|
步骤2:将本地的分支全部推送到远程仓库。如果不加all的话,只推送了默认分支或者指定分支,如果远程仓库的其他分支还包含这个大文件的话,那么它仍然存在于仓库中
1
| git push origin --force --all
|
步骤3:删除本地仓库中的相关记录
1 2 3 4
| rm -rf .git/refs/original/ git reflog expire --expire=now --all git gc --prune=now git gc --aggressive --prune=now
|
4 提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| git commit -m [message]
git commit [file1] [file2] ... -m [message]
git commit -a
git commit -v
git commit --amend -m [message]
git commit --amend [file1] [file2] ...
|
5 撤销
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| git checkout [file] git checkout -- [file]
git checkout [commit] [file]
git checkout . git checkout -- .
git reset [file]
git reset --hard
git reset [commit]
git reset --hard [commit]
git reset --keep [commit]
git revert [commit]
git stash -m [message]
git stash pop
git stash list
git stash apply [id]
git stash drop [id]
git stash show [id]
git stash show [id] -p
git diff stash@{[id]}
git stash clear
git restore [file]
git restore --staged [file]
|
6 分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| git branch
git branch -vv
git branch -r
git branch -a
git branch [branch-name]
git checkout -b [branch]
git checkout -b [branch_local] origin/[branch_remote]
git branch [branch] [commit]
git branch --track [branch] [remote-branch]
git checkout [branch-name]
git checkout -
git branch --set-upstream [branch] [remote-branch]
git merge [branch]
git rebase [branch]
git rebase --continue
git rebase --abort
git rebase -i [startcommit] [endcommit]
git rebase -i [startcommit]
git rebase -i --root
git cherry-pick [commit1] [commit2] ...
git branch -d [branch-name]
git push origin --delete [branch-name] git branch -dr [remote/branch]
git branch -m [oldbranch] [newbranch] git branch -M [oldbranch] [newbranch]
|
7 标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| git tag
git tag [tag]
git tag [tag] [commit]
git tag -d [tag]
git push origin :refs/tags/[tagName]
git show [tag]
git push [remote] [tag]
git push [remote] --tags
git checkout -b [branch] [tag]
|
8 查看信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| git status
git log
git log --stat
git log -S [keyword]
git log [tag] HEAD --pretty=format:%s
git log [tag] HEAD --grep feature
git log --follow [file]
git log [file]
git log -p [file]
git log -5 --pretty --oneline
git shortlog -sn
git blame [file]
git diff --stat
git diff
git diff --cached [file]
git diff HEAD
git diff [first-branch]...[second-branch]
git diff --shortstat "@{0 day ago}"
git show [commit]
git show [commit] --stat
git show --name-only [commit]
git show [commit]:[filename]
git whatchanged
git whatchanged –stat
git whatchanged [file]
git reflog
|
9 克隆
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git clone https://github.com/xxx/yyy.git
git clone git@github.com:xxx/yyy.git
git clone https://github.com/xxx/yyy.git --depth 1 git clone -b <branch_name> https://github.com/xxx/yyy.git --depth 1
git fetch --unshallow
git remote set-branches origin '<需要获取的分支名>' git fetch --depth 1 origin '<需要获取的分支名>'
|
10 远程同步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| git fetch [remote]
git fetch [remote] [branch]
git fetch [remote] pull/29048/head
git fetch [remote] pull/29048/head:pull_request_29048
git remote -v
git remote show [remote]
git remote add [shortname] [url]
git pull [remote] [branch]
git push [remote] [branch]
git push [remote] --force
git push [remote] --all
|
11 插件
git-extra
12 发布
13 Alias
1
| git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cs, %cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
|
14 .gitignore
基础规则
- 空白行,不匹配任何文件,仅增加可读性
- 规则以
#
开头,表示注释
- 规则
行尾的空格
,会被忽略,除非用\
进行转义
- 规则以
!
开头,表示反转该规则(.gitignore
文件默认的语义是忽略,反转后表示包含)
- 规则以
/
结尾,表示匹配目录
以及该目录下的一切
- 规则以不包含
/
,表示匹配文件
或目录
*
匹配多个字符,不包括/
?
匹配单个字符,不包括/
[]
匹配指定的多个字符
- 规则以
/
开头,表示以根目录
开始,即匹配绝对路径
。例如/*.c
匹配cat-file.c
,但不匹配mozilla-sha1/sha1.c
**/
开头,表示匹配所有目录
。例如**/foo
匹配foo
目录或文件。**/foo
与foo
的作用是一样的
/**
开头,表示匹配内部的一切。例如abc/**
匹配abc
目录下的所有文件。abc/**
与abc/
的作用是一样的
/**/
表示匹配0
个或多
个目录
。例如a/**/b
匹配a/b
、a/x/b
、a/x/y/b
15 git-lfs
Git Large File Storage
16 git-worktree
git-worktree
17 Tips
17.1 修改diff工具
项目地址:github-icdiff
安装:
1 2 3 4 5 6 7
| pip3 install git+https://github.com/jeffkaufman/icdiff.git
git difftool --extcmd icdiff
git config --global icdiff.options '--highlight --line-numbers'
|
使用:
17.2 中文显示为8进制形式的问题
在Windows中,git bash打印的中文可能表示成\+三个数字
的形式,即八进制表示
通过如下命令可以解决该问题
1
| git config --global core.quotepath false
|
17.3 copilot
copilot
18 参考