按时间正序查某人的提交记录
1 | git log --author="某某人" --since="2024-12-12" --reverse --oneline --pretty=format:"%h %ad %s" --date=iso |
在 Local 排除文件(不修改 .gitignore)
1 2 | git update-index --skip-worktree postcss.config.js # 排除 git update-index --no-skip-worktree postcss.config.js # 不再排除 |
修复分支名与远程不一致时 –set-upstream 不起作用
1 | git config --global push.default upstream |
忽略已经提交过的文件
1 2 3 | git rm -r --cached unpackage/ # 忽略目录 git rm --cached yarn.lock # 忽略文件 # 记得加入 .gitignore 并将删除请求 commit |
修改 git 作者
1 | git commit --amend --author= "NewAuthor <NewEmail@address.com>" |
批量修改 git 作者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #!/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" ' |
本地分支改名
1 2 | git branch -m old_branch new_branch # 本地分支改名 git push origin :old_branch # 删除远程分支 |
pull 时用 rebase 替换默认的 merge
1 | git config --global pull.rebase true |
获取当前分支名称
1 2 3 | git symbolic-ref --short -q HEAD # 如果使用了 git-prompt 可以这样: echo $(__git_ps1 "%s" ) |
修改最近一次的 commit message
1 | git commit --amend |
修改历史记录中的 commit message
1 2 3 4 5 | # 修改最近3条内的 git rebase -i HEAD~3 把你要改 message 的那条 commit 前的 pick 改为 edit 后 :wq git commit --amend 并更新 message git rebase -- continue |
修改默认的 commit 编辑器为 vim
1 2 | git config --global core.editor vim # nano 的保存方式:ctrl+x,y,enter |
删除 git 历史记录中的大文件
1 2 3 4 5 6 7 8 9 10 | # 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
1 2 3 | git push origin -d dev # 或: git push origin --delete dev |
删除本地记录的程远已经不存了的分支
1 | git remote prune origin |
记住密码
1 2 3 | 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 …”
1 2 | git config --system -- unset credential.helper # win 下可能需要以管理员身份运行 |
Git 用户配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # ~/.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 |