官方手册
config 配置文件
/etc/gitconfig全局配置~/.gitconfig或~/.config/git/config用户配置 ( windows$HOME\.gitconfig==C:\Users\$USER\.gitconfig).git/config工程配置- 局部配置优先 like css
git config --global user.name "Otao"
git config --global user.email "otao.normal@gmail.com"
git config --global core.editor "'C:\Users\enda\AppData\Local\Programs\Microsoft VS Code\Code.exe' --wait"
git config --list #列出当前用户配置
git config --list --global
git config --show-origin [config] # 显示配置源于何处
help 帮助
git help [verb]
git [verb] -h # or --help
流程
- 仓库(
repository)git init- 克隆现有项目
git clone (https|git|ssh)://host/proj [localname](在当前目录下建立proj或[localname]文件夹)
- 开始或停止跟踪(
track)文件git add [file|dir]
- 暂存(
stage)或提交(commit)git commit -m 'comment'
- 忽略指定的文件和文件模式
- 撤销错误操作
- 浏览项目的
历史版本以及不同提交(commits)之间的差异 - 向远程仓库推送(
push) - 如何从远程仓库拉取(
pull)文件
文件状态 (untracked | tracked)
- 未跟踪 untracked =>
git add file=> staged - 未修改 unmodified =>
edit file=> modifiedgit rm file=> untracked
- 已修改 modified =>
git stage=> staged - 已暂存 staged =>
git commit=> unmodified
git status #查看当前状态
git status -s # --short 查看状态简单模式
12 file # 1->暂存区 2->工作区
M modified-unstaged.txt
MM staged-modified.txt
A staged.txt
M modified-staged.txt
?? untrached.txt
# commited
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
# uncommited
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
# unstaged
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
# untrached
Untracked files:
(use "git add <file>..." to include in what will be committed)
# unmerged
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: index.html
忽略文件 .gitignore
cat .gitignore
# 注释行
# glob模式匹配 默认递归
# /开头 防止递归
# 结尾/ 指定目录
# !开头 取反 不忽略被忽略文件中匹配文件
*.[oa]
*~
所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。 星号(*)匹配零个或多个任意字符;[abc] 匹配任何一个列在方括号中的字符 (这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c); 问号(?)只匹配一个任意字符;如果在方括号中使用短划线分隔两个字符, 表示所有在这两个字符范围内的都可以匹配(比如 [0-9] 表示匹配所有 0 到 9 的数字)。 使用两个星号(**)表示匹配任意中间目录,比如 a/**/z 可以匹配 a/z 、 a/b/z 或 a/b/c/z 等。
查看变化
git diff # 暂存后修改部分
git diff --staged # --cached 暂存后未提交部分
提交修改
git status # check unstaged files 检查未暂存文件
git commit # commit staged files 提交暂存文件
# 打开编辑器编辑提交说明
git commit -m "Story 182: Fix benchmarks for speed" # 按说明参数直接提交
删除文件
git rm file # 从暂存中删除文件 并删除工作区文件
git rm -f file # 从仓库中删除文件
git rm --cached file # 仅删除仓库中文件,保留工作区文件,当忽略文件设定失误 包含非必需文件
移动文件
由于git无法自动追踪文件名变化,需要显式移动
git mv a.txt dir\a.txt
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
查看提交历史
git log # 降序显示所有提交历史
git log -p -2 # --patch 降序显示所有提交历史与差异 限制2项
git log --stat # 简要模式
git log --pretty=[oneline|short|full|fuller] # 预格式化输出
git log --pretty=format:"%h - %an, %ar : %s" # 自定义格式化输出
# 当 oneline 或 format 与另一个 log 选项 --graph 结合使用时尤其有用。 这个选项添加了一些 ASCII 字符串来形象地展示你的分支、合并历史
git log --since=2.weeks # 最后2周 --until 直到2周前
git log --since=2008-01-15
git remote 远程仓库
git remote # 查看当前仓库 (简写 shortname)
git remote -v # 查看完整仓库链接
git remote add pb https://github.com/paulboone/ticgit # 添加仓库 `pb`
git fetch pb # 拉取 下载
git pull <remote> <branch>
git pull origin master
只有当你有所克隆服务器的写入权限,并且之前没有人推送过时,这条命令才能生效。 当你和其他人在同一时间克隆,他们先推送到上游然后你再推送到上游,你的推送就会毫无疑问地被拒绝。 你必须先抓取他们的工作并将其合并进你的工作后才能推送。
git remote show origin # 查看当前分支
git branch 分支
git branch # 查看分支
git branch -v # 查看分支与提交信息
git branch <new-branch>
git branch testing # 创建分支
git log --oneline --decorate # 查看HEAD
git checkout <branch> 切换分支,移动HEAD指针
git log --oneline --decorate --graph --all # 查看分叉历史
git checkout -b <new-branch> # 创建并切换至新分支
git branch --merged # 查看当前分支下以今合并分支
iss53 # 可删除
* master
git branch --no-merged # 查看当前分支下未合并分支
testing
HEAD指针:指向引用分支,branch 基于其分支向下创建分支- 分支切换会改变你工作目录中的文件
hotfix 模式
git checkout master # 切换到主线
git checkout -b hotfix # 建立并切换到 hotfix分支
[some changes]
git commit -a -m 'fixed bugs' # 暂存并提交修改
git checkout master # 切换到主线
git merge hotfix # 合并分支 将当前分支移动为其子分支
git branch -d hotfix # 删除临时分支
- 快进(fast-forward) 父子继承
- 合并提交 多重继承
遇到冲突时的分支合并
<<<<<<< HEAD:index.html
<div id="footer">contact : email.support@github.com</div>
=======
<div id="footer">
please contact us at support@github.com
</div>
>>>>>>> iss53:index.html
之后
<div id="footer">
please contact us at email.support@github.com
</div>
# 修改完冲突后
git add [conflict]
git status
git mergetool # 或者通过图形化引导解决冲突
git tag 打标签 (不好用)
标签推送需要手动推送
git tag -l "[filter|*]" # 列出标签
git tag -a v1.2 9fceb02 # 后期对提交打标签 校验和[7位起]
git tag -d [tag] # 删除标签
git push origin [tag] # 推送标签分支
git push origin --tags # 推送所有未登记标签
$ git push origin --delete <tagname> # 显式推送删除标签更新
- 轻量标签 lightweight
- 引用分支
- 附注标签 annotated
- 标签者信息、日期、附注标签、校验码
git tag -a v1.4 -m "my version 1.4"
github
命令行新建仓库
echo "# [project-name]" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/[user]-github/[project-name].git
git push -u origin main
推送已存在项目到仓库
git remote add origin https://github.com/[user]-github/[project-name].git
git branch -M main
git push -u origin main
Github SSH链接 RSA密钥对生成 注册
ssh-keygen -t rsa -b 4096 -C "your_comment" -f ~/.ssh/id_rsa -N "passphase" # 生成密钥对 OpenSSH
# 私钥 ~/.ssh/id_rsa
# 公钥 ~/.ssh/id_rsa.pub
clip < ~/.ssh/id_rsa.pub # 复制公钥到剪贴板
# github > login > setting > SSH and GPG keys > new SSH keys
git config --global user.name "Firstname Lastname"
git config --global user.email "your_email@youremail.com"
ssh -T git@github.com # 测试链接
ssh -vT git@github.com # DEBUG 测试链接
Git 凭证存储
- 默认每一次连接都会询问你的用户名和密码。
- “cache” 模式会将凭证存放在内存中一段时间。 密码永远不会被存储在磁盘中,并且在15分钟后从内存中清除。
- “store” 模式会将凭证用明文的形式存放在磁盘中,并且永不过期。 这意味着除非你修改了你在 Git 服务器上的密码,否则你永远不需要再次输入你的凭证信息。 这种方式的缺点是你的密码是用明文的方式存放在你的 home 目录下。
git config --global credential.helper=[cache|store]
[credential]
# C:\users\username\.gitconfig
helper = store --file /mnt/thumbdrive/.git-credentials
helper = cache --timeout 30000