Skip to content

Git tips

Published:

批量删除 git 分支

git branch -a | grep -e "fix/" | xargs git branch -D

批量添加匹配文件到暂存区

 git status -s -uall | grep .vue | awk '{print $2}' | xargs git add

按最后提交日期排序所有远程分支

git branch -rv --sort=-committerdate

更多:How can I get a list of Git branches, ordered by most recent commit?

更好的 git log

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

列出另外一个分支没有的提交

git cherry -v branch-A branch-B

clone 特定分支

git clone -b develop git@github.com:user/myproject.git

递归移动文件夹

mv bar/{,.}* .

列出特定分支的记录

git log -p branch-name

查看特定文件的 git 记录

 git log -p -- filename

修改 git 提交为任何人

git -c user.name="NEW NAME" -c user.email="new_email@gmail.com" commit --amend --date="Tue Nov 20 03:00 2018 +0100" --author="NEW NAME <new_email@gmail.com>"

查看当前所有子目录的 git 状态

find . -maxdepth 1 -mindepth 1 -type d -exec sh -c '(echo {} && cd {} && git status -s && echo)' \\;

创建 git 归档时忽略某些目录

tar cvfz app.tar.gz --exclude ".git/*" --exclude ".git" app/

列出未发布的 commit

git log @{u}..

在所有 commit 中寻找代码

git rev-list --all | xargs git grep

找到一个文件是何时被删掉的

git log --diff-filter=D -- path/to/file

删除远程分支

git push origin :branch

让 git 可以递归调用

git config --global alias.git '!git'

revert cherry-pick

git rebase -p --onto SHA^ SHA

永久删除文件

git filter-branch --tree-filter 'rm -rf my_folder/my_file' HEAD

永久链接

permalink = "!f() { echo "https://$(git config --get remote.origin.url | grep --color=never -o -E 'github.com[:/][^\\.]+' | sed s/\\:/\\\\//)/commit/$(git rev-parse @{u})"; }; open $(f)"

仅忽略本地副本的文件更改

git update-index --assume-unchanged <file>

git update-index --no-assume-unchanged <file>

忽略已跟踪的文件

git rm --cached <file>

获取当前分支名称

git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'

查看某个版本的目录树

git show HEAD~5:hello.txt

git show awesome-feature:app/models

作者 : 4Ark

地址 : https://4ark.me/post/git-tips.html/

来源 : https://4ark.me

著作权归作者所有,转载请联系作者获得授权。