常用Git命令

博文列表#

常用技巧#

克隆指定分支#

How do I clone a single branch in Git?

# clone only the remote primary HEAD (default: origin/master)
git clone <url> --single-branch
# as in:
git clone <url> --branch <branch> --single-branch [<folder>]

查看文件区别#

git diff <previous_commit_hash>..<newer_commit_hash> -- {file_path} 用于查看指定文件两个不同commit之间的区别, 比如:

git diff 9a422584b66686b11c20ce330aeb84f47e1c0b9b..e5f766ac6899f191a2fd53c9247f1ee9264886e0 -- src/some_file.js

原文 - Show diff between commits

Try

git diff k73ud^..dj374

to make sure to include all changes of k73ud in the resulting diff.

git diff compares two endpoints (instead of a commit range). Since the OP want to see the changes introduced by k73ud, he/she needs to difference between the first parent commit of k73ud: k73ud^ (or k73ud^1 or k73ud~ ).

That way, the diff results will include changes since k73ud parent (meaning including changes from k73ud itself), instead of changes introduced since k73ud (up to dj374).

Also you can try:

git diff oldCommit..newCommit
git diff k73ud..dj374

and (1 space, not more):

git diff oldCommit newCommit
git diff k73ud dj374

And if you need to get only files names (e.g. to copy hotfix them manually):

git diff k73ud dj374 --name-only

And you can get changes applied to another branch:

git diff k73ud dj374 > my.patch
git apply my.patch

缓存认证数据#

git config --global credential.helper cache # 设置credential存储在内存中, 默认是 “900”,也就是 15 分钟
git config --global credential.helper store --file ~/.my-credentials # 设置credential存储在指定文件

配置文件方式, 设置credential存储在内存中, 并设置过期时间:

~/.gitconfig
[credential]
helper = cache --timeout 3600

切换指定编辑器#

使用以下方式指定默认编辑器为 vim:

git config --global core.editor "vim"
export GIT_EDITOR=vim

或者编辑配置文件使用 sublime 作为默认编辑器:

~/.gitconfig
[core]
editor = 'subl' --wait
note

注意要加上 --wait

标签#

# ref: https://stackoverflow.com/questions/3404936/show-which-git-tag-you-are-on
# 显示当前的标签
git describe --tags
# ref https://stackoverflow.com/questions/35979642/how-to-checkout-remote-git-tag
# 获取所有的tag
git fetch --all --tags --prune
# 切换标签
git checkout tags/<tag_name> -b <branch_name>
# list all tags
git tag
# create tag
git tag -a v1.0 -m "Product release"
# delete tag
git tag -d <tag_name>

删除分支#

git branch -d feature/login # 删除本地分支
git push origin --delete feature/login # 删除远程分支