修改历史 commit 信息 1 2 3 4 git rebase -i HEAD~2 pick -> edit git commit --amend git rebase --continue
修改 commit 时间 1 2 3 4 # 设置为当前时间 GIT_COMMITTER_DATE="$(date '+%Y-%m-%d %H:%M:%S')" git commit --amend --no-edit --date "$(date)" # 设置为指定时间 GIT_COMMITTER_DATE="2021-03-19 17:54:27 GMT+8" git commit --amend --no-edit --date "2021-03-19 17:54:27 GMT+8"
比较两个分支的不同 1 git diff branch_1..branch_2
列出不同分支之间的所有 commit 1 git log --left-right --cherry-pick --oneline v1.14.6..v1.14.10
比较两个分支中指定文件或目录 1 git diff mybranch master -- myfile.cs
merge 时使用指定方代码解决冲突 1 2 git merge -X theirs origin/dev git merge -X ours origin/dev
查看一个文件完整的修改历史 1 git log --follow -p -- _config.yml
将当前分支下子目录内容提交至另一个分支 1 git subtree push --prefix dist origin gh-pages
删除 submodule 1 2 3 git submodule deinit <path_to_submodule> git rm <path_to_submodule> git commit -m "Removed submodule "
合并所有 commit 为一个 1 2 3 git rebase --root -i // 使用以下命令可以将需要 rebase 的 commit 的时间全部设置为当前时间 git rebase --ignore-date 303a824f46b497f71582e2e5d493c132b85e3e0a
删除所有没有远程分支的本地分支 1 git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d
撤销某个 commit 1 git revert --strategy resolve <commit>
使用 ssh 替代 https 访问 1 git config --global url."git@git.ucloudadmin.com:" .insteadOf "https://git.ucloudadmin.com/"
查看 commit 内容 查看指定 commit id 的提交内容
1 git show 67cebafce9c45a1d65fddd3d1742c4cb42851d10
查看最近 n 次提交内容
创建和删除 tag 1 2 3 4 5 6 7 8 9 10 11 12 // 删除本地 tag git tag -d v0.0.1 // 删除远程仓库 tag git push --delete origin v0.0.1 // 创建本地 tag git tag v0.0.1 -m "Release version 0.0.1" // 以某个 commit 为基础创建 tag git tag -a v0.0.1 30728cab -m "Release version 0.0.1" // 推送指定 tag 到远程 git push origin v0.0.1 // 推送所有 tag 到远程 git push --tags
获取最近的 tag 1 2 3 4 5 // 获取最新的 tag git describe --tags $(git rev-list --tags --max-count=1) // 获取最新 commit 对应的 tag git describe --abbrev=0 git describe --tags
创建和应用 patch 1 2 3 4 // 生成 git patch git diff c78bca..709fcfe > /tmp/cloudprovider.patch // 应用 patch git apply /tmp/cloudprovider.patch
gitlab 创建 tag 和发布附件流程 参考:https://stackoverflow.com/a/55415383
cherry-pick 时移除不需要的修改 参考:https://stackoverflow.com/questions/5717026/how-to-git-cherry-pick-only-changes-to-certain-files 如果需要移除的修改较少,可使用以下方式:
1 2 3 4 5 6 7 git cherry-pick -n 1683355bf81435ffdcbf332ad3558cb92853c9eb // 丢弃该 commit 对 go.mod 文件的修改 git restore --staged go.mod // 或者 git checkout HEAD go.mod // 直接提交即可, commit 信息使用上述 commit 的 git commit
如果大部分修改都需要移除,可采用:
1 2 3 4 5 6 git cherry-pick -n 1683355bf81435ffdcbf332ad3558cb92853c9eb git reset HEAD // 添加需要保留的修改 git add <path> // 移除所有不需要保存的修改 git clean -f
如果仅有一两项文件或目录的修改需要保留,可执行:
1 2 3 4 5 6 7 8 // 应用对文件或目录下文件的修改 git show SHA -- file1.txt file2.txt | git apply - git show SHA -- dir1 dir2 | git apply - // 保存修改 git add file1.txt file2.txt git commit -c SHA // 或者二合一 git show SHA -- file1.txt file2.txt | git apply --cached -
使用指定 commit 中的内容覆盖指定文件或目录 1 git checkout c5f567 -- file1/to/restore file2/to/restore
github actions
clone all projects in group https://github.com/gabrie30/ghorg 或者
1 for repo in $(curl -s --header "PRIVATE-TOKEN: your_private_token" https://<your-host>/api/v4/groups/<group_id> | jq ".projects[].ssh_url_to_repo" | tr -d '"' ); do git clone $repo ; done ;
设置代理 1 2 3 4 5 6 git config --global http.proxy http: git config --global http.proxy socks5h: git config --global --unset http.proxy git config --global http.https: git config --global --unset http.https:
查看所有分支的最后活跃时间 1 2 3 4 5 6 7 8 9 for branch in `git branch -a`;do ;if [ $branch != "*" ]; then ; hasAct=$(git log --abbrev-commit --date=relative -1 $branch ); lastActivity=$(echo "$hasAct " | grep Date: | sed 's/Date: //' ); echo "$branch last activity was\033[1;31m$lastActivity \033[0m" ; echo "" fi ;done ;