Git Tricks Worth Knowing

使用 Git 的「奇技淫巧」

Posted by Will on August 24, 2021

Restore / Reset

  • Restore file change
1
git restore <filename>
  • Reset all changes
1
git reset --hard HEAD
  • Unstage
1
git restore --cached <filename>
  • Restore accidental deletion before commit
1
git checkout HEAD <filename>
  • Restore deleted file after commit
1
git reset --hard HEAD~1

Remote

  • List remotes
1
git remote -v
  • Add remote
1
git remote add <remote_name> <remote_url>
  • Set remote url
1
git remote set-url <remote_name> <remote_url>

Diff

  • Diff unstaged files
1
git diff
  • Diff staged files
1
git diff --staged
  • List staged files
1
git diff --staged --name-only

Branch

  • Clone specifying branch
1
git clone -b <branch_name> <remote_url>
  • Create and checkout
1
git checkout -b <branch_name>
  • Delete local branch
1
git branch -d <branch_name>
  • Delete remote branch
1
git push -d <remote_name> <branch_name>
  • Update local branch list
1
git fetch -p