关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

git奇技淫巧

发布时间:2023-06-27 14:00:15

收集一些有趣&好用的git命令


git commit 多行提交信息


git commit -m "commit title" -m "commit description"


git commit accepts several message flags (-m) to allow multiline commits




只回退某个文件(已经提交)

谢谢。 如果您可以还原 /docs Git 子树中的更改,我将合并。 历史内容的文档编辑需要在 hugoDocs 存储库中完成。

git log fileName

(如果文件修改记录太多,则使用 git log -number fileName)

找到想要还原的版本的commit_id

git checkout ${commit} fileName

git checkout 8f7891e70c51163185fbbd878b4925d11b6a2a93 docs/content/en/news/0.82.0-relnotes/index.md

提交了.idea怎么办


git rm -rf .idea

git config --global url."https://".insteadOf git://


git config --global url."https://".insteadOf git://

之后.gitconfig中会多出一行参数设置:

 [url "https://"]  insteadOf = git://

   

设置之后,使用git://,或者https:// 两种方式都会默认变成https://的形式




Git中设置代理和取消代理

git config --global http.proxy 'socks5://127.0.0.1:1080' && git config --global https.proxy 'socks5://127.0.0.1:1080'




tag自动递增


如 2.0到2.1

#!/bin/bash set -e  # Add tag, auto incr tag latest_tag=$(git describe --tags `git rev-list --tags --max-count=1`) echo "tag为:" echo $latest_tag  new_tag=$(echo $latest_tag | awk -F. -v OFS=. '{++$NF;print};')  echo "新的tag为:" echo $new_tag  git tag -a "$new_tag" -m "$commit_msg"

   

关于 awk -v OFS


git tag 和git tag -a的细微区别:

-a必须要有tag说明,而不带-a即直接git tag不会强制要求写说明。当使用git tag -m时,效果和git tag -a -m是一样的




修改远程仓库地址

git remote rm origin

git remote add origin 新的远程仓库地址


将两个commit合并为一个

git rebase -i HEAD~2 (如果想合并之前N个commit,则将2改为N)

-i 即 interactive

git rebase --interactive HEAD~[N]即交互式 rebase。

简写为git rebase -i HEAD~[N]

N 为想要合并的提交的数量,从最近的一次提交往前数 (N >= 2)

通过将多个临时的小的提交合并成一次提交,然后将整理好的代码 push 给远端

(历史记录是按照时间排序的,时间近的排在前面)

将第二行的pick改为squash(即压缩此提交)

然后wq保存

再 git log查看:

git push -f



合并多个commit


同上

git rebase -i 想合并在一起的最后一次提交的再前一次提交

(或 git rebase -i HEAD~n, n为要合并的提交次数)

将第一行之后的pick改为ssquash (挤压,压进 之意)

wq保存退出,git会压缩提交历史

若有冲突,需进行修改

修改完成后,需要保存

git add .

git rebase --continue

若想退出放弃此次压缩,执行命令: git rebase --abort

若没有冲突(或 冲突已解决), 则会出现一个 commit message 编辑页面,修改 commit message,然后 输入:wq

之后 git push -f 或 git push --force,推送到远程


Git 合并多个 commit,保持历史简洁


/template/Home/leiyu/PC/Static