Skip to content

Git 实战使用

仲灏2021-12-24约 1 分钟

<div style="display: none;" hidden="true" aria-hidden="true" data-nosnippet>Are you an LLM? You can read better optimized documentation at /pages/769dad.md for this page in Markdown format</div>

可视化训练

https://learngitbranching.js.org/?NODEMO=&locale=zh_CN

切换某个分支到某个版本

安装git

配置账号密码

git
git config --global --replace-all user.name "your user name"
git config --global --replace-all user.email"your user email"

添加公钥

https://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E7%94%9F%E6%88%90-SSH-%E5%85%AC%E9%92%A5

git
$ ssh-keygen -o
Generating public/private rsa key pair.
Enter file in which to save the key (/home/schacon/.ssh/id_rsa):
Created directory '/home/schacon/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/schacon/.ssh/id_rsa.
Your public key has been saved in /home/schacon/.ssh/id_rsa.pub.
The key fingerprint is:
d0:82:24:8e:d7:f1:bb:9b:33:53:96:93:49:da:9b:e3 schacon@mylaptop.local

克隆项目

命令

创建分支 git branch **

切换分支 git cheout **

合并分支 git merge **

git flow

https://www.git-tower.com/learn/git/ebook/cn/command-line/advanced-topics/git-flow/

git flow init

img

git flow feature help

img

git flow feature start express

img

commit message

https://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html

https://github.com/angular/angular/blob/master/CONTRIBUTING.md

每次提交,Commit message 都包括三个部分:Header,Body 和 Footer。

git
<type>(<scope>): <short summary>
  │       │             │
  │       │             └─⫸ Summary in present tense. Not capitalized. No period at the end.
  │       │
  │       └─⫸ Commit Scope: animations|bazel|benchpress|common|compiler|compiler-cli|core|
  │                          elements|forms|http|language-service|localize|platform-browser|
  │                          platform-browser-dynamic|platform-server|router|service-worker|
  │                          upgrade|zone.js|packaging|changelog|dev-infra|docs-infra|migrations|
  │                          ngcc|ve

  └─⫸ Commit Type: build|ci|docs|feat|fix|perf|refactor|test
  • type

    • feat:新功能(feature)
    • fix:修补bug
    • docs:文档(documentation)
    • style: 格式(不影响代码运行的变动)
    • refactor:重构(即不是新增功能,也不是修改bug的代码变动)
    • test:增加测试
    • chore:构建过程或辅助工具的变动

如果typefeatfix,则该 commit 将肯定出现在 Change log 之中。其他情况(docschorestylerefactortest)由你决定,要不要放入 Change log,建议是不要。

  • scope

    • 用于说明 commit 影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。
  • subject

    • 是 commit 目的的简短描述,不超过50个字符。
    • imgimg

工具

Commitizen是一个撰写合格 Commit message 的工具。

npm install -g commitizen

在项目目录里,运行下面的命令,使其支持 Angular 的 Commit message 格式。

commitizen init cz-conventional-changelog --save --save-exact

changelog

自动生成工具

https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-cli

git
$ npm install -g conventional-changelog-cli
$ cd my-project
$ conventional-changelog -p angular -i CHANGELOG.md -s

问题记录

问题现象

使用git pull命令时,提示“refusing to merge unrelated histories”。

原因分析

云端与本地的仓库不同,例如:不同的分支、或不同的仓库等。

处理方法

使用命令$ git pull origin master --allow-unrelated-histories进行强制合并。

问题现象

.gitignore文件无效

原因分析

文件已经被提交过,处于被跟踪状态,所有即使加入.gitignore文件也无效

处理方法

删除该目录的缓存git rm -r --cached unpackage