git操作

0 前言

git日常操作的那些命令,很好记,但总是有特殊需求,这些命令就不太好记了,本文记录遇到的不常用命令,以备查询。

注:操作很长的,单独列出文章。

查看全局所有配置信息:

1
git config --list --show-origin

1 git包含子仓库

这个问题是我往gitee上传hexo代码时遇到的,hexo代码文件夹,本身是一个git仓库,而其中的themes/ayer也是一个git仓库,当在hexo根目录下执行git add时,就会提示警告。如果忽略警告,强行commit、push,就会发现在gitee上显示的ayer文件夹是打不开的,再次clone后,ayer文件夹是空的,不符合预期。

1.1 子仓库作为普通文件夹提交

1.1.1 操作

关键是两行代码:

1
2
3
4
# 删除已保存的信息
git rm --cached themes/ayer
# git add themes/hexo-theme-huhu # 注意区别
git add themes/hexo-theme-huhu/ // 最后一定要加上/,表示将这个文件夹加入,而不是将这个文件夹当做一个子模块。

之后,就是正常的add、commit、push了,注意子模块中的.gitignore文件,同样会产生作用。

1.1.2 参考

git仓库包含子仓库时,add报错的解决办法

1.2 作为子仓库提交

我选择的解决办法是第一个,子仓库命令git submodule add只是了解过,并没有用过,建议真要使用的时候,请先搜索。
了解过后的理解:表现形式上,在add、commit、push之后,gitee上表现得是一个链接的方式,打开ayer文件夹,就是跳转到ayer的官方仓库,本质上这个文件夹还是空的。clone时,下载下来的ayer文件夹是空的,还得执行对应的子模块更新命令,有点麻烦。但是优势在于push的时候,不用上传ayer文件夹,节省流量。
另一个问题就是,如果子模块是第三方的,并且也对其作了修改,这个时候子模块就不好用了,除非把子模块作为一个新的远程仓库,本地关联这个新的,而不是原来子模块的根仓库。

2 修改已提交的commit注释

操作太长,已独立出来,参见 git修改提交信息与注释

3 git从指定的commit处创建分支

1
2
3
4
# 创建分支
git branch 分支名称 commit的hash/某个tag名
# 创建分支,并切换过去
git checkout -b justin commit的hash/某个tag名

4 git代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 设置全局代理
# http
git config --global https.proxy http://127.0.0.1:1080
# https
git config --global https.proxy https://127.0.0.1:1080

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

# 只对github.com使用代理,其他仓库不走代理
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
git config --global https.https://github.com.proxy socks5://127.0.0.1:1080
# 取消github代理
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy

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

5 git回滚commit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 命令执行后,上一次已经commit过的文件,需要重新add
git reset HEAD^
git reset --mixed HEAD^

# 需要重新commit
git reset --soft HEAD^

# 上一次的修改全部丢失,少用这个参数
git reset --hard HEAD^

# 撤销reset操作
# 通过此命令,找到原来的commit的ID
git reflog
# 上面用哪个参数,返回也是用哪个参数
git reset commitID

6 git合并冲突

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 拉取远程更新
git fetch origin/master
# 比较分支差异
git diff origin/master master
git log branch1...branch2
git log --lefg-right branch1...branch2

# 合并,提示冲突
git merge origin/master
# 查看具体冲突文件
git status

# 手动解决冲突

# 解决完后,再提交
git add .
git commit -m "合并提示信息"

7 git 中文乱码

1
2
# git status乱码
git config --global core.quotepath false

8 LF与CRLF的转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true

# 提交时转换为LF,检出时不转换
git config --global core.autocrlf input

# 提交检出均不转换
git config --global core.autocrlf false

# 拒绝提交包含混合换行符的文件
git config --global core.safecrlf true

# 允许提交包含混合换行符的文件
git config --global core.safecrlf false

# 提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn

# 设置 core.eol 为 lf
git config --global core.eol lf

# 设置 core.eol 为 crlf
git config --global core.eol crlf

win上使用:

1
2
3
4
5
6
7
8
# 提交时转换为LF,检出时不转换
git config --global core.autocrlf input

# 提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn

# 设置 core.eol 为 lf
git config --global core.eol lf

9 git fetch使用

方法一:

1
2
3
4
5
6
7
8
# 从远程的origin仓库的master分支下载代码到本地的origin master
git fetch origin master

# 比较本地的仓库和远程参考的区别
git log -p master.. origin/master

# 把远程下载下来的代码合并到本地仓库,远程的和本地的合并
git merge origin/master

方法二:

1
2
3
4
5
6
7
8
9
10
11
# 从远程的origin仓库的master分支下载到本地并新建一个分支temp
git fetch origin master:temp

# 比较master分支和temp分支的不同
git diff temp

# 合并temp分支到master分支
git merge temp

# 删除temp
git branch -d temp

10 设置git log的日期格式

1
2
git config --global log.date iso
git config --global log.date format:'%Y-%m-%d %H:%M:%S'

11 git查看各个branch之间的关系图

1
git log --graph --decorate --oneline --simplify-by-decoration --all
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2020-2024 zhanglonglong
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信