Subsections of 🐸Git

Cheatsheet

Init global config

git config --list
git config --global user.name "AaronYang"
git config --global user.email aaron19940628@gmail.com
git config --global pager.branch false
git config --global pull.ff only
git --no-pager diff

change user and email (locally)

git config user.name ""
git config user.email ""

list all remote repo

git remote -v
modify remote repo
git remote set-url origin git@github.com:<$user>/<$repo>.git

Get specific file from remote

git archive --remote=git@github.com:<$user>/<$repo>.git <$branch>:<$source_file_path> -o <$target_source_path>
for example
git archive --remote=git@github.com:AaronYang2333/LOL_Overlay_Assistant_Tool.git master:paper/2003.11755.pdf -o a.pdf

Clone specific branch

git clone -b slurm-23.02 --single-branch --depth=1 https://github.com/SchedMD/slurm.git

Update submodule

git submodule add –depth 1 https://github.com/xxx/xxxx a/b/c

git submodule update --init --recursive

Save credential

login first and then execute this

git config --global credential.helper store

Delete Branch

  • Deleting a remote branch
    git push origin --delete <branch>  # Git version 1.7.0 or newer
    git push origin -d <branch>        # Shorter version (Git 1.7.0 or newer)
    git push origin :<branch>          # Git versions older than 1.7.0
  • Deleting a local branch
    git branch --delete <branch>
    git branch -d <branch> # Shorter version
    git branch -D <branch> # Force-delete un-merged branches

Prune remote branches

git remote prune origin

Add a new remote repo

git remote add dev https://xxxxxxxxxxx.git

Update remote repo

git remote set-url origin http://xxxxx.git
Mar 7, 2024

Subsections of Action

Customize A Gitea Action

Introduction

In this guide, you’ll learn about the basic components needed to create and use a packaged composite action. To focus this guide on the components needed to package the action, the functionality of the action’s code is minimal. The action prints “Hello World” and then “Goodbye”, or if you provide a custom name, it prints “Hello [who-to-greet]” and then “Goodbye”. The action also maps a random number to the random-number output variable, and runs a script named goodbye.sh.

Once you complete this project, you should understand how to build your own composite action and test it in a workflow.

Warning

When creating workflows and actions, you should always consider whether your code might execute untrusted input from possible attackers. Certain contexts should be treated as untrusted input, as an attacker could insert their own malicious content. For more information, see Secure use reference.

Composite actions and reusable workflows

Composite actions allow you to collect a series of workflow job steps into a single action which you can then run as a single job step in multiple workflows. Reusable workflows provide another way of avoiding duplication, by allowing you to run a complete workflow from within other workflows. For more information, see Reusing workflow configurations.

Prerequisites

Note

This example explains how to create a composite action within a separate repository. However, it is possible to create a composite action within the same repository. For more information, see Creating a composite action.

Before you begin, you’ll create a repository on GitHub.

  1. Create a new public repository on GitHub. You can choose any repository name, or use the following hello-world-composite-action example. You can add these files after your project has been pushed to GitHub.

  2. Clone your repository to your computer.

  3. From your terminal, change directories into your new repository.

cd hello-world-composite-action
  1. In the hello-world-composite-action repository, create a new file called goodbye.sh with example code:
echo "echo Goodbye" > goodbye.sh
  1. From your terminal, make goodbye.sh executable.
chmod +x goodbye.sh
  1. From your terminal, check in your goodbye.sh file.
git add goodbye.sh
git commit -m "Add goodbye script"
git push

Creating an action metadata file

  1. In the hello-world-composite-action repository, create a new file called action.yml and add the following example code. For more information about this syntax, see Metadata syntax reference.
name: 'Hello World'
description: 'Greet someone'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  random-number:
    description: "Random number"
    value: ${{ steps.random-number-generator.outputs.random-number }}
runs:
  using: "composite"
  steps:
    - name: Set Greeting
      run: echo "Hello $INPUT_WHO_TO_GREET."
      shell: bash
      env:
        INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}

    - name: Random Number Generator
      id: random-number-generator
      run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
      shell: bash

    - name: Set GitHub Path
      run: echo "$GITHUB_ACTION_PATH" >> $GITHUB_PATH
      shell: bash
      env:
        GITHUB_ACTION_PATH: ${{ github.action_path }}

    - name: Run goodbye.sh
      run: goodbye.sh
      shell: bash

This file defines the who-to-greet input, maps the random generated number to the random-number output variable, adds the action’s path to the runner system path (to locate the goodbye.sh script during execution), and runs the goodbye.sh script.

For more information about managing outputs, see Metadata syntax reference.

For more information about how to use github.action_path, see Contexts reference.

  1. From your terminal, check in your action.yml file.
git add action.yml
git commit -m "Add action"
git push
  1. From your terminal, add a tag. This example uses a tag called v1. For more information, see About custom actions.
git tag -a -m "Description of this release" v1
git push --follow-tags

Testing out your action in a workflow

The following workflow code uses the completed hello world action that you made in Creating a composite action.

Copy the workflow code into a .github/workflows/main.yml file in another repository, replacing OWNER and SHA with the repository owner and the SHA of the commit you want to use, respectively. You can also replace the who-to-greet input with your name.

on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - uses: actions/checkout@v5
      - id: foo
        uses: OWNER/hello-world-composite-action@SHA
        with:
          who-to-greet: 'Mona the Octocat'
      - run: echo random-number "$RANDOM_NUMBER"
        shell: bash
        env:
          RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}

From your repository, click the Actions tab, and select the latest workflow run. The output should include: “Hello Mona the Octocat”, the result of the “Goodbye” script, and a random number.

Creating a composite action within the same repository

  1. Create a new subfolder called hello-world-composite-action, this can be placed in any subfolder within the repository. However, it is recommended that this be placed in the .github/actions subfolder to make organization easier.

  2. In the hello-world-composite-action folder, do the same steps to create the goodbye.sh script

echo "echo Goodbye" > goodbye.sh
chmod +x goodbye.sh
git add goodbye.sh
git commit -m "Add goodbye script"
git push
  1. In the hello-world-composite-action folder, create the action.yml file based on the steps in Creating a composite action.

  2. When using the action, use the relative path to the folder where the composite action’s action.yml file is located in the uses key. The below example assumes it is in the .github/actions/hello-world-composite-action folder.

on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - uses: actions/checkout@v5
      - id: foo
        uses: ./.github/actions/hello-world-composite-action
        with:
          who-to-greet: 'Mona the Octocat'
      - run: echo random-number "$RANDOM_NUMBER"
        shell: bash
        env:
          RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}
Mar 7, 2024

Customize A Github Action

Introduction

In this guide, you’ll learn about the basic components needed to create and use a packaged composite action. To focus this guide on the components needed to package the action, the functionality of the action’s code is minimal. The action prints “Hello World” and then “Goodbye”, or if you provide a custom name, it prints “Hello [who-to-greet]” and then “Goodbye”. The action also maps a random number to the random-number output variable, and runs a script named goodbye.sh.

Once you complete this project, you should understand how to build your own composite action and test it in a workflow.

Warning

When creating workflows and actions, you should always consider whether your code might execute untrusted input from possible attackers. Certain contexts should be treated as untrusted input, as an attacker could insert their own malicious content. For more information, see Secure use reference.

Composite actions and reusable workflows

Composite actions allow you to collect a series of workflow job steps into a single action which you can then run as a single job step in multiple workflows. Reusable workflows provide another way of avoiding duplication, by allowing you to run a complete workflow from within other workflows. For more information, see Reusing workflow configurations.

Prerequisites

Note

This example explains how to create a composite action within a separate repository. However, it is possible to create a composite action within the same repository. For more information, see Creating a composite action.

Before you begin, you’ll create a repository on GitHub.

  1. Create a new public repository on GitHub. You can choose any repository name, or use the following hello-world-composite-action example. You can add these files after your project has been pushed to GitHub.

  2. Clone your repository to your computer.

  3. From your terminal, change directories into your new repository.

cd hello-world-composite-action
  1. In the hello-world-composite-action repository, create a new file called goodbye.sh with example code:
echo "echo Goodbye" > goodbye.sh
  1. From your terminal, make goodbye.sh executable.
chmod +x goodbye.sh
  1. From your terminal, check in your goodbye.sh file.
git add goodbye.sh
git commit -m "Add goodbye script"
git push

Creating an action metadata file

  1. In the hello-world-composite-action repository, create a new file called action.yml and add the following example code. For more information about this syntax, see Metadata syntax reference.
name: 'Hello World'
description: 'Greet someone'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  random-number:
    description: "Random number"
    value: ${{ steps.random-number-generator.outputs.random-number }}
runs:
  using: "composite"
  steps:
    - name: Set Greeting
      run: echo "Hello $INPUT_WHO_TO_GREET."
      shell: bash
      env:
        INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}

    - name: Random Number Generator
      id: random-number-generator
      run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
      shell: bash

    - name: Set GitHub Path
      run: echo "$GITHUB_ACTION_PATH" >> $GITHUB_PATH
      shell: bash
      env:
        GITHUB_ACTION_PATH: ${{ github.action_path }}

    - name: Run goodbye.sh
      run: goodbye.sh
      shell: bash

This file defines the who-to-greet input, maps the random generated number to the random-number output variable, adds the action’s path to the runner system path (to locate the goodbye.sh script during execution), and runs the goodbye.sh script.

For more information about managing outputs, see Metadata syntax reference.

For more information about how to use github.action_path, see Contexts reference.

  1. From your terminal, check in your action.yml file.
git add action.yml
git commit -m "Add action"
git push
  1. From your terminal, add a tag. This example uses a tag called v1. For more information, see About custom actions.
git tag -a -m "Description of this release" v1
git push --follow-tags

Testing out your action in a workflow

The following workflow code uses the completed hello world action that you made in Creating a composite action.

Copy the workflow code into a .github/workflows/main.yml file in another repository, replacing OWNER and SHA with the repository owner and the SHA of the commit you want to use, respectively. You can also replace the who-to-greet input with your name.

on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - uses: actions/checkout@v5
      - id: foo
        uses: OWNER/hello-world-composite-action@SHA
        with:
          who-to-greet: 'Mona the Octocat'
      - run: echo random-number "$RANDOM_NUMBER"
        shell: bash
        env:
          RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}

From your repository, click the Actions tab, and select the latest workflow run. The output should include: “Hello Mona the Octocat”, the result of the “Goodbye” script, and a random number.

Creating a composite action within the same repository

  1. Create a new subfolder called hello-world-composite-action, this can be placed in any subfolder within the repository. However, it is recommended that this be placed in the .github/actions subfolder to make organization easier.

  2. In the hello-world-composite-action folder, do the same steps to create the goodbye.sh script

echo "echo Goodbye" > goodbye.sh
chmod +x goodbye.sh
git add goodbye.sh
git commit -m "Add goodbye script"
git push
  1. In the hello-world-composite-action folder, create the action.yml file based on the steps in Creating a composite action.

  2. When using the action, use the relative path to the folder where the composite action’s action.yml file is located in the uses key. The below example assumes it is in the .github/actions/hello-world-composite-action folder.

on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - uses: actions/checkout@v5
      - id: foo
        uses: ./.github/actions/hello-world-composite-action
        with:
          who-to-greet: 'Mona the Octocat'
      - run: echo random-number "$RANDOM_NUMBER"
        shell: bash
        env:
          RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}
Mar 7, 2024

Gitea Variables

Preset Variables

变量名称示例说明 / 用途
gitea.actor触发 workflow 的用户的用户名。(docs.gitea.com)
gitea.event_name事件名称,比如 pushpull_request 等。(docs.gitea.com)
gitea.ref被触发的 Git 引用(branch/tag/ref)名称。(docs.gitea.com)
gitea.repository仓库标识,一般是 owner/name。(docs.gitea.com)
gitea.workspace仓库被 checkout 到 runner 上的工作目录路径。(docs.gitea.com)

Common Variables

变量名称示例说明 / 用途
runner.osRunner 所在的操作系统环境,比如 ubuntu-latest。(docs.gitea.com)
job.status当前 job 的状态(例如 success 或 failure)。(docs.gitea.com)
env.xxxx自定义配置变量,在用户/组织/仓库层定义,统一以大写形式引用。(docs.gitea.com)
secrets.XXXX存放敏感信息的密钥,同样可以在用户/组织/仓库层定义。(docs.gitea.com)

Sample

name: Gitea Actions Demo
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
on: [push]

env:
    author: gitea_admin
jobs:
  Explore-Gitea-Actions:
    runs-on: ubuntu-latest
    steps:
      - run: echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event."
      - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!"
      - run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}."
      - name: Check out repository code
        uses: actions/checkout@v4
      - run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner."
      - run: echo "🖥️ The workflow is now ready to test your code on the runner."
      - name: List files in the repository
        run: |
          ls ${{ gitea.workspace }}
      - run: echo "🍏 This job's status is ${{ job.status }}."

Result

🎉 The job was automatically triggered by a `push` event.

🐧 This job is now running on a `Linux` server hosted by Gitea!

🔎 The name of your branch is `refs/heads/main` and your repository is `gitea_admin/data-warehouse`.

💡 The `gitea_admin/data-warehouse` repository has been cloned to the runner.

🖥️ The workflow is now ready to test your code on the runner.

    Dockerfile  README.md  environments  pom.xml  src  templates

🍏 This job's status is `success`.
Mar 7, 2024

Github Variables

Context Variables

变量名称示例说明 / 用途
github.actor触发 workflow 的用户的用户名。([docs.gitea.com][1])
github.event_name事件名称,比如 pushpull_request 等。([docs.gitea.com][1])
github.ref被触发的 Git 引用(branch/tag/ref)名称。([docs.gitea.com][1])
github.repository仓库标识,一般是 owner/name。([docs.gitea.com][1])
github.workspace仓库被 checkout 到 runner 上的工作目录路径。([docs.gitea.com][1])
env.xxxx在workflow中定义的变量,比如 ${{ env.xxxx }}
secrets.XXXX通过 Settings -> Actions -> Secrets and variables 创建的密钥。
Mar 7, 2024

Subsections of Template

Apply And Sync Argocd APP

name: apply-and-sync-app
run-name: ${{ gitea.actor }} is going to sync an sample argocd app 🚀
on: [push]

jobs:
  sync-argocd-app:
    runs-on: ubuntu-latest
    steps:
      - name: Sync App
        uses: AaronYang0628/apply-and-sync-argocd@v1.0.6
        with:
          argocd-server: '192.168.100.125:30443'
          argocd-token: ${{ secrets.ARGOCD_TOKEN }}
          application-yaml-path: "environments/ops/argocd/operator.app.yaml"
Mar 7, 2025

Publish Chart 2 Harbor

name: publish-chart-to-harbor-registry
run-name: ${{ gitea.actor }} is testing out Gitea Push Chart 🚀
on: [push]

env:
  REGISTRY: harbor.zhejianglab.com
  USER: byang628@zhejianglab.com
  REPOSITORY_NAMESPACE: ay-dev
  CHART_NAME: data-warehouse
jobs:
  build-and-push-charts:
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
    strategy:
      matrix:
        include:
          - chart_path: "environments/helm/metadata-environment"
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Log in to Harbor
        uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
        with:
          registry: "${{ env.REGISTRY }}"
          username: "${{ env.USER }}"
          password: "${{ secrets.ZJ_HARBOR_TOKEN }}"

      - name: Helm Publish Action
        uses: AaronYang0628/push-helm-chart-to-oci@v0.0.3
        with:
          working-dir: ${{ matrix.chart_path }}
          oci-repository: oci://${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}
          username: ${{ env.USER }}
          password: ${{ secrets.ZJ_HARBOR_TOKEN }}
Mar 7, 2025

Publish Image 2 Dockerhub

name: publish-image-to-ghcr
run-name: ${{ gitea.actor }} is testing out Gitea Push Image 🚀
on: [push]

env:
  REGISTRY: ghcr.io
  USER: aaronyang0628
  REPOSITORY_NAMESPACE: aaronyang0628
jobs:
  build-and-push-images:
    strategy:
      matrix:
        include:
          - name_suffix: "aria-ng"
            container_path: "application/aria2/container/aria-ng"
            dockerfile_path: "application/aria2/container/aria-ng/Dockerfile"
          - name_suffix: "aria2"
            container_path: "application/aria2/container/aria2"
            dockerfile_path: "application/aria2/container/aria2/Dockerfile"
    runs-on: ubuntu-latest
    steps:
    - name: checkout-repository
      uses: actions/checkout@v4
    - name: log in to the container registry
      uses: docker/login-action@v3
      with:
        registry: "${{ env.REGISTRY }}"
        username: "${{ env.USER }}"
        password: "${{ secrets.GIT_REGISTRY_PWD }}"
    - name: build and push container image
      uses: docker/build-push-action@v6
      with:
        context: "${{ matrix.container_path }}"
        file: "${{ matrix.dockerfile_path }}"
        push: true
        tags: |
          ${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}/${{ github.repository }}-${{ matrix.name_suffix }}:${{ inputs.tag || 'latest' }}
          ${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}/${{ github.repository }}-${{ matrix.name_suffix }}:${{ github.ref_name }}
        labels: |
          org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
Mar 7, 2025

Publish Image 2 Ghcr

name: publish-image-to-ghcr
run-name: ${{ gitea.actor }} is testing out Gitea Push Image 🚀
on: [push]

env:
  REGISTRY: ghcr.io
  USER: aaronyang0628
  REPOSITORY_NAMESPACE: aaronyang0628
jobs:
  build-and-push-images:
    strategy:
      matrix:
        include:
          - name_suffix: "aria-ng"
            container_path: "application/aria2/container/aria-ng"
            dockerfile_path: "application/aria2/container/aria-ng/Dockerfile"
          - name_suffix: "aria2"
            container_path: "application/aria2/container/aria2"
            dockerfile_path: "application/aria2/container/aria2/Dockerfile"
    runs-on: ubuntu-latest
    steps:
    - name: checkout-repository
      uses: actions/checkout@v4
    - name: log in to the container registry
      uses: docker/login-action@v3
      with:
        registry: "${{ env.REGISTRY }}"
        username: "${{ env.USER }}"
        password: "${{ secrets.GIT_REGISTRY_PWD }}"
    - name: build and push container image
      uses: docker/build-push-action@v6
      with:
        context: "${{ matrix.container_path }}"
        file: "${{ matrix.dockerfile_path }}"
        push: true
        tags: |
          ${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}/${{ github.repository }}-${{ matrix.name_suffix }}:${{ inputs.tag || 'latest' }}
          ${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}/${{ github.repository }}-${{ matrix.name_suffix }}:${{ github.ref_name }}
        labels: |
          org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
Mar 7, 2025

Publish Image 2 Harbor

name: publish-image-to-harbor-registry
run-name: ${{ gitea.actor }} is testing out Gitea Push Image 🚀
on: [push]


env:
  REGISTRY: harbor.zhejianglab.com
  USER: byang628@zhejianglab.com
  REPOSITORY_NAMESPACE: ay-dev
  IMAGE_NAME: metadata-crd-operator
jobs:
  build-and-push-images:
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
    strategy:
      matrix:
        include:
          - name_suffix: "dev"
            container_path: "."
            dockerfile_path: "./Dockerfile"
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Log in to Harbor
        uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
        with:
          registry: "${{ env.REGISTRY }}"
          username: "${{ env.USER }}"
          password: "${{ secrets.ZJ_HARBOR_TOKEN }}"

      - name: Extract Current Date
        id: extract-date
        run: |
          echo "current-date=$(date +'%Y%m%d')" >> $GITHUB_OUTPUT
          echo will push image: ${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}/${{ env.IMAGE_NAME }}-${{ matrix.name_suffix }}:v${{ steps.extract-date.outputs.current-date }}

      - name: Build And Push Container Image
        uses: docker/build-push-action@v6
        with:
          context: "${{ matrix.container_path }}"
          file: "${{ matrix.dockerfile_path }}"
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}/${{ env.IMAGE_NAME }}-${{ matrix.name_suffix }}:v${{ steps.extract-date.outputs.current-date }}
          labels: |
            org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
Mar 7, 2025

Subsections of Notes

Not Allow Push

Cannot push to your own branch

mvc mvc

  1. Edit .git/config file under your repo directory.

  2. Find url=entry under section [remote "origin"].

  3. Change it from:

    url=https://gitlab.com/AaronYang2333/ska-src-dm-local-data-preparer.git/

    url=ssh://git@gitlab.com/AaronYang2333/ska-src-dm-local-data-preparer.git

  4. try push again