Skip to content

Latest commit

 

History

History
257 lines (186 loc) · 4.64 KB

README.md

File metadata and controls

257 lines (186 loc) · 4.64 KB
title sidebar
Git
true

Git

## 代理
git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080

git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080

git config --global --unset http.proxy
git config --global --unset https.proxy

npm config delete proxy

Git 常用命令

Git 常用命令

添加远程分支

git remote add origin <url>

查看项目分支(远程和本地)

git branch -a

查看本地分支关联(跟踪)的远程分支

git branch -vv

切换分支

# 复制远程分支到本地且切换到该分支
git checkout -b develop origin/develop
# 切换到本地已有分支
git checkout develop

删除分支

# 删除本地分支
git branch -d <BranchName>
# 删除远程分支
git push origin :<BranchName>

删除本地远程分支的跟踪

其他小伙伴删除了远程分支,但自己本地还存在远程分支的跟踪,使用删除远程分支的命令会报错,应采用如下命令

# 逐个删除
git branch --delete --remotes <Remote>/<BranchName>
git branch -dr <Remote>/<BranchName>
# 一次删除所有远程已删除的跟踪
git remote prune <Remote>

重命名文件和文件夹

git mv <oldname> <newname>

查看远程仓库 URL

git remote -v

重命名本地分支

git branch -m <newBranchName>

取消本地关联的远程仓库

git remote remove origin

重命名 remote

git remote rename origin destination

清空提交历史记录,成为新仓库

方法一:

git update-ref -d HEAD
# 重设第一个 commit
git commit
# 强制推送到远程仓库
git push -f -u <Romote> <Branch>

方法二:

# 1. 新建分支且无任何 commit 信息
git checkout --orphan latest_branch
# 2. 初始 commit
git add -A
git commit -am "Initial"
# 3. 删除原有分支 master
git branch -d master
# 4. 重命名当前分支为 master
git branch -m master
# 5. 强制推送到远程分支
git push -f origin master

只提交某文件的部分修改

git add <filename> -p

该命令每一处修改都问询问你是否提交

Stash 指定文件

git stash push -m "stash message" <filePath>

逐个询问 Stash 文件

git stash -p

Tag

# 打标签
git tag -a "v1.0.0"
# 删除标签,和删除分支一样
git push <Remote> :<Tag>

查看标签详细信息

# 仅显示单行注释信息
git tag -ln
# 显示多行注释信息
git tag -ln<number> # 如显示3行:git tag -ln3

简化展示 commit 历史

git log --pretty=oneline --graph

显示 branch-a 分支有但 branch-b 没有的 commit

git log branch-a ^branch-b

可用来查看 branch-a 是否已经合并到 branch-b

忽略并删除已提交的文件

# 删除已提交的文件夹:.github
git rm --cached -r .github
# 编辑 .gitignore 添加忽略
# .github

不同工作目录使用不同的 Git 配置

首先在工作目录新建 .gitconfig,假设路径为 ~/company/.gitconfig

[user]
	name = Another Name
	email = [email protected]

修改用户目录下的配置(~/.gitconfig),增加如下:

[user]
	name = Jun
	email = [email protected]
[includeIf "gitdir/i:~/company/"]
	path = ~/company/.gitconfig

此时,全局 user.name 默认为 Jun~/company/ 目录下为 Another Name

Git 词典

Git 提交信息技巧。

git commit -m "<word>: <描述>"

如:

git commit -m "Fix: 修复了很牛逼的 Bug"

参考 Angular 提交规范

word 场景
Initial 初始化
Fix 修复
Feat(Feature) 新功能
Test 测试用例
Remove 移除
Revert 还原
Perf(Performance) 优化提升
Style 格式,不影响代码运行
Docs(Document) 文档
CI 自动构建
Upgrade 升级
Downgrade 降级
Refactor 重构,不是修复 BUG,也不是添加功能

其他

生成 AUTHORS

git log --format='%aN <%aE>' | LC_ALL=C.UTF-8 sort -uf > AUTHORS