0%

git 命令使用

git的使用

git 相关命令

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
# 添加远程受控地址
git remote add github git@github.com:User/UserRepo.git

# 查看分支
git branch
# 合并dev分支到本地,变成一条提交记录
git merge --squash dev
git merge --squash -s recursive -X theirs master20231016 --allow-unrelated-histories

# 新建空分支
git checkout --orphan emptybranch
# 下载代码
git checkout -b dev github/master
# 切换分支
git switch master

# 提交
git commit -m 'init commit'

# 删除文件
git rm -rf .
# 删除分支
git branch -D branchTemp
# 删除远程分支
git push origin --delete <branchName>

# 重命名本地分支
git branch -m devel develop

# 推送分支
git push -u origin master

git 基础设置

1
2
3
4
5
6
7
### 记得去除敏感信息
git config --global user.name "你的名字或昵称"
git config --global user.email "你的邮箱"

### 记住密码 ~/.gitconfig
[credential]
helper = store

git 配置文件示例 .git/config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[http]
proxy = http://64.00.00.26:8888
[remote "origin"]
url = http://git.xxxx.com:8888/team-kgq/kgq-xxx-website.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "dev202310"]
remote = origin
merge = refs/heads/dev202310
[credential]
helper = store

git 冲突解决方法 覆盖本地

  1. 其他方法
  2. 合并策略
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
--------------- 重置代码 -------------------

git checkout master
git reset --hard origin/master
git pull origin master
git branch

--------------- 冲突解决方案 -----------------

git status
git diff --name-only --diff-filter=u
## 标记已解决
git add README.md
git commit -m "feat: merge test 20231018"
git push origin test

--------------- 初始工程 -------------------

git clone http://ingit.11019fe.com:8888/team-kgq/kgq-xxx-api.git

git pull

git remote add github git@github.com:team-kgq/KGQ-xxx-Api.git

git fetch github

git 删除大文件

  1. 查看大文件
    1
    git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"
  2. 删除大文件
    1
    git filter-branch -f --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch newapi.phar' --tag-name-filter cat -- --all
  3. 推送到所有分支
    1
    git push origin --force --all

git代理

代理设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 一次性代理
git clone -c http.proxy="http://127.0.0.1:1087" https://github.com/
# 全局设置代理
git config --global http.https://github.com.proxy socks5://127.0.0.1:1086

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

在 ~/.ssh/config 文件中加入以下配置:
# 必须是 github.com
Host github.com
HostName github.com
User git
# 走 HTTP 代理
# ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=8080
# 走 socks5 代理
# ProxyCommand nc -v -x 127.0.0.1:1080 %h %p

代理安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 安装 tinyproxy
sudo apt install tinyproxy

# 修改配置文件
vim /etc/tinyproxy/tinyproxy.conf
# 修改端口号,尽量将该端口号修改为大于 1024,不然需要使用 root 权限才可以启动 tinyproxy
Port 8888
# 修改允许连接到 tinyproxy 的地址,如果没有特殊需求,可以直接将改行注释掉,允许所有 IP 访问
#Allow 127.0.0.1

# 启动代理服务器
service tinyproxy start

# 关闭代理服务器
service tinyproxy stop

# 重启代理服务器
service tinyproxy stop

127.0.0.1:8888

参考

  1. tinyproxy 配置 git代理服务器
  2. git 代理配置方案
  3. git 提交记录清除方案
  4. git 新建空分支
  5. git 删除大文件