git 基本操作


config 的三个作用域

git config --local # local 只对某个仓库有效
git config --global # golbal 对当前用户所有仓库有效
git config --system # system 对系统所有登陆的用户有效
git config

显示 config 的配置, 加 --list

git config --list --local
 git config --list --global
 git config --list --system

重命名

git mv a b

add 更新文件

git add -u

git 查看日志

git log -n2 --oneline
git log --all
git log --all --graph

查看帮助

git help --web clone

安装图形

brew install git-gui

查看 git object

git cat-file -t 1e0da1e74b20e3b873ce80549ec458b4805c132f # 查看类型
git cat-file -p 1e0da1e74b20e3b873ce80549ec458b4805c132f # 查看内容

创建新分支

git checkout -b new_branch exist_branch_or_commit

diff

git diff HEAD HEAD^^
git diff HEAD HEAD^1
git diff HEAD HEAD^2

删除分支

git branch -d <branch_name>
# 强制
git branch -D <branch_name>

修改上一次commit的信息

git commit --amend

变基

git rebase -i <commit_id>

比较暂存区和 HEAD

git diff # 工作区和暂存区的差异
git diff --cached # 暂存区和 HEAD 之间的差异
git diff -- <file1> <file2> # 比较多个文件
git diff HEAD # 比较工作区和 HEAD 之间的差异

将暂存区恢复到与 HEAD 一致

git reset HEAD # 暂存区的变更会添加到工作区
git reset HEAD -- <file_name>

比较两个 branch/commit

git diff master dev
git diff master dev -- main.java

将工作区恢复到与 暂存区一致

git checkout -- <file_name>

暂存区和工作区回退到 commit_id

git reset --hard <commit_id>

删除暂存区文件

不需要先在工作路径先删除

git rm  test.scala

存取暂存区与工作区未提交的变更

git stash # 将暂存区与工作区的变更放入 stash
git stash apply # 将 stash 中的栈顶放入工作区,栈顶元素不删除
git stash pop # 将 stash 中的栈顶放入工作区,栈顶元素删除

.gitignore

*.d -- 所有以 .d 结尾的文件 *.DSYM/ -- .DSYM 结尾的文件夹下的所有文件 *.DSYM -- .DSYM 结尾的文件 与 文件夹下的所有文件

备份

直观区别:哑协议传输进度不不可⻅;智能协议传输可⻅。
传输速度:智能协议比哑协议传输速度快。

常⽤协议 语法格式 说明
本地协议1 /path/to/repo.git 哑协议
本地协议2 file:///path/to/repo.git 智能协议
http协议 http://git-server.com:port/path/to/repo.git 智能协议
https协议 https://git-server.com:port/path/to/repo.git 智能协议
ssh协议 user@git-server.com:path/to/repo.git 智能协议

生成 ssh key

ssh-keygen -t rsa -b 4096 -C "git@wangxiuwen.com"

允许历史上不相关的分支 merge

git merge --allow-unrelated-histories master <remote>/master

fast-forward

提交到远程仓库的代码按照时间顺序的

基于远端特性分支创建本地分支,同时切换到该分支

test git:(master) git -P  branch -rav
* master                                  0a5be37 init
  remotes/github/feature/add_git_commands 17a41e0 Update README.md
  remotes/github/feature/test_new_branch  17a41e0 Update README.md
  remotes/github/main                     95cda65 Initial commit
  remotes/github/master                   0a5be37 init
➜  test git:(master) git checkout -b feature/add_git?commands github/feature/add_git_commands

github 高级搜索

github 默认搜索仓库名称和描述

created:>2020-12-01 in:readme stars:>1000 language:Javascript
filename:gitlab-ci.yml

参考链接

github help
课件