Skip to content

Instantly share code, notes, and snippets.

@nicohsieh
Last active July 25, 2019 23:34
Show Gist options
  • Save nicohsieh/b76a9655d3bdb7c2767ad66402db5c74 to your computer and use it in GitHub Desktop.
Save nicohsieh/b76a9655d3bdb7c2767ad66402db5c74 to your computer and use it in GitHub Desktop.
Git command memo

Memo for some Git commands and tasks

Commands

Checkout

git checkout <commit> <file> # check out a certain verion of a file

Cherry

git cherry -v # output commits that are not in the remote branch (-v verbose)
git cherry -v <remote>/<branch> # output commits that are not in another upstream branch 

Clean

git clean -df # force removing files/directories that are not under version control

Config

git config -l # show all inherited values from: system, global and local (-l shorthand of --list)
git config -e # edit config (-e shorthand of --edit)

Diff

git diff --cached # show staged change
git diff --name-only # only show diffed file names
git diff <branch> -- <file> # diff the file in different branch

Fetch

git fetch origin # synchronize with origin

LFS

git lfs install # install LFS. If LFS is installed by brew, it has to be installed in the project directory
git lfs track <pattern> # start tracking a certain pattern
git lfs track # list tracked patterns
git lfs ls-files # list tracked files

Log

git log --author=<pattern> # show commits matching author pattern
git log --after=<yyyy-m-d> --before=<yyyy-m-d> # show commits within a assigned date range
git log --grep=<pattern> # search matching commit messages
git log <file path> # show commits of the specified file
git log origin<branch> # show commits in the origin remote

Remote

git remote show origin # show remote origin
git remote add <remote_name> <remote_url> # add a new remote
git remote -v # print remote info
git remote rename <old_name> <new_name> # rename remote
git remote rm <name> # remove remote
git remote update # fetch updates from remotes

Reset

git reset <filepath> # unstage staged changes in the file
git reset HEAD~<number> # reset a number of commits, --hard, --soft, --mixed(default)

Show-ref

git show-ref --tags # show tagged commits

Stash

git stash save <message> # save stash with a message
git stash list # show all stashes
git stash pop # apply the latest stash
git stash clear # clear all stashes
git stash drop stash@{<index>} # drop the stash at the specified index
git stash show -p stash@{<index>} # show the content of the stash at the specified index

Tag

git tag <tagName> <commitHasH> # tag a commit
git tag -d <tagName> # delete a tag
git show <tagName> # show tag content

Tasks

Rename Branch

git branch -m <new_branch> # rename current branch locally  
git branch -m <old_branch> <new_branch> # rename branch locally    
git push origin :<old_branch> <new_branch>  # delete the old branch
git push --set-upstream origin <new_branch> # push the new branch, set local branch to track the new remote

Delete Branch

git branch -d <branch_name> # delete local branch
git push <remote> -d <branch_name> # delete remote branch

Reset to Origin Branch

git fetch <remote>
git reset --hard <remote>/<branch>

Patch

git diff <tag1>..<tag2> > <patchName>.patch
git apply <patchName>.patch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment