Git



alternate text

Git 安装与配置

Git 常用命令

git config

git config --global -e  # 默认为 --global
git config  -e    # or git config --edit
git config --list
git config --global core.editor vim  # 配置默认编辑器 vim

#  设置代理服务- 全局
git config --global http.proxy  socks5://127.0.0.1:1080 # 代理服务器
git config --global https.proxy socks5://127.0.0.1:1080

git config --global --unset http.proxy   # 撤销代理服务器
git config --global --unset https.proxy

git config --global --get http.proxy   # 查询理服务器
git config --global --get https.proxy


#设置代理服务 - 只对github.com
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080

#取消代理
git config --global --unset http.https://github.com.proxy


# 记住密码
$ git config credential.helper store # 永久记住密码
$ git config credential.helper cache  # 临时记住密码15分钟
$ git config credential.helper 'cache --timeout=3600' # 临时记住密码1小时

撤销与回退

$ git rm    --cached path              # 撤销add 文件
$ git rm -r --cached path              # 撤销add 目录

$ git checkout <commit_id> <path>      # 回退单个文件到某一次提交
$ git checkout <branc_name> -- <path>  # checkout file from a brach

$ git revert <commit_id>
# git revert是用一次新的commit来回滚之前的commit, 而git reset是直接删除指定的commit。

$ git commit --amend                   # 修改commit 备注信息

Create New branch

$ git branch  <new_branch_name> <commit_id>
$ git checkout -b  <new_branch_name> <commit_id>
$ git branch -a   # 查看分支(本地 + 远程)
$ git push origin --delete <new_branch_name>   # 删除远程分支

Gitignore

# 忽略,指定文件之外的所有文件
$ tee .gitignore <<-'EOF'
/*

!Dockerfile
!init.sh
!sources.list
!*.md
EOF

git log

$ git log --stat  #  --stat,查看修改了那些文件,仅显示简要的增改行数统计

# https://www.cnblogs.com/Sir-Lin/p/6064844.html
$ git log -- filename  <path>  # 查看某文件的修改历史
$ git show <commit-id> <path>
$ git log -b <branch_name>
$ git log --graph --all
$ git cherry-pick xxx
$ git add  <path>

#####################
# about git diff
#####################

$ git diff --cached
# Compare files from two different branches
$ git diff <branch_name> <another_branch_name> -- <path>

# 比较不同分支,不同文件
$ git diff 4.2.0-dev-fix:./src/model/offline_vm/hotplug_usb.py \
           4.2.0-vgt_passthough:./src-core/model/offline_vm/hotplug_usb.py
$ git pull --rebase origin master

git lfs

apt-get install git-lfs
git lfs clone <repo>

git submodule

git submodule add url path/to/name
git submodule add <repo> <dir>          # 添加 子模块
git submodule add -b tractor --force <url> path
                                        # 删除 子模块


git submodule init                      # 初始化子模块
git submodule status                    # 查看子模块
git submodule update                    #

##########################
#    跟新,子模块
##########################
git submodule foreach git pull origin master # 拉取所有子模块
git submodule sync                      #
git add .                               #
git push origin master                  #


git clone <repository> --recursive      # 递归的方式克隆整个项目
git submodule update --init --recursive     # 更新子模块
git submodule update --recursive --remote   # 更新子模块

git tag

# 打TAG也就是发布版本
$ git tag -a v1.4 -m "version 1.4"
$ git show v1.4   # 查看
$
$ git push --tags
$ git push my_origin --tags

$ git tag -d  tag_name

如果还不能理解可以到这里看看是linus是怎么给Linux内核打的TAG,TAG看起来像什么:https://github.com/torvalds/linux/releases

git patch

########################################
# 当前分支,打patch
########################################
# 1、生成patch(在代码修改后没有commit之前的状态执行,进入想生成patch的目录即可)
git diff > project.patch
# 2、打patch,方法和linux的diff一样
patch -p0 < project.patch
git apply project.patch  //也可用这个命令打patch

###################################################
# 不同分支,不同文件,打pacth,
# 将 my_dev 对比 master的修改,打成path
###################################################
git diff master my_dev -- src/view/setting/other_seting.py > other_seting.py.pacth

# 此时branch 位于 master
git apply other_seting.py.pacth

打包发布

git archive --format zip --output /path/to/file.zip master # 将 master 以zip格式打包到指定文件

Send Mail

$ sudo apt-get install -y git-email

# config
$ git config --local user.email "jiang_xmin@massclouds.com"
$ git config --local user.name  "jiang_xmin"
$ git config --local sendemail.smtpserver     "smtp.qiye.163.com"
$ git config --local sendemail.smtpencryption "tls"
$ git config --local sendemail.smtpuser        "jiang_xmin@massclouds.com"
$ git config --local sendemail.smtpserverport "25"
# git config --local sendemail.smtppass       'passwd'

$ git config --local sendemail.annotate     true
$ git config --local sendemail.chainreplyto true
$ git config --local sendemail.confirm      always
$ git config --local sendemail.to jxm_zn@163.com  # sendo to

# send
$ git send-email --no-chain-reply-to --annotate --confirm=always --to=jxm_zn@163.com  master -1

Github

# 未加速
git clone https://github.com/Autoware-AI/autoware.ai.git

# 加速
git clone https://github.com.cnpmjs.org/Autoware-AI/autoware.ai.git
git clone https://hub.fastgit.org/Autoware-AI/autoware.ai.git


git clone
git clone https://ghproxy.com/https://github.com/stilleshan/ServerStatus

wget & curl
wget https://ghproxy.com/https://github.com/stilleshan/ServerStatus/archive/master.zip
wget https://ghproxy.com/https://raw.githubusercontent.com/stilleshan/ServerStatus/master/Dockerfile
curl -O https://ghproxy.com/https://github.com/stilleshan/ServerStatus/archive/master.zip
curl -O https://ghproxy.com/https://raw.githubusercontent.com/stilleshan/ServerStatus/master/Dockerfile
sudo gedit /etc/hosts

添加

151.101.185.194 http://github.global.ssl.fastly.net
192.30.253.112  http://github.com

保存,退出,并重启网络

/etc/init.d/networking restart

Gitlab

搭建Gitlab

docker run --name='gitlab-ce' -d \
      -p 10022:22 -p 10080:80 \
      --restart always \
      --volume /data/gitlab/config:/etc/gitlab \
      --volume /data/gitlab/logs:/var/log/gitlab \
      --volume /data/gitlab/data:/var/opt/gitlab \
      gitlab/gitlab-ce

sudo docker run --detach \
    --hostname gitlab.example.com \
    --env GITLAB_OMNIBUS_CONFIG="external_url 'http://192.168.8.100:10080'; gitlab_rails['lfs_enabled'] = true;" \
    --publish 10443:443 --publish 10080:80 --publish 10022:22 \
    --name gitlab \
    --restart always \
    --volume /srv/gitlab/config:/etc/gitlab \
    --volume /srv/gitlab/logs:/var/log/gitlab \
    --volume /srv/gitlab/data:/var/opt/gitlab \
    gitlab/gitlab-ce:10.2.4-ce.0


# 更改物理机主机,sshd端口
sudo sed -i 's/#Port\ 22/Port\ 1022/g' /etc/ssh/sshd_config
sudo shutdown -r now

#
sudo docker run --detach \
   --hostname "192.168.1.100" \
   --env GITLAB_OMNIBUS_CONFIG="gitlab_rails['lfs_enabled'] = true;" \
   --publish 10443:443 --publish 80:80 --publish 22:22 \
   --name gitlab \
   --restart always \
   --volume /srv/gitlab/config:/etc/gitlab \
   --volume /srv/gitlab/logs:/var/log/gitlab \
   --volume /srv/gitlab/data:/var/opt/gitlab \
  gitlab/gitlab-ee:13.9.1-ee.0
# /srv/gitlab/config/gitlab.rb
pages_external_url "http://192.168.1.100/"
gitlab_pages['enable'] = true

搭建Gitlab-CI 持续集成

快速使用

1. Install gitlab-runner

docker run -d --name gitlab-runner --restart always \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

# or
docker run -d --name gitlab-runner --restart always \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

2. Registering Runners

$ docker exec -it gitlab-runner gitlab-runner register

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://gitlab.com

Please enter the gitlab-ci token for this runner
xxx

Please enter the gitlab-ci description for this runner
[hostame] my-runner

Please enter the gitlab-ci tags for this runner (comma separated):
my-tag,another-tag

Whether to run untagged jobs [true/false]:
[false]: true

Whether to lock Runner to current project [true/false]:
[true]: false

Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
docker

Please enter the Docker image (eg. ruby:2.1):
alpine:latest
$ sudo docker exec -it gitlab-public-runner gitlab-runner register -n \
        --url "https://gitlab.com/" \
        --registration-token "REGISTRATION_TOKEN" \
        --executor docker \
        --description 2.3-docker-gitlab-runner  \
        --docker-image ubuntu:14.04  \
        --docker-privileged true\
        --locked false \
        --run-untagged true \
        --tag-list public-runner,another-tag

# add volumes
$ sudo docker exec -it gitlab-public-runner gitlab-runner register -n \
        --url https://gitlab.com/ \
        --registration-token REGISTRATION_TOKEN \
        --executor docker \
        --description "My Docker Runner" \
        --docker-image "docker:latest" \
        --docker-volumes /var/run/docker.sock:/var/run/docker.sock

3. gitlab-ci.yml

gitlab-ci 环境变量
  • CI_REPOSITORY_URL
  • CI_PIPELINE_URL
  • CI_PROJECT_URL : Project url
  • CI_SERVER_NAME
  • CI_COMMIT_REF_NAME : 分支名
$ echo "$CI_REPOSITORY_URL"
http://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@172.19.2.57/jiang_xmin/gitlib-ci_test.git
$ echo "$CI_PIPELINE_URL"
http://172.19.2.57/jiang_xmin/gitlib-ci_test/pipelines/149
$ echo "$CI_PROJECT_URL"
http://172.19.2.57/jiang_xmin/gitlib-ci_test
$ echo "$CI_SERVER"
yes
$ echo "$CI_SERVER_NAME"
GitLab
$ echo "$CI_COMMIT_REF_NAME"
dev
$ IP=`echo "$CI_PROJECT_URL" | sed "s/^http:\/\///"| sed "s/\/.*$//"`
$ echo "$IP"
172.19.2.57

4. gitlab-runner 常用命令

$ sudo gitlab-runner register
$ sudo gitlab-runner unregister --name "name"
$ sudo gitlab-runner list
$ sudo gitlab-runner verify

Triggering pipelines through the API

curl -X POST \
     -F token=c180975fb840ba2b5c942347a58f90 \
     -F ref=master \
     http://192.168.1.102/api/v4/projects/7/trigger/pipeline

downloading-the-latest-artifacts

# pirvite  project
curl -L  --header "PRIVATE-TOKEN: nDU2GenxaCiNouREB91n"  \
    "http://192.168.1.102/jiang_xmin/mc-terminal/-/jobs/artifacts/4.2.0-dev/download?job=job1" \
    -o artifacts.zip

# public project
curl -L -o artifacts.zip \
"http://192.168.1.102/jiang_xmin/mc-terminal/-/jobs/artifacts/4.2.0-dev/download?job=job1"

wget -O artifacts.zip  \
http://192.168.1.102/jiang_xmin/mc-terminal/-/jobs/artifacts/4.2.0-dev/download?job=job1

####
curl -L -o mcstudent_offline_4.2.1-11-g54157f0_amd64.deb \
http://192.168.1.102/jiang_xmin/mc-terminal/-/jobs/artifacts/4.2.0-dev/raw/BUILD/mcstudent_offline_4.2.1-11-g54157f0_amd64.deb?job=job1

wget -O mcstudent_offline_4.2.1-11-g54157f0_amd64.deb \
http://192.168.1.102/jiang_xmin/mc-terminal/-/jobs/artifacts/4.2.0-dev/raw/BUILD/mcstudent_offline_4.2.1-11-g54157f0_amd64.deb?job=job1

Advanced

$ sudo gitlab-runner register
$ sudo gitlab-runner register -c "$HOME/.gitlab-runner/config.toml"

$ sudo gitlab-runner unregister --name "name"
$ sudo gitlab-runner list
$ sudo gitlab-runner verify

FAQ

# 关闭蓝灯后,push失败
connect to 127.0.0.1 port 38897: Connection refused

# 查看
$ env | grep -i proxy

# 清空环境变量
$ export http_proxy=""
$ export https_proxy=""
$ export HTTP_PROXY=""
$ export HTTPS_PROXY=""
How to remove submodule

1. Delete the relevant line from the .gitmodules file.
2. Delete the relevant section from .git/config.
3. Run git rm --cached path_to_submodule (no trailing slash).

4. Remove directory .git/modules/<submodule name>
! [remote rejected] master -> master (shallow update not allowed)

$ git fetch --unshallow
$ git fetch --unshallow origin

与村下作诗一首



有酒的日子就得死 喝一次死一次

我在看那叶子死 那风死

在看我死以后

我死以后

就让那叶子点燃我 让那风把我骨灰吹进你眼里

你再也看不见我了

你梦里梦见我 梦看清了我

那是我老了的样子 我也知道你就是个婊子

可是我真想你啊 你赤裸着给我跳舞

给我看生活的高潮 闻死亡的味道



你啊你

你听我喝下这酒 燃起这烟

听我打了个嗝 听我决定要去流浪 听这世间的繁华不过尔尔

你啊你

你把眼睛换成星星 把身体换成我走不完的路程

用你的一生置换我的一生

鲜血淋漓在我的双脚上

荆棘的不是路 而是我自己啊

你啊 我想你定和这荆棘有所关联

否则 我怎会痛不欲生

烟戳在心窝上 不如你疼



故事开始在我死以后

我死以后 清晨的太阳开始复活

在山头唱首悲歌 唱你安宁地死在牢笼

你被野狼啮去了肉 被鹰啄去了头 被虎剔去了骨 你的灵魂开始在大地铺陈

在岸边谱曲挽歌 谱渔船撞上西边日落

水鸟立在岸边歌唱 俯视水下沉船 俯视水里埋葬的夕阳

在灯塔流浪的一生哼成调 千山万水的温情游弋喉咙里

那些在夜晚闪过的光 在白日停留的人 化成杯里的苦酒 久久沉醉

杯酒换盏 人去几回 一曲未落 一曲又起 我死在万千故事里

后记(村下南北):

我爱的不是酒,但我总跟在所有人都醉后才开始醉,可能这有点矫情。 这是一个与诗与烟相关的夜晚,自然
酒是少不了的。今日,是一个叫做向死而生的生辰。我们总活在夜里,总在一口酒和一口烟之间念叨出零碎的
诗,也许诗不够好,但却是相隔千万里的两个人所诞生的“孩子”。我们都有千言万语,可脱口而出的总是“你
这个傻逼”。

祝你生日快乐,也祝你能在往后的艰辛苦楚里快乐,干杯。

后记(向死而生):

我不是个好人,我们都不是什么好人。我们有什么可耻之处自己最清楚。我们变得完美的时候,就在握紧酒
瓶的时候。我们所有苟且不堪的日子,那些所有遗忘了自己的人们,都被一气喝下,然后变成腥骚的尿。
(村下,走一个)我不知道还能活多少年,不知道将来的日子里,还会丢失多少朋友。还好,时间永远是现
在,那些过去的和未来的…………都在酒里,干杯。

诗歌,出自邻居的耳朵,邻居的耳朵网站已于2018年2月28日正式关停