解决windows环境下的CRLF与unix环境下的LF问题,windows提交时CRLF=>LF,签出时LF=>CRLF,unix环境保留
| git config --global core.autocrlf input |
修改文件格式(修复显示'^M')
| : set ff |
| |
| : set ff=dos/unix |
忽略查看差异时显示'^M'
| git diff -b |
| |
| git diff --ignore-cr-at-eol |
| |
| git diff --ignore-space-change |
| |
| git config --global core.whitespace cr-at-eol |
| |
| git config --global --unset core.whitespace |
查看冲突的文件
| git ls-files -u | cut -f 2 | sort -u |
| |
| git diff --name-only --diff-filter=U |
查看冲突的内容
解冲突
| |
| git add |
| git commit |
| git push |
| |
| |
| git pull --rebase |
| |
| git add |
| git rebase --continue |
| git push |
批量修改提交历史中的用户和邮箱
| git filter-branch --commit-filter ' |
| if [ "$GIT_AUTHOR_EMAIL" = "history@email.com" ]; |
| then |
| GIT_AUTHOR_NAME="new_author"; |
| GIT_AUTHOR_EMAIL="new_email@email.com"; |
| git commit-tree "$@"; |
| else |
| git commit-tree "$@"; |
| fi' HEAD |
修改刚提交的提交信息,如用户和邮箱
| git commit --amend --author "new_author <new_email@email.com>" --no-edit |
修改指定提交的提交信息,如用户和邮箱
| git rebase -i commit-id^ |
| git commit --amend --author "new_author <new_email@email.com>" --no-edit |
| git rebase --continue |
追加tag到指定commit
| git tag -a <tag> -m <message> <commit-id> |
查看自上次Tag后提交次数
带时间显示的操作历史
恢复误把新提交追加到上次提交
| git reflog |
| |
| git reset commit-id |
撤销--amend追加的commit
| |
| git reflog |
| git reset HEDA@{n+1} |
追加新提交到指定提交
| git stash push -m "贮藏已修改的内容" |
| git rebase -i commit-id^ |
| |
| |
| git stash pop |
| git add |
| git commit --amend --no-edit |
| git push |
| git rebase --continue |
修改--amend追加到错误的commit,且push到远端
| |
| git reflog |
| git reset HEDA@{n+1} |
| git stash |
| git rebase -i commit-id^ |
| git stash pop |
| git add |
| git commit --amend --no-edit |
| git push |
| git rebase --continue |
| |
| git push |
拆分本次修改到多笔提交
| |
| git stash |
| git rebase -i HEAD^^ |
| git stash pop |
| |
| git add |
| git reset |
| |
| git add -p |
| git commit --amend --no-edit |
| git push |
| |
| git stash |
| git rebase --continue |
| git stash pop |
| git add |
| git commit --amend --no-edit |
| git push |
挑拣文件的部分修改提交(交互式暂存)
基于Gerrit 在代码仓追加新提交到指定提交(不限用户)(Gerrit使用Change-Id来标识提交,要合并为同一提交保持Change-Id不变即可)
| |
| git add |
| git commit |
| |
| git push |
回退
| |
| git reset --soft commit-id |
| |
| git reset commit-id |
| |
| git reset --hard commit-id |
版本回退
| |
| git reset --hard commit-id |
| git push -f git.repository |
| |
| git revert -n commit-id |
| git commit |
| git push |
压缩多笔提交为1笔
| |
| git rebase -i HEAD~n |
| git push |
| |
| |
| git rebase -i <commit-id> |
| |
| |
| git reset <commit-id^> |
| |
| git reset HEAD~n |
push指定commit(应对多个未push的commit)
| |
| git rebase -i <commit-id>^ |
| |
| git push origin <commit-id>:remote repository |
| |
删除指定提交
| git rebase -i commit-id^ |
| |
| |
| git reset --hard commit-id^ |
| git cherry-pick commit-id..commit-id-latest |
| |
| |
| git push -f origin remote-branch |
干净工作仓
| |
| git checkout . |
| |
| git clean -df |
| |
| git clean -ndf |
取消已跟踪的文件或文件夹
| git rm --cached <file> |
| git rm -r --cached <path> |
查看工作区某类型文件的修改列表
| git diff --stat *.c *.cpp *.h |
查看两次提交文件修改列表
| |
| git diff --name-filter HEAD^ HEAD |
| |
| git diff --diff-filter=A --name-filter HEAD^ HEAD |
查看具体差异
| |
| git diff |
| |
| git diff --cached |
| |
| git diff --staged |
| |
| git diff HEAD |
| |
| |
| git diff commit-id1 commit-id2 |
| |
| git diff branch1 branch2 |
| |
| git diff branch1 branch2 <file> |
| |
| git diff --stat branch1 branch2 |
查看提交级别差异
| |
| git log branch1 ^branch2 |
| |
| git log branch1..branch2 |
| |
| git log -lefg-right branch1...branch2 |
| |
| git log commit1..commit2 |
| |
| git log tag1..tag2 |
查看文件行是谁修改的
| git blame <filename> -L start_line,end_line |
查看文件行的修改历史
| git log -L start_line,end_line:<filename> |
拉取指定分支的代码
| git clone -b target-branch repository.git |
查看两个分支的父节点
| git merge-base branch-A branch-B |
| git log --oneline --graph --decorate branch-A branch-B |
贮藏文件但保留暂存区修改(贮藏与清理)
贮藏指定文件
| git stash push -m "description" <file> |
从贮藏恢复指定文件
| git checkout stash@{n} -- <file> |
查询指定分支的操作历史
git rev-parse
| |
| git branch --show-current |
| |
| git rev-parse --abbrev-ref --short HEAD |
| |
| git rev-parse --short HEAD |
git cherry-pick
| |
| git cherry-pick feature |
| |
| git cherry-pick Hash-A |
| |
| git cherry-pick Hash-A Hash-B |
| |
| git cherry-pick Hash-A..Hash-B |
| git cherry-pick Hash-A^..Hash-B |
远端仓多分支情况,本地创建分支如何绑定远端分支
| git checkout -b local origin/remote |
查看本地分支关联的远端分支
| git branch -vv |
| |
| git branch -a |grep "\->" |
本地分支关联远端分支
| git branch --set-upstream-to=origin/<branch> local-branch |
新建分支带走 detached 状态下的提交
| git checkout -b temp-branch <commit-id> |
| git checkout -b local origin/remote |
| git merge temp-branch |
| git branch -D temp-branch |
| |
| git checkout -b new-branch <commit-id> |
| git branch --set-upstream-to=origin/<branch> new-branch |
在指定节点创建分支
| git checkout -b new-branch <commit-id> |
恢复 detached 状态
| git checkout <commit-id> |
| |
| |
| git reflog |
| |
| git for-each-ref --sort=-committerdate refs/heads --format='%(committerdate:iso8601) %(refname:short) %(objectname:short) %(contents:subject)' |
查看提交的文件状态,Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), changed (T), Unmerged (U), Unknown (X), Broken (B).
| git log --raw |
| git show --stat |
| git show --raw |
| |
| |
| git show --stat --diff-filter=[(A|C|D|M|R|T|U|X|B)…[*]] |
查看提交信息,作者、提交者、作者提交时间、提交者提交时间
指定格式查看提交历史
| git log --graph --pretty="%C(yellow) Hash: %h %C(blue)Date: %ad %C(red) Message: %s " --date=human |
查询提交信息中关键字
| git log --grep="string" |
| |
| git log -i --grep="string" |
| |
| git log --grep="string1\|string2" |
查看指定字符串的提交历史
| |
| git log -p |grep "string" |
| |
| git log -p *.c |grep "string" |
| |
| git log -p >log_file.txt |
| |
| git log -p -G "$arg3" |
| |
| git log -p -G "$arg3" --name-only |
| |
| git log -p -S "string" |
查看指定文件或路径的提交历史
| git log -- <file_path> |
| |
| git show <commit_hash>:<file_path> |
查看差异时排除指定目录
| git diff ':!'$dir_exclude'' |
| git diff --stat :!'$dir_exclude'' |
| |
查看指定commit所在分支
| git branch --contains <commit-id> |
补丁生成与应用
| git diff > code.diff/code.patch |
| |
| git apply --stat code.diff/code.patch |
| |
| git apply --check code.diff/code.patch |
| |
| git apply code.diff/code.patch |
本地没有分支即 detached 状态,如何同步代码
| git pull origin remote-branch |
repo同步单代码仓
repo检查修改涉及代码仓
| repo forall -p -c git diff --stat *.c *.cc *.cpp *.h | tee repo.diff |
repo forall 执行多条命令
| |
| repo forall -p -c "git stash list |grep ${!#} |grep -o '{.*}' |sed -e 's/{//g' |sed -e 's/}//g' |xargs -I % git stash apply %" |
repo生成代码仓某个节点的快照
| |
| repo manifest -o name.xml |
| |
| repo manifest -o name.xml -r |
| |
| cp name.xml .repo/mainifest |
| |
| repo init -m name.xml |
| |
| repo sync |
repo查看不同节点xml之间的提交差异
| |
| repo diffmanifests [manifest1.xml [manifest2.xml] [options] |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
2017-12-01 【转】轻松记住大端小端的含义(附对大端和小端的解释)