Skip to content

Git

Bus

  • new git reposity: git init
  • clone reposity:

    git clone /path/to/repository # 创建本地仓库克隆版本
    git clone username@host:/path/to/repository  # 克隆远端参考
    

  • add and commit:

    git add <filename>
    git add *  # or
    git commit -m "message"  # 提交改动到HEAD
    

  • push:

git push origin master  # push to 远端
git remote add origin <server>  # 连接本地仓库到远程服务器
  • branch:

    git checkout -b feature_x  # 创建分支,并切换过去
    git checkout master        # 切换回主分支
    git branch -d feature_x    # 删除分支
    git push origin <branch>   # 推动分支到远端
    

  • update and merge:

    git pull  # 更新本地: = >fetch > merge
    git merge <branch>  # 合并分支
    # if 冲突(conflict), 修改并提交
    git add <filename>
    git diff <source_branch> <target_branch>  # 查看改动
    

  • tag:

    git tag 1.0.0 1b2e1d63ff # 1b2e1d63ff 是你想要标记的提交 ID 的前 10 位字符。
    git log  # 获取提交ID
    

  • replace local:

    git checkout -- <filename>  # 最新Head替换work dir中的文件,已在缓存区改动以及新文件不受影响。
    
    git fetch origin
    git reset --hard origin/master  # 丢弃所有本地改动和提交
    

  • useful tips:

    gitk # builtin 图形化
    git config color.ui true  # 彩色git输出。
    git config format.pretty oneline  # 显示历史记录时,只显示一行注释信息
    git add -i  # 交互添加文件到缓存区
    git diff --name-only --diff-filter=U  # 展示conflict的文件名字。
    

  • url-check:

    git remote -v
    
    git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git
    

  • list tags:

    git tag -l --format="%(refname)   %(creatordate)"
    # refs/tags/2.0.0   Wed Nov 17 14:38:45 2021 +0800
    git tag -l --format="%(refname:short)   %(creatordate:short)"
    # 1.2.5   2021-11-15
    git tag -l --format="%(refname:short)   %(creatordate:iso)"
    # 1.2.5   2021-11-15 18:15:26 +0800
    git tag -l --format="%(refname:short)   %(creatordate:iso-strict)"
    # 3.0.4   2022-06-01T09:40:02+08:00
    git tag -l --format="%(refname:short)   %(creatordate:relative)"
    # 3.0.3   2 weeks ago
    

  • proxy:

    git config --global http.proxy http://127.0.0.1:33210
    
    git config --global http.https://domain.com.proxy http://proxyUsername:proxyPassword@proxy.server.com:port
    git config --global http.https://domain.com.sslVerify false
    

Commands

  • delete local tag(删除本地标签)
git tag -d <tag_name>
# git tag -d v2.0
  • delete remote tag(删除远程标签)

    git push --delete origin tagname
    # git push --delete origin v1.0
    

  • save user and password(保存用户密码)

    git config --global credential.helper store
    git pull
    

  • change branch name(修改分支名称)

git branch -m <new-name>
  • delete branch(删除分支)
$ git push -d <remote_name> <branch_name>
$ git branch -d <branch_name>

$ git push -d origin <branch_name>
  • change committed msg(修改提交信息)
$ git log
$ git commit --amend
  • clone and build branch(拉取并新建分支)
$ git clone -b master https://github.com/ampache/ampache.git ampache
  • git global setup(设置全局配置)
git config --global user.name "dean"
git config --global user.email "dean@**"
  • create new repository(新建仓库)
git clone <http:host.git>
cd <Folder>
touch READEME.md
git add READEME.md
git commit -m "add readme"
git push -u origin master
  • existing folder(已有项目)
git init
# to remote
git remote add origin <url>
git add .
git commit -m "Initial commit"
git push -u origin master
  • existing git repository(已有git仓库)
cd <Folder>
git remote rename origin old-origin
git remote add origin <url>
git push -u origin --all
git push -u origin --tags
  • show logs(查看最近修改日志信息)
    git log --name-status 每次修改的文件列表, 显示状态
    git log --name-only 每次修改的文件列表
    git log --stat 每次修改的文件列表, 及文件修改的统计
    git whatchanged 每次修改的文件列表
    git whatchanged --stat 每次修改的文件列表, 及文件修改的统计
    git show 显示最后一次的文件改变的具体内容
    

github actions

  • Generate an SSH Key
cd ~/.ssh

# empty password, and name recommand(github-action)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# or
ssh-keygen -t ed25519 -a 200 -C "your_email@example.com"
  • Adding the Public Key to authorized_keys
cat github-actions.pub >> authorized_keys
# Double means append, while single means overwrite.
cat .ssh/id_ed25519.pub | ssh b@B 'cat >> .ssh/authorized_keys'
  • Adding the private key to your repository’s secrets
clip < ~/.ssh/id_ed25519
# go to github actions.
Settings => Secrets => New repository secret.
  • Adding the Private key to a Github Actions Workflow
name: CICD

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: sync ws code with git
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            cd ~/ws/dean-mk-docs
            git pull

  deploy:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: docker service restart
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            cd ~/ws/dean-mk-docs
            docker rm -f $(docker ps -aqf "name=dean-mkdocs")
            docker run --name "dean-mkdocs" -d -p 8000:8000 -v ${PWD}:/docs squidfunk/mkdocs-material

Reference

  1. git 简明教程