🔱 你不知道的 Git 命令
TL;DR
TL;DR
我最喜欢 Git 的一点就是它简洁易用,而且可自定义,其中之一就是别名。Git 支持别名,这意味着你可以为命令指定任何你想要的名称。我更喜欢为很长的命令设置别名,这样就不用每次都需要搜索了。
Git 中的配置alias
是这样运作的。
git config --global alias.[new_alias] "[previous_git_command]"
# Example
git config --global alias.save commit
从上面的例子来看,我不需要git commit
再这样做了。git save
这样就足够好了。
在具有多个选项的命令周围添加引号。
git recommit
git config --global alias.recommit 'commit --amend -m'
git commit --amend
允许您更改最后的提交消息。recommit
更简单,更容易记住。
# Change the last commit message with recommit
git recommit "New commit message"
# [master 64175390] New commit message
# Date: Tue Sep 22 15:09:11 2020 +0000
# 1 file changed, 2 insertions(+)
# create mode 100644 vue.js
git commend
git config --global alias.commend 'commit --amend --no-edit'
使用标志提交修改--no-edit
允许您使用上次提交来提交 repo 中的新更改,因此您不必再次重复提交消息。
git search
git config --global alias.search 'grep'
# Example
git search [search_term]
git grep
允许你在代码库中搜索关键词,并返回各种匹配结果。这很酷,但我不知道它是什么grep
意思,如果你知道的话请告诉我。我search
更喜欢“易于记忆且易于使用”这个说法。
git here
git config --global alias.here '!git init && git add . && git commit -m "init 🦄"'
通常,当我初始化一个新的仓库时,我会暂存所有文件,然后提交初始提交消息。git here
只需一步即可完成所有操作。只需在您想要创建新仓库的文件夹中运行它,就可以了。
git who
git config --global alias.who 'blame'
# Example
git who index.ts
# 641753902 (Ephraim Atta-Duncan 2020-09-22 15:09:11 +0000 1)
# 641753902 (Ephraim Atta-Duncan 2020-09-22 15:09:11 +0000 2) console.log("who?")
git blame
用于逐行检查文件内容,查看每行最后修改的时间以及修改者。如果某一行存在 bug,你可以找到是谁who
做的,并追究责任。
git zip
git config --global alias.zip 'archive --format=tar.gz -o ../repo.tar.gz'
# Example
git zip [branch_name]
这些archive
命令允许你创建整个或部分仓库的 tarball 和 zip 文件,git zip
方便记忆。只需添加分支名称即可。
git newbie
git config --global alias.newbie 'checkout --orphan'
# Example
git newbie [new_branch_name]
git checkout
使用此--orphan
标志,您可以创建一个不包含父分支任何历史记录的分支。无需提交,即可创建全新的分支。
git clonely
git config --global alias.clonely 'clone --single-branch --branch'
# Example
git clonely [branch_name] [remote_url]
git clonely v3 https://github.com/vuejs/vue-apollo
# Cloning into 'vue-apollo'...
# remote: Enumerating objects: 2841, done.
# remote: Total 2841 (delta 0), reused 0 (delta 0), pack-reused 2841
# Receiving objects: 100% (2841/2841), 1.92 MiB | 330.00 KiB/s, done.
# Resolving deltas: 100% (1743/1743), done.
git clone
使用--single-branch --branch
flags 可以让你从仓库克隆到特定的分支,我可以说,我已经在 Google 上搜索过不止十次了。使用别名会更好。
git plg
git config --global alias.plg "log --graph --pretty=format:'%C(yellow)%h%Creset -%Cred%d%Creset %s %Cgreen| %cr %C(bold blue)| %an%Creset' --abbrev-commit --date=relative"
# Example
git plg # plg - Pretty Log
git log
除了有点丑、没有颜色差异,而且如果想自定义,还得谷歌搜索一番,其他都没什么问题。还好我们有别名功能。给命令加上别名,就能得到非常漂亮的日志。
git fresh
git config --global alias.fresh "filter-branch --prune-empty --subdirectory-filter"
# Example
git fresh [subfolder] [branch_name]
git fresh src main # Don't do this unless you know what you are doing
替换的一系列命令fresh
用于从子文件夹的内容中创建一个新的存储库。filter-branch
使用它,许多标志会获取指定子文件夹的内容,并用子文件夹的内容替换整个存储库中的内容。
TL;DR
将其添加到您的.gitconfig
文件中。
[alias]
recommit = commit --amend -m
commend = commit --amend --no-edit
here = !git init && git add . && git commit -m \"Initialized a new repository\"
search = grep
who = blame
zip = archive --format=tar.gz -o ../repo.tar.gz
lonely = clone --single-branch --branch
plg = log --graph --pretty=format:'%C(yellow)%h%Creset -%Cred%d%Creset %s %Cgreen| %cr %C(bold blue)| %an%Creset' --abbrev-commit --date=relative
fresh = filter-branch --prune-empty --subdirectory-filter