皆为 Bash 命令

初始化

1
git init

暂存区

  • 添加所有文件到暂存区
1
git add .
  • 添加指定文件到暂存区
1
git add <file_path>
  • 查看暂存区状态
1
git status
  • 清除暂存区(保留工作区文件)
1
git rm -r --cached .
  • 查看暂存区中文件大小(压缩后)
1
2
3
4
5
6
7
8
9
10
11
blobs=$(git ls-files -s | awk '{print $2}')
total=0
for blob in $blobs; do
size=$(git cat-file -s "$blob" 2>/dev/null)
total=$(( total + size ))
done

echo "Staging Area Size (Compressed in Git):"
echo "$total bytes"
echo "$(awk "BEGIN {printf \"%.2f KB\", $total/1024}")"
echo "$(awk "BEGIN {printf \"%.2f MB\", $total/1024/1024}")"

提交与记录

  • 提交更改
1
git commit -m "commit 注释"
  • 查看已追踪文件
1
git ls-files
  • 查看指定目录下的已追踪文件
1
git ls-files <dir>
  • 移除暂存区中文件(不删除工作区)
1
git rm --cached <file_path>

分支与推送

  • 推送到远程主分支
1
git push origin master
  • 推送到指定分支(普通推送)
1
git push origin <branch>
  • 推送并建立追踪关系
1
git push -u origin <branch>
  • 强制推送(覆盖)
1
git push -f origin <branch>
  • 安全强制推送(检查是否有他人新提交)
1
git push --force-with-lease origin <branch>
  • 推送所有分支
1
git push --all origin
  • 删除远程分支
1
git push origin --delete <branch>

标签

  • 推送单个标签
1
git push origin <tagname>
  • 推送所有本地标签
1
git push origin --tags
  • 删除远程标签
1
git push origin --delete <tag_name>

协议

  • 查看远程仓库地址
1
git remote -v
  • 将 SSH 地址改为 HTTPS
1
git remote set-url origin https://github.com/username/repository.git
  • 将 HTTPS 地址改为 SSH(需指定用户名和密钥)
1
git config --global core.sshCommand "ssh -i /path/to/ssh_id -o User=username"
  • 检查 SSH 连接是否成功
1
ssh -T [email protected]

配置项

  • 修改 Git 缓冲区大小(单位为 bytes)
1
git config --global http.postBuffer <size>

大文件

  • 检查仓库中最大文件(按大小升序)
1
git ls-files -s | sort -n -k3