Skip to content

Instantly share code, notes, and snippets.

@hiyangguo
Last active March 22, 2023 14:15
Show Gist options
  • Save hiyangguo/5c1b8b3e0e163a200d70e4a596382862 to your computer and use it in GitHub Desktop.
Save hiyangguo/5c1b8b3e0e163a200d70e4a596382862 to your computer and use it in GitHub Desktop.
Git cheat sheet.
  1. Delete mutiple branch
git branch -D `git branch --list '3.2.*'`
  1. Apply the changes introduced by some existing commits
# one
git cherry-pick 88ce01c
# mutiples
git cherry-pick 88ce01c..42ab7f4
  1. Rename branch
git branch -m old-name new-name
  1. Delete all untracked files
git clean -df
  1. Keep in local branches sync with remote branches
git fetch --prune origin 
  1. Recursively add files by pattern.
find . -name "filename" | xargs git add

eg: Find ./src directory all index.js files.

find ./src -name "index.js" | xargs git add
  1. See detail changelog of a file.
git blame file
# Process only line range 2 to 7
glit blame -L 2,7 file
  1. Use origin files overwrite local change.
git rest --hard origin/branch
  1. Create empty branch.
git checkout --orphan empty-branch
git rm -rf .
git clean -df
git commit --allow-empty -m "root commit"
  1. Clean all commits and duplicate current branch
git checkout --orphan latest_branch
git add -A
git commit -am "commit message"
git branch -D branch-name
git branch -m branch-name
  1. Specify an SSH key for git push
git config --add --local core.sshCommand 'ssh -i <<<PATH_TO_SSH_KEY>>>'
  1. Create a branch from another branch
# You can replace master to any branch you want to checkout from.
git checkout -b newBranchName master
  1. Update upstream
git branch --set-upstream-to <remotename>/<branchname>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment