Skip to content
Netflix - 每月低至 25 元

Git 快速上手

基本配置

路径 C:\Users\用户名\.gitconfig

ini
[user]
	name = izhichao
	email = admin@zhichao.org
[filter "lfs"]
	clean = git-lfs clean -- %f
	smudge = git-lfs smudge -- %f
	process = git-lfs filter-process
	required = true
[init]
	defaultBranch = master

远程仓库

HTTPS 协议

修改 .git/configurl 项为

sh
url=https://用户名:密码@github.com/用户名/仓库名.git

配置代理

修改 C:\Users\用户名\.gitconfig 文件

[http]
  proxy = http://127.0.0.1:7890
[https]
  proxy = http://127.0.0.1:7890

SSH 协议

sh
ssh-keygen -t rsa -C "注册邮箱"

打开相应路径下 .ssh 文件夹中的 id_rsa.pub ,复制里面的key

上传至 Github - Account - Setting

配置代理

新建 C:\Users\用户名\.ssh\config 文件

Host github.com
  User git
  Port 22
  Hostname github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Program Files\Git\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

Host ssh.github.com
  User git
  Port 443
  Hostname ssh.github.com
  IdentityFile "C:\Users\用户名\.ssh\id_rsa"
  TCPKeepAlive yes
  ProxyCommand "C:\Program Files\Git\mingw64\bin\connect.exe" -S 127.0.0.1:7890 -a none %h %p

常用命令

sh
# Git仓库初始化
$ git init

# 查看当前工作状态
$ git status

# 添加到缓存区
$ git add 文件名
$ git add 文件名1 文件名2 文件名3 ......
$ git add .  # 添加当前目录到缓存区中
$ git add -A # 提交所有文件

# 提交至版本库
$ git commit -m "注释"

# 克隆线上仓库到本地
$ git clone 线上仓库地址

# 提交到线上仓库
$ git push

# 拉取线上仓库
$ git pull

版本回退

sh
# 查看历史版本
$ git log
$ git log --pretty=oneline
$ git reflog # 查看所有历史版本

# 回退操作
$ git checkout filename	# 工作区回滚
$ git reset HEAD^1	# 撤销最后一次提交

Branch

sh
# 查看分支
$ git branch

# 创建分支
$ git branch 分支名

# 切换分支
$ git checkout 分支名
$ git checkout -b 分支名 # 创建并切换

# 删除分支
$ git branch -d 分支名

# 合并分支
$ git merge 被合并的分支

Commit

Commit 合并

  1. git log 获取需要合并的前一个 commit id
  2. git rebase -i [startpoint] [endpoint]
  3. 根据提示将需要合并的 commit 前的 pick 修改为 squash
  4. 注释多余的 commit 描述

Commit 规范

  • chore:构建过程、工具、库依赖等变更,不影响源代码
  • build:影响构建系统或外部依赖项的更改(例如:gulp、broccoli、npm)
  • ci:对 CI 配置文件和脚本的更改
  • docs:文档变更(例如注释、文档添加或更新)
  • feat:新功能(新增功能或功能增强)
  • fix:修复 bug
  • perf:提高性能的代码更改
  • refactor:重构代码,既不修复错误也不添加新功能
  • style: 代码样式变更,不影响代码逻辑(例如格式化代码、空格、分号等)
  • test:添加或修改测试代码

Commit 批量修改

sh
# change.sh
git filter-branch -f --env-filter '

OLD_EMAIL="old@zhichao.org"
CORRECT_NAME="izhichao"
CORRECT_EMAIL="new@zhichao.org"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

开启大小写敏感

sh
git config core.ignorecase false --global
关注微信公众号RackNerd - 美国 163 直连线路
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0

预览:

评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3