阅读更多
1 Basic Concepts
1.1 Workspace
代表你正在工作的那个文件集,也就是git管理的所有文件的集合
下文用Workspace
来表示工作区
1.2 Repository
工作区有一个隐藏目录.git
,这个不算工作区,而是Git的版本库
Git的版本库里存了很多东西,其中最重要的就是称为stage
(或者叫index
)的暂存区 ,还有Git为我们自动创建的第一个分支master
,以及指向master
的一个指针叫HEAD
下文用Index
来表示暂存区,用HEAD
表示当前分支的最新提交,用Repository
表示提交区
2 Configuration
配置文件:
~/.gitconfig
:对应于global
.git/config
:对应于非global
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
2.1 Alias
1 2 3 4 5 6 7 8 git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" 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" git config --global alias.lg "log --color --graph --pretty='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cd, %cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=format:'%Y-%m-%d %H:%M:%S'"
2.2 Modify Default Editor
1 2 git config --global core.editor "vim" git config --global core.editor "nvim"
项目地址: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'
使用:
3 Adding/Deleting Files
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 Permanently Deleting Files
如何查找仓库记录中的大文件:
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 Submit
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 Undo
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 git checkout -- . git checkout -- [file] git checkout [branch] -- [file] git checkout [commit] -- [file] 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 Branch
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 Tag
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 Log
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 78 79 80 81 82 83 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 log --date =format:"%Y-%m-%d %H:%M:%S" 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] -- [filename] git show [commit] --stat git show --name-only [commit] git show [commit]:[filename] git whatchanged git whatchanged –stat git whatchanged [file] git reflog
9 Clone
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 '[branch_name]' git fetch --depth 1 origin '[branch_name]'
10 Sync
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 git fetch [remote] git fetch [remote] [branch] git fetch [remote] [commit] 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 Submodule
Commit hash of each submodule is stored as normal git object. You can check it by git submodule status
1 2 3 4 5 6 7 8 9 10 11 12 13 git submodule add [repository_url] [path/to/submodule] git submodule add -b [branch_name] [repository_url] [path/to/submodule] git submodule update --init --recursive git submodule sync --recursive git submodule sync --recursive [path/to/submodule] git submodule status
12 Plugin
git-extra
13 Publish
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 gist
Gists allow developers to share code or text snippets with others, making it easy to collaborate or seek help with specific programming tasks.
Gist
18 copilot
copilot
19 Tips
19.1 Install Latest Version
19.1.1 Centos
1 2 3 4 5 6 sudo yum -y remove git sudo yum -y remove git-* sudo yum -y install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm sudo yum install git
在Windows中,git bash打印的中文可能表示成\+三个数字
的形式,即八进制表示
通过如下命令可以解决该问题
1 git config --global core.quotepath false
19.3 Proxy
19.3.1 SSH Protocol
Edit ~/.ssh/config
1 2 3 4 5 6 7 Host github.com # HostName github.com Hostname ssh.github.com Port 443 User git # Go through socks5 proxy, like Shadowsocks ProxyCommand nc -v -x 127.0.0.1:7890 %h %p
19.3.2 HTTP Protocol
1 2 git config --global http.proxy "http://127.0.0.1:7890" git config --global https.proxy "https://127.0.0.1:7890"
19.4 DNS
19.5 Access Tokens
Settings
-> Developer Settings
-> Personal access tokens
You can use token as your password to push to remote repository
20 Reference