Git Tips
I got inspired by this great git-tips/tips repo and want to generate my own list based on my personal usage.
It is always good to check out the official docs if you want a detailed explanation, or the man-page (Git Manual) for each git command.
Set up local git user
git config --global user.name "Your Name"
git config --global user.email "[email protected]"Configure for global gitignore file
git config --global core.excludesfile ~/.gitignore_globalThen create a .gitignore_global file under the home directory.
Show all untracked files and untracked directories
git status -u-u option defaults to -uall which shows all individual files in untracked directories. Without the -u option, the command will only show untracked files and directories, but not the files inside of those directories.
Show changes between commits, commit and working tree, etc.
git diff
# show the numbers of lines changes
git diff --statShow all the commit history including all remotes, branches, tags
git log --remotes --branches --tags --graph --onelineLimit the number of logs
git log -n <number>Search logs by commit messages
git log -i --grep=<pattern>-ior--regexp-ignore-caseignore case--grep=<pattern>limit the log messages matching the<pattern>
Search logs by file or code changed
git log -S<string>Search by string, binary files are also searched, good for tracking a block of code.
Search log by file name and/or directory
git log -- </dir/filename>Show commit history with a custom format
git log --pretty=format:'%C(yellow)%h%C(reset) - %an %Cgreen[%ar]%Creset %s'This is just an example, more customization can be achieved.
%habbreviated commit hash%anauthor name%arauthor date, relative%ssubject%C(yellow)or%Cgreencolor specification%C(reset)or%Cresetreset color
Shows a commit's log message and textual diff
git show <commit_hash>Show the last modified revision and author of each line of a file
git blame </dir/filename>Rebase all commits reachable from the current branch.
git rebase -i --rootDelete a local branch
git branch -d <branch>List tags
git tagAdd a local tag
git tag <tag> # from the current commit
git tag <tag> <commit_hash> # from a different commitDelete local tags
git tag -d <tag> # single tag
git tag -d $(git tag) # all tagsChange capitalization of filenames
git mv example.txt Example.txtSimply changing the filename by mv example.txt Example.txt won't work in macOS because it's case preserving but case insensitive. In the future it will cause path collision. So avoid changing the filename directly; use git mv to change filenames.
Remove (prune) remote-tracking branches that no longer exist in the remote
git fetch -p # prunePush a local branch to remote and set up remote-tracking
git push -u <remote> <branch>Delete a remote branch
git push -d <remote> <branch>
git push <remote> :<branch> # alternative, using the ref syntaxPush local tags to remotes
git push <remote> <tag> # single tag
git push <remote> --tags # all tagsDelete remote tags
git push -d <remote> <tag> # single tag
git push -d <remote> $(git tag) # all tags
git push <remote> :refs/tags/<tag> # alternative, using the refs syntax