Git

Git #

Git Cheetsheet #

1# clone一个已创建的仓库
2git clone ssh://[email protected]/repo.git
1# 创建一个新的本地仓库
2git init
1# 显示工作目录下全部已修改的文件
2git status
1# 显示工作目录中已修改但尚未暂存的文件与当前 HEAD 的差异
2git diff
3# 显示已暂存但尚未提交的更改
4git diff --staged
1# 把当前所有修改添加到暂存区
2git add .
1# 基于当前暂存区中的内容提交一个commit
2git commit -m '<commit message>'
 1# 修改最近一次提交
 2
 3# 修改提交信息(commit message)
 4# 这会打开默认编辑器
 5git commit --amend
 6# 修改提交信息(不打开编辑器)
 7git commit --amend -m "New commit message"
 8
 9# 添加遗漏的文件或修改
10git add forgotten_file
11git commit --amend
12# --reset-author 选项会将提交的作者信息重置为当前的git用户配置(user.name和user.email)
13git commit --amend --reset-author
1# 从最新提交开始显示所有的提交记录
2git log
1# 显示指定文件的所有修改
2git log -p <file>
1# 谁,在什么时间,修改了文件的什么内容
2git blame <file>
1# 显示所有分支(包含远程分支)
2git branch -av
3# 显示本地分支
4git branch -v
1# 切换当前分支
2git checkout <branch> 
1# 基于当前分支创建新分支
2git branch <new-branch>
3# 基于远程分支创建新分支
4git checkout --track <remotes/origin/branch>
1# 删除本地分支
2git branch -d <branch>
1# 给当前的提交打tag
2git tag <tag-name>
1# 从tag创建分支
2git switch -c <new-branch> <tag-name>
3# 或
4git checkout -b <new-branch> <tag-name>
1# 列出当前配置的所有remote
2git remote -v
3# 显示某个remote的详细信息
4git remote show <remote>
5# 添加新的remote
6git remote add <shortname> <url>
 1# 下载remote的所有改动到本地 不会自动合并到当前
 2git fetch <remote>
 3# 下载remote的所有改动到本地 自动合并到当前
 4git pull <remote> <branch>
 5# 将本地版本发布到remote
 6git push <remote> <branch>
 7# 删除remote分支
 8git branch -dr <remote/branch>
 9# 发布tag
10git push --tags
1# merge <branch> into current HEAD
2git merge <branch>
3# rebase current HEAD onto <branch>.
4# Don‘t rebase published commits!
5git rebase <branch>
6# abort a rebase
7git rebase --abort
8# continue a rebase after resolving conflicts
9git rebase --continue
1# use configured merge tool to solve conflicts
2git mergetool
3# use editor to manually solve conflicts and (after resolving) mark file as resolved
4git add <resolved-file>
5git rm <resolved-file>
 1# discard all local changes in your working directory
 2git reset --hard HEAD
 3# discard local changes in a specific file
 4git checkout HEAD <file>
 5# revert a commit (by producing a new commit with contrary changes)
 6git revert <commit>
 7# reset HEAD pointer to a previous commit
 8# and discard all changes since then
 9git reset --hard <commit>
10# reset HEAD pointer to a previous commit
11# and preserve all changes as unstaged changes
12git reset <commit>
13# reset HEAD pointer to a previous commit
14# and preserve uncommitted local changes

pre-commit #

pre-commit - 一个用于管理和维护多语言预提交钩子的框架。

Config #

修改全局配置:

1git config --global --edit

上面的命令实际上就是编辑~/.gitconfig文件:

1[user]
2    name = xxx
3    email = [email protected]
4[core]
5    editor = vi

Conditional includes #

https://git-scm.com/docs/git-config#_conditional_includes

~/.gitconfig:

 1[user]
 2name = xxx
 3email = [email protected]
 4[core]
 5editor = vi
 6
 7# 包含的配置对~/works/目录及子目录下所有仓库生效
 8[includeIf "gitdir:~/works/"]
 9path = ~/.gitconfig.works
10
11# 包含的配置对~/Groups/目录及子目录下所有仓库生效, 注意这里Groups包含大写字母
12# gitdir/i意味着目录名匹配将忽略大小写
13[includeIf "gitdir/i:~/Groups/"]
14path = ~/.gitconfig.groups

~/.gitconfig.works:

1[user]
2name = yyy
3email = [email protected]

~/.gitconfig.groups:

1[user]
2name = zzz
3email = [email protected]
© 2025 青蛙小白 | 总访问量 | 总访客数