Git: Quick Reference Sheet
Some common and useful Git commands.
Checkout New Branch From Origin
git fetch origin my_branchgit pullgit checkout my_branch
View Branch Commit History
git log
View Entire Local Change History
git reflog
View File Changes
By SHA:
git diff --name-only af43c41d..HEADgit diff af43c41d..master
By branch:
git diff --name-only origin/deploy..mastergit diff origin/deploy..master
Correct Previous Commits
Review, alter, remove, amend last 3 commits:
git rebase -i HEAD~3- Type
ito enterinteractive mode. - Find the line with the desired commit hash. Modify it using
pick,drop, etc. - Hit the
escbutton to exitinteractive mode. - Type
wqto save and close the file (Git will proceed through the stipulated changes) or typeq!to close the file abandoning all changes. git push -fto override previous changes - do not use this onmaster/mainonly ever within a development branch.
Git Amend
Correct the last commit message:
git commit --amend -m "Your new message"
Discard Uncommitted Branch Charge
git clean -f -dgit reset --hard HEAD
Abandon a Rebase
git rebase --abortgit clean -f -dgit reset --hard HEAD
Change Branch W/ Same Name As Dir
If a branch contains a dir in the root with the same name as a branch, Git will now complain.
Use the following instead:
git fetch origin deploy(if freshly cloned)git switch -f deploy
Set Environment Config
git config --global user.username "my_username"git config --global user.email "my_email@email.com"
Disable automatic conversion of Unix line endings to Dos/Windows ones (on Windows):
git config --global core.autocrlf false- The above will prevent line ending replacement if Unix-compatible carriage-returns are set in the file.
- crlf -> Carriage Return, Line Feed
- A handy conversion tool.
Resources and Links
- https://devhints.io/vim
- https://git-scm.com/doc
- https://toolslick.com/conversion/text/dos-to-unix
- https://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf
- https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings#global-settings-for-line-endings
- https://jollygood.prose.sh/vim-in-git-for-windows
GitHub Actions: Overview
GitHub Actions (the tool itself) supports Workflow and CI/CD automation through GitHub.
Official Documentation: https://docs.github.com/en/actions/about-github-actions/understanding-github-actions
Very helpful exercises: https://learn.microsoft.com/en-us/collections/n5p4a5z7keznp5
GitHub Actions
GitHub Actions are packaged scripts to automate tasks through GitHub.
There are three kinds of GitHub Actions:
Container Actions - where a Linux Environment comprises part of the Action.
# Example name: "Hello Actions" description: "Greet someone" author: "octocat@github.com" inputs: MY_NAME: description: "Who to greet" required: true default: "World" runs: uses: "docker" image: "Dockerfile" branding: icon: "mic" color: "purple"JavaScript Actions - execute JavaScript as an Action.
name: 'Hello World' description: Simple example inputs: myinput: # id of input description: My input arg required: true default: "I am a string" outputs: myoutput: # id of output description: Output of the function runs: using: node20 main: script.jshttps://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-javascript-action
Composite Actions - combine multiple Workflow Steps together into one Action.
name: 'Hello World' description: 'Greet someone' inputs: who-to-greet: # id of input description: 'Who to greet' required: true default: 'World' 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: Run goodbye.sh run: goodbye.sh shell: bash
The above are characterized by having:
inputsand/oroutputsrunsandusing
GitHub Workflow
name: A workflow for my Hello World file
on: push
jobs:
build:
name: Hello world action
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: ./action-a
with:
MY_NAME: "Mona"
https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-composite-action
The Anatomy of a GitHub Action
Workflow > Job(s) > Step(s) > Action(s) defined in a YAML file.
- A Workflow defines one or more Jobs.
- A Job defines one or more Steps.
- A Job has an associated Runner that executes the Job.
- (Think Runnable or Callable in Java.)
- A Step defines one or more Actions.
- A Task with multiple commands.
- An Action is a discrete command.
- (Think
RUNin Docker.)
- (Think
GitHub Integration
Organizations and users typically integrate their GitHub Repositories with GitHub Actions:
- Define a
workflow.yamlfile in the root of some Source Code. - The Source Code is checked into a GitHub Repository.
- The GitHub Repository is associated with GitHub Secrets or any integrations through the GitHub User Interface.
Resources and Links
- https://docs.github.com/en/actions/about-github-actions/understanding-github-actions
- https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-docker-container-action
- https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-javascript-action
- https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-composite-action
- https://learn.microsoft.com/en-us/collections/n5p4a5z7keznp5
Code samples:
GitHub Actions: Advanced Topics
Triggers
Reference list for available trigger conditions: https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows.
Use like so:
name:
on:
issues:
types: [opened, edited, milestoned]
Combining Actions
GitHub Actions can also be combined or composed.
Typified by a uses YAML black:
name: my_example
on:
#...
jobs:
tag:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run test
run: |
pytest test.py
Environment Variables
Can define Environment Variables that can be used elsewhere in the Workflow:
#...
env:
AWS_REGION: MY_AWS_REGION
ECR_REPOSITORY: MY_ECR_REPOSITORY
ECS_SERVICE: MY_ECS_SERVICE
#...
Default Environment Variables:
- Are prefixed with
GITHUB_. - Defined by GitHub and not within a Workflow.
- Have an associated Context property.
Default Environment Variables: https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables
Advanced Expressions
GitHub Actions supports many complex Expressions, Operators, and Functions (as YAML keys or values, depending):
- Numeric Boolean:
<=,>=,==,!=, etc. - Literals:
${{ 'I''m a string and I need tic marks around me in here!' }},${{ -9.2 }} - Logical Boolean:
&&,||,!, etc. - YAML Conditional Boolean:
if: ${{ success() }}, etc. - String:
contains('Hello world', 'llo'), etc. - Parsing:
toJSON(value), etc. - Dynamic Variable Setting:
>>
Resources and Links
- https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/triggering-a-workflow
- https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows
- https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables
- https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/evaluate-expressions-in-workflows-and-actions
GitHub Actions: Integration
GitHub Secrets
GitHub Actions can integrate with GitHub Secrets to define any Secrets, Credentials, or Tokens required by the CI/CD or Workflow:
- These are defined in the GitHub User Interface available through Settings > Secrets and variables > Actions > Actions secrets and variables.
- e.g. -
https://github.com/Thoughtscript/carbon_offsets/settings/secrets/actions - These are not to be confused with Codespaces Secrets!
- And passed as values into YAML like so:
${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}.
Some services handle the actual retrieval, refreshing, and obtaining of Tokens through the above.
Many Cloud Providers offer prepublished GitHub Actions that perform certain operations (such as logging into AWS) that can be used in a
usesblock.
In other cases one may need to define a command that calls some say OAUTH 2.0 REST API and stores the dynamic token (using >>) before making subsequent calls (per usual token auth flows):
name: Create issue on commit
on: [ push ]
jobs:
my_issue:
runs-on: ubuntu-latest
# ...
steps:
- name: Create issue using REST API
run: |
curl --request POST \
--url https://myoauthserverendpoint \ ...
Terraform
Terraform HCP can integrate with GitHub Actions (and with Terraform Cloud Providers).
https://developer.hashicorp.com/terraform/tutorials/automation/github-actions
AWS
Azure
jobs:
build:
runs-on: ubuntu-latest
steps:
# Checkout the repo
- uses: actions/checkout@main
- uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# ...
https://learn.microsoft.com/en-us/azure/app-service/deploy-github-actions?tabs=openid%2Caspnetcore
Resources and Links
- https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions
- https://aws.amazon.com/blogs/devops/integrating-with-github-actions-ci-cd-pipeline-to-deploy-a-web-app-to-amazon-ec2/
- https://learn.microsoft.com/en-us/azure/app-service/deploy-github-actions?tabs=openid%2Caspnetcore
GitHub Actions: Enterprise
GitHub Actions supports many features for Enterprise operations.
Templates
- GitHub Actions Templates can be defined to encourage standards and reuse.
- These are similar to GitHub Pull Request Templates.
- These are basically prepopulated but blank YAML files that can be used as a starting point.
https://docs.github.com/en/actions/writing-workflows/using-workflow-templates
Organization Policies
- GitHub Actions Policies can be defined to restrict who can do what.
- These are similar to GitHub Organizational Policies.
Resources and Links
- https://docs.github.com/en/actions/writing-workflows/using-workflow-templates
- https://docs.github.com/en/enterprise-cloud@latest/admin/managing-github-actions-for-your-enterprise/getting-started-with-github-actions-for-your-enterprise/introducing-github-actions-to-your-enterprise
- https://docs.github.com/en/enterprise-cloud@latest/admin/enforcing-policies/enforcing-policies-for-your-enterprise/enforcing-policies-for-github-actions-in-your-enterprise
GitHub Actions: Misc.
Misc. study items:
[skip ci],[ci skip],[no skip],[skip actions],[actions skip]- Use
|for a multiline string (to run multiple commands in a single step - not&&) - Default permission levels that can be assigned to
GITHUB_TOKEN:none,write,read. - Multiple jobs will run in parallel by default.
needskeyword specifies that one job requires another.$vs${{ ... }}runs: using: "composite" steps: - name: Set Greeting # Use the ENV value run: echo "Hello $INPUT_WHO_TO_GREET." shell: bash env: # Set the ENV value INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}- Debugging syntax from within a Step:
echo "::debug::Set the Octocat variable".- https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#example-setting-a-debug-message
echowill also cause other text to display without using::debug::.
- OIDC is recommended for security hardening.
- Workflow triggering events:
workflow_call- is invoked by another Workflow (say as part of a nested Workflow)workflow_dispatch- manually triggered in the GitHub UI.workflow_run- is run after another iscompleted,requested,in_progress.- https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#workflow_call
- Status Check Functions:
if: ${{ success() }}if: ${{ always() }}if: ${{ cancelled() }}if: ${{ failure() }}- https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/evaluate-expressions-in-workflows-and-actions#status-check-functions
steps.<step_id>.outcome- Step Context
- Values:
success,failure,cancelled, orskipped - Not to be confused with
steps.<step_id>.outputswhich explicitly defined. - https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs#steps-context
- Branch Filters use Glob Patterns:
- https://code.visualstudio.com/docs/editor/glob-patterns
- As used in Spring Servlet matching: https://docs.spring.io/spring-framework/reference/web/webflux/controller/ann-requestmapping.html#webflux-ann-requestmapping-uri-templates
- https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/triggering-a-workflow#using-filters-to-target-specific-branches-or-tags-for-push-events
- Disabling