开发常用命令

PowerShell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 初始化仓库
git init

# 添加所有文件到暂存区
git add .

# 添加特定文件到暂存区,需要修改 file_path
git add <file_path>

# 查看暂存区状态
git status

# 清除暂存区
git rm -r --cached .

# 提交更改
git commit -m "commit 注释"

# 推送到远程仓库
git push origin master

# 查看目前被追踪文件
git ls-files

# 查看特定目录下的被追踪文件,需要修改 dir
git ls-files dir

# 移除不需要追踪的文件
git rm --cached <file_path>

# 将 SSH 转为 HTTPS,需要修改 username/repository
git remote set-url origin https://github.com/username/repository.git

# 将 HTTPS 转为 SSH,需要修改 User 和 ssh_id
git config --global core.sshCommand

# 检查 SSH 配置
ssh -T [email protected]

# 查看远程连接方式,git 开头则为 SSH,HTTPS 开头为 HTTPS 连接
git remote -v

# 查看暂存区内容的大小
# BEGIN 无意义,只是方便阅读,不需要复制到终端
# 1. 获取暂存区所有文件的 SHA-1 哈希值
$blobs = git ls-files -s | ForEach-Object { ($_ -split '\s+')[1] }

# 2. 计算所有对象的压缩大小总和(字节)
$totalBytes = 0
foreach ($blob in $blobs) {
$size = git cat-file -s $blob 2>$null
if ($size) { $totalBytes += [int]$size }
}

# 3. 格式化输出结果
Write-Host "Staging Area Size (Compressed in Git):"
Write-Host "$totalBytes bytes"
Write-Host "$([math]::Round($totalBytes / 1KB, 2)) KB"
Write-Host "$([math]::Round($totalBytes / 1MB, 2)) MB"
# END 无意义,只是方便阅读,不需要复制到终端

# 修改 Git 缓冲区大小
git config --global http.postBuffer size # 根据情况用数字替换 size,单位为字节

# 检查是否有大型文件
git ls-files -s | sort -n -k 3

# 推送到分支远程仓库 视情况修改 branch
git push origin <branch> # 普通推送,无额外功能
git push -u origin <branch> # 建立追踪关系,后续该分支可用 git push 直接推送
git push -f origin <branch> # 强制推送,会覆盖他人提交,只能用于个人分支
git push --force-with-lease origin <branch> # 安全强制推送,会检查分支是否有他人的新提交
git push --all origin # 推送所有分支

# 删除远程分支
git push origin --delete <branch>

# 推送远程标签 需要修改 tagname
git push origin <tagname> # 单个标签
git push origin --tags # 所有本地标签

# 删除远程标签
git push origin --delete <tag_name>

cmd

1