青蛙小白

Git

Git Cheetsheet

# clone一个已创建的仓库
git clone ssh://[email protected]/repo.git
# 创建一个新的本地仓库
git init
# 显示工作目录下全部已修改的文件
git status
# 显示工作目录中已修改但尚未暂存的文件与当前 HEAD 的差异
git diff
# 显示已暂存但尚未提交的更改
git diff --staged
# 把当前所有修改添加到暂存区
git add .
# 基于当前暂存区中的内容提交一个commit
git commit -m '<commit message>'
# 修改最近一次提交

# 修改提交信息(commit message)
# 这会打开默认编辑器
git commit --amend
# 修改提交信息(不打开编辑器)
git commit --amend -m "New commit message"
# 修改提交时间为当前时间
git commit --amend --date="now" --no-edit


# 添加遗漏的文件或修改
git add forgotten_file
git commit --amend
# --reset-author 选项会将提交的作者信息重置为当前的git用户配置(user.name和user.email)
git commit --amend --reset-author
# 修改最近一次提交(HEAD)的用户名和邮箱
git commit --amend --author="新用户名 <新邮箱地址>"
# 从最新提交开始显示所有的提交记录
git log
# 显示指定文件的所有修改
git log -p <file>
# 谁,在什么时间,修改了文件的什么内容
git blame <file>
# 显示所有分支(包含远程分支)
git branch -av
# 显示本地分支
git branch -v
# 切换当前分支
git checkout <branch> 
# 基于当前分支创建新分支
git branch <new-branch>
# 基于远程分支创建新分支
git checkout --track <remotes/origin/branch>
# 删除本地分支
git branch -d <branch>
# 给当前的提交打tag
git tag <tag-name>
# 从tag创建分支
git switch -c <new-branch> <tag-name>
# 或
git checkout -b <new-branch> <tag-name>
# 列出当前配置的所有remote
git remote -v
# 显示某个remote的详细信息
git remote show <remote>
# 添加新的remote
git remote add <shortname> <url>
# 下载remote的所有改动到本地 不会自动合并到当前
git fetch <remote>
# 下载remote的所有改动到本地 自动合并到当前
git pull <remote> <branch>
# 将本地版本发布到remote
git push <remote> <branch>
# 删除remote分支
git branch -dr <remote/branch>
# 发布tag
git push --tags
# merge <branch> into current HEAD
git merge <branch>
# rebase current HEAD onto <branch>.
# Don‘t rebase published commits!
git rebase <branch>
# abort a rebase
git rebase --abort
# continue a rebase after resolving conflicts
git rebase --continue
# use configured merge tool to solve conflicts
git mergetool
# use editor to manually solve conflicts and (after resolving) mark file as resolved
git add <resolved-file>
git rm <resolved-file>
# discard all local changes in your working directory
git reset --hard HEAD
# discard local changes in a specific file
git checkout HEAD <file>
# revert a commit (by producing a new commit with contrary changes)
git revert <commit>
# reset HEAD pointer to a previous commit
# and discard all changes since then
git reset --hard <commit>
# reset HEAD pointer to a previous commit
# and preserve all changes as unstaged changes
git reset <commit>
# reset HEAD pointer to a previous commit
# and preserve uncommitted local changes

pre-commit

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

Config

修改全局配置:

git config --global --edit

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

[user]
    name = xxx
    email = [email protected]
[core]
    editor = vi

Conditional includes

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

~/.gitconfig:

[user]
name = xxx
email = [email protected]
[core]
editor = vi

# 包含的配置对~/works/目录及子目录下所有仓库生效
[includeIf "gitdir:~/works/"]
path = ~/.gitconfig.works

# 包含的配置对~/Groups/目录及子目录下所有仓库生效, 注意这里Groups包含大写字母
# gitdir/i意味着目录名匹配将忽略大小写
[includeIf "gitdir/i:~/Groups/"]
path = ~/.gitconfig.groups

~/.gitconfig.works:

[user]
name = yyy
email = [email protected]

~/.gitconfig.groups:

[user]
name = zzz
email = [email protected]
评论