在编程的过程中,使用git
的各种命令是自然的。它是一个不需要特别去学习就能有很好的体验的好工具。下面完全出于刻意学习的目的,记录一些之前不求甚解的内容或根本没去用的好功能。
参考资料:Pro Git
Basics
天天用的commit -am
实际使用中通常不会需要用到staged
,-am
已经成为习惯动作。
-a
跳过手动add
,自动添加对已经track的文件的修订-m
直接添加提交信息
–cached选项
--cached
选项可用于操作staged状态,而且比--staged
更泛用。
示例
从代码库移除文件,但不会删除本地的文件。
git rm --cached README.md
mv命令
显式的重命名文件。虽然git自己会找,但有时候找的不见得正确。
tag命令
git tag
就会给出tag的列表,但如果用git tag list
则会创建一个名为list的tag,务必留意。
status命令中的提示
git status
除了查看文件信息,还提供了一些大多数时间都会被忽略的命令提示。如果你马上要做一件不常做的动作,也许其中就有提示,比如放弃修订(特别危险的一个动作)。
remote -v
git remote -v
可以更方便的查看远程代码库的地址,比git remote get-url <repo-addr>
要简单。
Branching
不要滥用rebase
rebase
会修改提交历史记录,不要用来操作已经发布的repo(会搅得一团乱),应仅用来处理本地的提交。
Do not rebase commits that exist outside your repository.
Git on the Server
用官方提供的docker image可以部署gitlab-ce
。具体可以参考一份详细的在线教程。下面提供的是在时速云上的部署方法。
创建应用
事先准备repo
和log
两个存储点,gitlab/gitlab.rb
配置文件,再使用编排创建一个名为gitlab-ce
的单容器应用。需要注意的是,这样配置应用镜像里会运行一个k8s,对容器配置的要求会比较高。低于2G内存基本无法正常运行,编排中创建的4G版也不能说流畅。
kind: Service
apiVersion: v1
metadata:
name: gitlab-ce
labels:
name: gitlab-ce
annotations:
tenxcloud.com/schemaPortname: 'gitlab-ce-1/TCP,gitlab-ce-2/TCP,gitlab-ce-3/HTTP'
spec:
ports:
- name: gitlab-ce-1
protocol: TCP
targetPort: 22
port: 22
- name: gitlab-ce-2
protocol: TCP
targetPort: 443
port: 443
- name: gitlab-ce-3
protocol: TCP
targetPort: 80
port: 80
selector:
name: gitlab-ce
externalIPs:
- 11.11.1.1
---
kind: Deployment
apiVersion: v1
metadata:
name: gitlab-ce
labels:
name: gitlab-ce
spec:
replicas: 1
selector:
matchLabels:
name: gitlab-ce
template:
metadata:
labels:
name: gitlab-ce
spec:
containers:
- name: gitlab-ce
image: 'index.tenxcloud.com/huandzh/gitlab-ce:latest'
ports:
- containerPort: 22
protocol: TCP
- containerPort: 443
protocol: TCP
- containerPort: 80
protocol: TCP
env:
- name: PATH
value: >-
/opt/gitlab/embedded/bin:/opt/gitlab/bin:/assets:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- name: TERM
value: xterm
resources:
limits:
memory: 4Gi
requests:
memory: 4Gi
args:
- /assets/wrapper
imagePullPolicy: Always
volumeMounts:
- name: volume-3
mountPath: /var/opt/gitlab
readOnly: false
- name: volume-4
mountPath: /var/log/gitlab
readOnly: false
- name: configmap-volume-1
mountPath: /etc/gitlab/gitlab.rb
subPath: gitlab.rb
readOnly: false
volumes:
- name: volume-3
rbd:
image: repo
fsType: xfs
- name: volume-4
rbd:
image: log
fsType: xfs
- name: configmap-volume-1
configMap:
name: gitlab
items:
- key: gitlab.rb
path: gitlab.rb
三个挂载点分别对应
- 存储repo :
/var/opt/gitlab
- 存储log :
/var/log/gitlab
- 配置文件 :
/etc/gitlab/gitlab.rb
如果需要使用绑定域名和自己的ssl证书,使用平台提供的反向代理,gitlab.rb
需要做一些特殊的配置,取消容器对https端口的转发。
external_url 'https://yourdomain'
nginx['redirect_http_to_https'] = false
nginx['listen_port'] = 80
nginx['listen_https'] = false
首次登入
应用首次启动需要一定的准备时间,创建后马上访问可能会遇到502错误,属于正常情况。等待一段时间后,首次登入页面会提示创建密码,对应为root账户。root账户创建后就可以继续注册其他用户了。
对于小规模团队来说自建云上的代码库不是特别有意义,不如直接购买线上服务,于是后面就没有做进一步的尝试了。如果还有兴趣可以参见官方文档。