首页 » 操作系统 » macOS » 正文

Git 常用设置收集 16+

发布者:站点默认
2018/12/10 浏览数(1,424) 分类:macOS, Windows, 前端, 后端 Git 常用设置收集 16+已关闭评论

修改 VS Code 默认的 commit message 字符长度限制

settings.json 默认标题为 50,概要为 72

"git.inputValidationSubjectLength": 150,
"git.inputValidationLength": 272

按时间正序查某人的提交记录

git log --author="某某人" --since="2024-12-12" --reverse --oneline --pretty=format:"%h %ad %s" --date=iso

在 Local 排除文件(不修改 .gitignore)

git update-index --skip-worktree postcss.config.js    # 排除
git update-index --no-skip-worktree postcss.config.js # 不再排除

修复分支名与远程不一致时 –set-upstream 不起作用

git config --global push.default upstream

忽略已经提交过的文件

git rm -r --cached unpackage/ # 忽略目录
git rm --cached yarn.lock     # 忽略文件
# 记得加入 .gitignore 并将删除请求 commit

修改 git 作者

git commit --amend --author="NewAuthor <NewEmail@address.com>"

批量修改 git 作者

#!/bin/sh
#via https://blog.csdn.net/diu_brother/article/details/51982993

git filter-branch --env-filter '
an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"

if [ "$GIT_COMMITTER_EMAIL" = "YourOldEmail" ]
then
    cn="YourNewAuthorName"
    cm="YourNewEmail"
fi
if [ "$GIT_AUTHOR_EMAIL" = "YourOldEmail" ]
then
    an="YourNewAuthorName"
    am="YourNewEmail"
fi

export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'

本地分支改名

git branch -m old_branch new_branch # 本地分支改名
git push origin :old_branch         # 删除远程分支

pull 时用 rebase 替换默认的 merge

git config --global pull.rebase true

获取当前分支名称

git symbolic-ref --short -q HEAD
# 如果使用了 git-prompt 可以这样:
echo $(__git_ps1 "%s")

修改最近一次的 commit message

git commit --amend

修改历史记录中的 commit message

# 修改最近3条内的
git rebase -i HEAD~3
把你要改 message 的那条 commit 前的 pick 改为 edit 后 :wq
git commit --amend 并更新 message
git rebase --continue

修改默认的 commit 编辑器为 vim

git config --global core.editor vim
# nano 的保存方式:ctrl+x,y,enter

删除 git 历史记录中的大文件

# 1. 从历史提交中找出占用空间最大的5个文件
git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"
# 2. 重写commit,删除这个大文件或目录
git filter-branch --force --index-filter 'git rm -rf --cached --ignore-unmatch big-file.jar' --prune-empty --tag-name-filter cat -- --all
# 3. 推送修改后的repo
git push origin master --force
# 4. 清理和回收空间
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now

这个技巧来自这里这里

删除远程的分支 dev

git push origin -d dev
# 或:
git push origin --delete dev

删除本地记录的程远已经不存了的分支

git remote prune origin

记住密码

git config --global credential.helper cache                  # 记住15分钟
git config --global credential.helper 'cache --timeout=3600' # 记住3600秒
git config --global credential.helper store                  # 一直记住

解决“fatal: Authentication failed for …”

git config --system --unset credential.helper
# win 下可能需要以管理员身份运行

Git 用户配置

# ~/.gitconfig
[user]
	name = Mr.User
	email = user@honzh.com
[alias]
br = branch
bd = branch -d
co = checkout
cb = checkout -b
ci = commit
cm = commit -m
df = diff
st = status
lg  = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit --date='format-local:%Y-%m-%d %H:%M:%S'
lg1 = log --pretty=format:'%C(green)%cd %Cred%h%Creset -%C(yellow)%d%Creset %s %C(bold blue)<%an>%Creset' --abbrev-commit --date='format-local:%Y-%m-%d %H:%M'
ls = log --stat
lp = log -p
last = log -1 HEAD
up = push
[core]
	ignorecase = false
[credential]
	helper = store

合并当前分支到指定分支后返回当前分支

#!/usr/bin/bash
if [ ! -d ".git" ]; then
	echo 'no git repo found'
	exit
fi

to=$1
if [ "$to" = "" ]; then
	echo "usage: $0 mergeToBranchName"
	exit
fi

current=$(git symbolic-ref --short -q HEAD)
if [ "$current" = "$to" ]; then
	echo 'can not merge to self.'
	exit
fi

echo -e ">> Merge [\e[0;33m$current\e[0m] into [\e[0;31m$to\e[0m] ..."
#ping 127.0.0.1 -c 2 > nul

git checkout $to && git pull --rebase=true && git merge $current -m "Merge branch '$current' into $dev" && git push && git checkout $current
r=$?

if [ $r -eq 0 ]; then
	echo -e ">> \e[0;32mSuccess\e[0m"
else
	echo -e ">> \e[0;31mFail\e[0m[$r]"
	git checkout $current > /dev/null 2>&1
fi

用指定 branchName 创建 git worktree

#!/usr/bin/env bash
if [ ! -d ".git" ]; then
	echo 'no git repo found'
	exit 1
fi

action="$1"
branch="$2"
folder="$branch"
addonParams=("${*:3}")

# replace \ and / to _
folder="${folder//\//_}"
folder="${folder//\\\\/_}"
folder="../$folder"

if [ "$branch" = "" ]; then
	name=$(basename $0)
	echo "usage: $name add/remove/lock/unlock BRANCH_NAME"
	git worktree list
else
	if [ "$action" = "add" ]; then
		git worktree $action $folder $branch $addonParams && code $folder
	elif [ "$action" = "remove" ]; then
		git worktree $action $folder $addonParams && git worktree prune
	else
		git worktree $action $branch $addonParams
	fi
fi
点击返回顶部
  1. 留言
  2. 联系方式