Git

安装/更新 #

CentOS 7 #

CentOS 7 内置了 Git 1.8.3,首先将其卸载。

yum remove git

由于官方源没有最新版本的 Git,所以需要增加第三方源,这里采用 ius

yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

在仓库列表中看到名为 git236 的库,因此使用 yum 安装。

yum install git236

安装完成后确认版本

git version

console:/ # git version 2.36.6

Ubuntu 18.04 #

apt install git

配置 #

忽略文件模式改变 #

git config --add core.filemode false

创建空仓库 #

git init --bare repo.git

添加远端仓库 #

git remote add origin <url>

拉取代码到指定目录 #

git clone project-url /path/to/dir

分支 #

创建分支 #

git branch <branch-name>

切换分支(不存在则自动创建) #

git branch -b <branch-name>

切换到本地不存在,但远端存在的分支 #

# 远端名称为 simon/dev
$ git branch -a
remotes/simon/dev

# 本地 branch 名称为 dev,同时设置远端为 simon/dev
$ git checkout -b dev simon/dev

修改分支名称 #

git branch -m <old-name> <new-name>
git branch -m <new-name> # 修改当前分支名称

拉取指定分支 #

git clone -b <branch-name> 你的远端地址

推送分支到远端 #

git push origin <local branch>:<remote branch> #remote branch不存在则自动创建

删除本地分支 #

 git branch -d <branch-name>

删除远端分支 #

git push origin --delete <branch-name>

更新远程分支 #

git remote update origin --prune
or
git remote update origin -p

分支命名规范 #

  • master(主分支,永远是可用的、稳定的、可直接发布的版本,不能直接在该分支上开发)
  • develop(开发主分支,代码永远是最新,所有新功能以这个分支来创建自己的开发分支,该分支只做只合并操作,不能直接在该分支上开发)
  • feature-xxx(功能开发分支,在develop上创建分支,以自己开发功能模块命名,功能测试正常后合并到develop分支)。如 feature/add-new-feature-20240119
  • release(预发布分支,在合并好feature分支的develop分支上创建,主要是用来提测的分支,修改好bug并确定稳定之后合并到develop和master分支,然后发布master分支)
  • release-fix(功能bug修复分支,在release上创建分支修复,修复好提测的bug之后合并回release分支。)
  • hotfix-xxx(紧急bug修改分支,项目上线之后可以会遇到一些环境问题需要紧急修复,在对应版本的release分支上创建,流程跟release分支相似,修复完成后合并release分支,根据情况判断需不需要再合并到develop和master分支)

fast-forward #

快进(Fast-forward)是指将当前分支直接移动到远程追踪分支的最新提交的操作。它是一种合并(merge)策略,不会创建新的提交。

There is no tracking information for the current branch. #

git branch --set-upstream-to=origin/<branch> main

将 dev 对齐到 master #

  1. 将工作区切换到 dev 分支
git checkout dev
  1. 把 origin/master 的修改 rebase 到 dev,然后把 dev 上特有的修改跟到后面,解完冲突后可以执行步骤3(参考)
git rebase origin/master
  1. -f 代表强行推送,需要有相关权限
git push -f origin HEAD:refs/heads/dev-branch

编辑之前已提交的 commit #

执行命令进入交互界面,把想要编辑的 commit 之前的 pick 改为 edit,输入 :wq 退出交互界面。

git rebase -i <想要编辑的 commit 的父 commit id>

修改代码后,执行 git addgit commit --amend,然后使用 git rebase --continue 完成 rebase。

代理 #

设置全局代理 #

git config --global http.proxy http://127.0.0.1:12333
git config --global https.proxy http://127.0.0.1:12333

设置 SSH 的 https 代理 #

修改 ~/.ssh/config 文件

Host github.com
Hostname ssh.github.com
Port 443
User git

git_ssh_proxy.md

管理公钥 #

公钥收集起来放到服务器的/home/git/.ssh/authorized_keys

Hooks #

监听某个操作,然后触发自定义逻辑。脚本位于 .git/hooks/。 注:.git 文件夹不会上传到远端仓库,因此只会在本地生效。 附:共用 hooks

git hooks for hexo #

#hooks/post-update
git --work-tree="/var/www/html" --git-dir="/root/blog.git" checkout -f

git pull 时避免出现 &ldquo;Merge branch &lsquo;master&rsquo; of &hellip;&rdquo;] #

git pull --rebase

参考

修改 commit 信息 #

git commit --amend

Git commit 的七条规则 #

  1. Separate subject from body with a blank line(将主题与正文用空行分隔开来)
  2. Limit the subject line to 50 characters(将主题行限制在50个字符以内)
  3. Capitalize the subject line(将主题行的首字母大写)
  4. Do not end the subject line with a period(不要在主题行末尾加句号)
  5. Use the imperative mood in the subject line(在主题行中使用祈使语气)
  6. Wrap the body at 72 characters(在每行正文中达到 72 个字符进行换行)
  7. Use the body to explain what and why vs. how(在正文中解释“什么”和“为什么”,而不是“如何”)

AngularJS 的 commit 规范

处理中文乱码 #

git config --global core.quotepath false #禁止路径转义

查看/清空stash #

git stash list
git stash clear

stash 文件时指定信息 #

git stash push -m "message"

增加 .gitignore 时,有一些不想追踪的文件已经被添加到缓存区 #

如果是文件夹:git rm -r --cached 文件夹名
如果是文件:git rm --cached 文件名

patch #

git format-patch HEAD^ #生成最近一次commit的patch
git am xxx.patch #直接将patch的所有信息打上去,无需重新git add和git commit,author也是patch的author而不是打patch的人。
git apply xxx.patch #与git am的区别是:git apply并不会将commit message等打上去,打完patch后需要重新git add和git commit。

<a href="https://www.cnblogs.com/Galesaur-wcy/p/15751576.html">解决 git am 时 patch does not apply</a> #

# 1. 强制合并不冲突的部分,冲突部分会生成 .rej 文件
git apply --reject patch-name 
# 2. 手动合并 .rej 文件中的冲突,合并完成后删除 .rej文件
# 3. 添加合并完的文件到暂存区
git add .
# 4. 继续 apply patch
git am --continue

Log #

单行显示 #

git log --oneline

只显示文件名 #

git log --name-only

统计代码行数 #

git log --author="simon.li" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'

统计每笔 commit 的行数变化 #

git log --shortstat

add #

git add -u . #添加到暂存区,不包括untracked files
git add . #添加到暂存区,包括untracked files

reset #

git reset --hard HEAD~ #删除 commit 历史中的第一笔修改
git reset --hard <commit-id> # 删除 commit-id 前的所有修改

Tag #

# 列出所有 tag
git tag

# 列出指定版本 tag,* 位通配符
git tag -l "v1.8.5*"

# 创建 tag
git tag -a v1.4 -m "my version 1.4"

# 查看 tag
git show v1.4

# 创建轻量 tag
git tag v1.4-lw

# 推送 tag
git push origin v1.5

# 删除本地 tag
git tag -d v1.4-lw

# 删除远端 tag
git push origin --delete <tagname>

# 切换至 tag 指定版本
git checkout 2.0.0

squash #

作用:合并 feature 分支的所有修改到同一笔。

git checkout master
git merge --squash feature-branch
git commit -m "commit new feature"