Skip to content

Latest commit

 

History

History
316 lines (268 loc) · 10.4 KB

software-usage.md

File metadata and controls

316 lines (268 loc) · 10.4 KB

Software Usage

Git

General info

Configuration

Show status

  • Show individual files in untracked directories (ref)
    git status [-u | --untracked-files]
    

Clone and fetch

  • Shallow clone: git clone --depth=<num> [--no-single-branch] --depth implies --single-branch, thus the fetch = +refs/heads/DEFAULT_BRANCH:refs/remotes/origin/DEFAULT_BRANCH line in .git/config, which makes new branches pushed to remote not auto-tracked locally
    • To overwrite --single-branch, run git remote set-branches REMOTE BRANCH or directly edit .git/config
    • To query all remote.*.fetch settings, git -P config get --all --show-names --regexp 'remote\..*\.fetch'
  • Convert a shallow clone to full clone (ref)
    git fetch --unshallow
  • Fetch a specific commit
    git fetch --depth=1 origin <commit>
    
  • Fetch a specific tag
    git fetch --no-tags origin tag <tag>
    
    https://stackoverflow.com/a/54635270 short form -n https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt--n see also Git config remote.<name>.tagOpt.

Commit changes

Workflow

  • Keep branch clean with --fixup and --autosquash (article)
    # `--fix` automatically marks your commit as a fix of a previous commit
    # The resulted commit message will be "fixup! <msg of referred commit>"
    git commit --fixup <commit>
    # `--autosquash` automatically organizes merging of  fixup commits and associated normal commits
    git rebase [-i] --autosquash <commit>

Branching and Merging

  • Create a local branch that tracks a remote one (doc)
    git fetch <remote> <branch>:<local branch>
  • Delete a remote-tracking branch (the corresponding local branch is unchanged, ref)
    git branch --delete --remotes <remote>/<branch>
  • Track new remote branch after a shallow clone (ref)
    # shallow clone
    git clone --depth=<num> <repository> [<directory>]
    
    # change the list of branches tracked
    git remote set-branches <remote> '*'
    # or
    git remote set-branches --add <remote> <new_branch>
    
    # add track to new remote branch
    git fetch <remote> <new_branch>
    wait for test: git fetch --update-shallow <remote> <branch>
  • Include a commit summary in merge commit
    git merge --no-ff --log <branch>

Switch to a branch or commit-ish

git switch BRANCH
git switch -c/--create NEW_BRANCH [START_POINT]
git switch ---detach START_POINT

Back to clean working directory

git stash list
# -a/--all also takes ignored files into consideration
git stash [push] [-u | --include-untracked] [(-m | --message) <message>]
git stash pop 
git stash show [--stat] [--patch] [-u | --include-untracked] [stash@{0}]
git stash drop [stash@{0}]

Restore files from index or some commit

# restore working tree (-W/--worktree) from HEAD
git restore [--] <pathspec>...
# restore index from HEAD
git restore --staged [--] <pathspec>...

# -S/--staged and -W/--worktree can be used in together
# -p/--patch: select hunks interactively
# -s/--source=<tree>: restore from <tree>

Tags

# add a lightweight tag
git tag -a <tag>
# add an annotated tag
git tag -a <tag> -m "annot"

# list only lightweight tags
# ref: https://stackoverflow.com/a/67687543
git for-each-ref refs/tags | grep commit

# list only annotated tags
git for-each-ref refs/tags | grep -v commit

Push to remote

  • Push single tag (Q&A)
    # to resolve tag/branch name clashes, use "refs/tags/<tag>"
    git push <remote> <tag>
    Differences between annotated (-m <message>) and unannotated tags: this Q&A

Change remote

  • Delete remote branch or tag
    git push --delete <remote> <branch/tag>

Show log

  • List commits that changed a specific file (ref)
    git log --follow -- filename
  • Show first commit (ref)
    git log --reverse

Large File Storage (LFS)

# install and init
brew install git-lfs
git lfs install

# track/untrack files by extension
git lfs track "*.gif"
git lfs untrack "*.gif"
# Equivalent to add/remove line
#   *.gif filter=lfs diff=lfs merge=lfs -text
# to/from `.gitattributes`

# fetch and checkout lfs files
git lfs fetch
git lfs checkout

# retrack files (after untrack them from lfs)
# QA: https://stackoverflow.com/q/35011366/
git add --renormalize .

Misc

GitHub CLI

  • gh api https://cli.github.com/manual/gh_api
    • adding parameters (via -f/--raw-field or -F/--field) changes the default HTTP method from GET to POST

      The default HTTP request method is GET normally and POST if any parameters were added. Override the method with -X/--method.

      • so gh api /repos/muzimuzhi/hello-github-actions/issues/26/comments -f body='Send by GitHub CLI' works but gh api /repos/denoland/vscode_deno/commits -f per_page=2 (without -X GET) failed with error gh: Not Found (HTTP 404)
      • feature request to only switch to POST when body parameters were added is made in cli/cli#6877 (comment)
  • debug PAGER= GH_DEBUG=1 https://cli.github.com/manual/gh_help_environment
    • GH_DEBUG=api also logs HTTP traffic