Skip to main content

常用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存储在内存中, 并设置过期时间:

[credential]
helper = cache --timeout 3600

配置编辑器

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

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

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

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

注意要加上 --wait

其他编辑器配置如下:

EditorConfig command
Atomgit config --global core.editor "atom --wait"
emacsgit config --global core.editor "emacs"
nanogit config --global core.editor "nano -w"
vimgit config --global core.editor "vim"
Sublime Text (Mac/Linux)git config --global core.editor "subl -n -w"
Sublime Text (Win, 32-bit install)git config --global core.editor "'c:/program files (x86)/sublime text 3/sublimetext.exe' -w"
Sublime Text (Win, 64-bit install)git config --global core.editor "'c:/program files/sublime text 3/sublimetext.exe' -w"
Textmategit config --global core.editor "mate -w"
Visual Studio Codegit config --global core.editor "code --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 # 删除远程分支

取消 git reset 操作

git reset 'HEAD@{1}'

来源 How to undo 'git reset'?