Skip to content

Latest commit

 

History

History
283 lines (238 loc) · 8.85 KB

software-usage.md

File metadata and controls

283 lines (238 loc) · 8.85 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>
    

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