Skip to content

Git quick command reference

discard all changes in the current working directory

git checkout -- .
git reset --hard
git checkout -- <file> # discard changes in one file

move commits from one branch to another

  • scenarios:
    • you have 2 commits that pushed to dev directly, instead you want to reset them and make a PR so the commits get a review.
    • move commits from the one branch1 to the other branch2, commits must be deleted from the branch1 and added to the branch2.
git checkout dev # branch to move from
git checkout -b feature/new-feature # branch to move to
git checkout dev # back to original branch
git reset --hard HEAD~2 # reset 2 commits back to the state of the branch
git push origin dev # push the changes to the remote
  • now the branch feature/new-feature has 2 commits ahead of dev.

delete all local branches that have no remote

git fetch -p && for branch in $(git for-each-ref --format '%(refname) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'); do git branch -D $branch; done
- reference: https://stackoverflow.com/a/33548037/10871748