Git使用

# 一、定义
远程主分支叫 origin/master
本地主分支叫 master
当前分支叫 head
# 二、仓库
// 克隆仓库
git clone xxx
// 添加修改
git add xxx
// 添加所有修改
git add .
// 添加 commit
git commit -m "xxx"
// 提交修改到远程仓库
git push
// 拉取远程仓库并合并到主分支
git pull
// 远程仓库强制覆盖本地主分支
git fetch --all && git reset --hard origin/master && git pull
// 查看项目的情况
git status
// 重设仓库(将远程仓库 yyy 移动到新仓库 xxx 内)
// 先在 github 上建立远程新仓库 xxx
// 后克隆远程仓库 yyy
// 重设链接
git remote rm origin
git remote add origin git@github.com:Taraxa-project/taraxa-floret-backend.git
git push --set-upstream origin master
git clone yyy
git remote -v
git remote rm origin (移除远程仓库链接 yyy)
git remote add origin xxx (重设远程仓库链接为 xxx)
git pull
git branch --set-upstream-to=origin/master master
git pull
git push --set-upstream origin master (推送本地仓库到远程仓库 xxx)
git branch --set-upstream-to=origin/master master
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
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
# 2.1 本地仓库和远程仓库
// 查看远程仓库名称
git remote
// 查看远程仓库详细信息
git remote -v
// 推送主分支到远程主仓库
git push origin master (远程仓库叫 origin,本地主仓库叫 master)
// 推送新分支到远程分支仓库
git push origin dev 5.将本地仓库的分支重新指向远程仓库的最新版(避免远程仓库分叉)
git rebase (相当于先取消本地的提交,然后拉取远程最新版,然后将取消的提交添加到最新版上去)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 三、分支
# 3.1 查看分支
// 查看本地和远程所有分支
git branch -a
// 查看远程分支
git branch -r
// 拉取所有分支
git pull --all
// 创建远程分支
git checkout -b 本地分支 origin/远程分支
// 删除本地分支
git branch -d 分支名
// 删除远程分支
git push origin –delete 分支名
// 以下假设新分支的名字是 dev
// 显示分支
git branch 查看本地分支
git branch -r 查看远程分支
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 3.2 创建分支
// 直接拉取远程分支
git fetch origin dev
// 本地
git branch dev
// 创建新分支(同时在本地和远程仓库创建)
git checkout -b dev
git push --set-upstream origin dev(为当前 dev 分支在远程 origin 创建同名推送分支)
// 将本地分支 dev 的链接指向远程的分支 origin/dev(在本地仓库的分支是独立建立的时候使用)
git branch --set-upstream-to=origin/dev dev
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 3.3 删除分支
git branch -d dev(删除前应该先合并分支)
// 强制删除没有被合并的分支
git branch -D dev
1
2
3
4
5
2
3
4
5
# 3.4 切换分支
git checkout devl
// 创建并切换到分支
git checkout -b dev
1
2
3
4
2
3
4
# 3.5 合并分支
// 先切换回主分支
git checkout master
// 再合并分支到当前分支
git merge dev
// 采用非快速合并分支的方式合并分支
git merge --no-ff -m "xxx" dev
// 查看分支合并历史图
git log --graph
git log --graph --pretty=oneline --abbrev-commit
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 3.6 储藏分支
// 临时储藏当前分支
git stash
// 查看储藏记录
git stash list
// 恢复储藏
git stash apply
// 删除储藏记录
git stash drop
// 恢复并删除储藏记录
git stash pop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 3.7 拉取远程分支
// 直接拉取
git clone -b 远程分支名 仓库地址
// 本地已有仓库
// 查看远程分支
git branch -r
// 创建本地分支并关联远程分支
git checkout -b 本地分支 origin/远程分支
// 已有本地分支创建关联
git branch --set-upstream-to origin/远程分支名 本地分支名
// 拉取
git pull
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 3.8 推送分支
git push origin aeneas:aeneas
// 推送本地分支 aeneas(冒号前面的)到远程分支 aeneas(冒号后面的)
1
2
2
# 3.9 子分支同步主分支
// 在子分支上
git merge master
git status
git add .
git commit -m
git push
// 同步远程已删除的分支
git remote prune origin
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 四、版本标签
// 查看提交记录
git log --pretty=oneline --abbrev-commit
// 创建标签
git tag v1.0
// 覆盖原有标签(相当于将 v1.0 重新绑定到当前 commit 上)
git tag -f v1.0
// 在指定 commit xxx 上创建标签
git tag v0.9 xxxx
// 创建标签并添加说明
git tag -a v0.1 -m "version 0.1 released" xxx
// 显示标签
// 显示标签详情
git show v1.0 3.将本地标签推送到远程仓库
git push origin v1.0
// 将本地所有标签推送到远程仓库
git push --tags
git push origin --tags
// 清除忽略的缓存并使远程生效
git rm -r --cached .
git add .
git commit -m 'remove ignore file'
git push
// 重设
git reset --soft
git reset --mixed
git reset --hard
// git 去除大文件的历史记录
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch --ignore-unmatch Miniconda3-latest-Linux-x86_63.sh' --prune-empty --tag-name-filter cat -- --all
// 更新本地工作组(和远程分支一样)
git fetch -p
// 默认 git 配置了忽略大小写敏感
git config core.ignorecase true
// 下面设置大小写敏感为敏感
git config core.ignorecase false
// 这时,再通过 git status 就能找到你修改文件名大小写后的变更了
// 更新所有子项目
git submodule update --init --recursive
git fetch origin
git rebase origin/master
// fork 项目与原项目同步
git clone 新项目地址
git remote add author 原项目地址
git remote -v 查看地址检查
// 拉取原项目更新
git fetch author
// 切换到新项目主分支
git checkout master
// 将原项目更新合并到新项目
git merge author/master
// 推送更新到远程仓库
git push origin master
// 撤销
// 工作区:写代码的区域
// 暂存区: git add 文件以后的区域
// 仓库区: git commit 文件以后的区域
// 撤销内容的修改
git restore <file>
// 撤销提交:即移动文件从暂存区回到工作区
git restore --staged <file>
使用 git add <file> 把文件重新放到暂存区,且保留文件的修改;
使用 git restore <file> 文件仍在本地代码库且会撤销文件的修改;
对于 git restore <file>命令,会撤销文件的修改,使文件恢复到暂存区或本地代码库(取决于文件在修改前的状态);
对于 git restore --staged <file>命令,把文件从暂存区撤回到工作区,保留文件最后一次修改的内容;
// 清空所有 commit
git checkout --orphan latest_branch
git add -A
git commit -am "commit message"
git branch -D master
git branch -m master
git push -f origin master
// 删除敏感数据
git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch viabtc 交易所 Mac 启动.md' HEAD
git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch path/to/your/file' HEAD
git push origin master --force
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 五、代理
// 手动设置 git 代理
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'
// 取消 git 代理
git config --global --unset http.proxy
git config --global --unset https.proxy
// 取消所有修改
git checkout .
// 移除.DS_Store 文件
git rm --cached .DS_Store
find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch
// 强制将 test 分支与主分支保持一致
git push origin master:test -f
// 远程仓库强制覆盖本地主分支
git fetch --all && git reset --hard origin/master && git pull
// 删除远程分支
git push origin --delete 远程分支名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
上次更新: 2022/11/29, 15:43:28