Git使用总结

目录

目录

Git 使用总结

概述

一、相关变量

常用命令

二、配置管理技巧

三、代码回滚技巧

1、强制恢复到一个指定版本号gitID对应的版本

2、删除云端指定文件或目录

3、git add 之后撤销

4、git revert

5、git commit之后撤销--push之前

四、分支管理技巧:

1、创建一个新的分支

2、将新建的分支推送到远端服务器

3、删除本地仓库的分支

4、删除远端仓库的分支

5、从远端check出一个分支

6、将分支A合并到分支B-先切换到分支B再合并

7、分支查看

8、分支切换

五、Patch管理技巧

1.1、用diff命令生成patch

1.2、引用用diff生成的patch

2.1、使用标准git生成patch

2.2、使用git am应用patch

2.3、使用patch直接打补丁

冲突解决

3、使用cherry-pick打补丁

六、查看工具技巧

1、只想看某一个Author的提交记录

2、想查看每一笔提交修改了哪些文件

3、查找摸一个关键词修改log记录

4、查看某一个文件每一行修改git 记录

5、显示某一个commit 修改详情

6、查看某一个人提交简介

7、查看所有分支更新

七、git log相关命令与实例

1、git log 格式化输出

2、使用git log --help 查看详细说明

3、日期相关格式化输出

4、【实例】

4.1、格式化输出

4.2、统计某一个分支所有提交者贡献commit 数

5、git log支持常用命令

6、git log常用条件搜索

7、一套完整的git操作步骤

8、好用的alias



Git 使用总结

概述

Git是一个功能非常强大的代码管理工具,在使用git之前,我们需要理解几个以下基本概念:

1、workspace:工作区

2、Index/Stage:暂存区

3、Repository:本地仓库

4、Remote:远程仓库

如下图:

熟悉几个概念之后,接下来熟悉熟悉具体的一些常用技巧,这部分是通过实际操作经验总结出来使用比较多的,当然还有一些其他功能。这部分会慢慢补充完善,当然最好的工具还是用Linux 命令行下输入:git log --help。查看详细的说明,这里学会用/key来方便查找关键词,方便快速定位查询命令位置。

以下相关命令相关变量名可以根据自己的实际来替换,有些命令后面都做了相关解释,读者可以通过实际操作来看效果。

一、相关变量

HEAD:当前commit ID

HEAD^:上一个commit ID

HEAD^^:上上一个commit ID

常用命令

git pull // 拉远端到本地仓库,并执行合并操作=git fetch + git merge origin/master

git fetch // 只更新本地仓库

git merge // 本地仓库和代码合

二、配置管理技巧

git remote set-url origin https://xxxxxxxxxxxxxxxxxxx //设定远端url:

git config --list //显示当前git配置信息

git config user.name //显示当前user名字

git config --global user.name "UserTest" // 修改当前git用户名字

git config user.email //显示当前user 邮箱

git config --global user.email "xxxxx@xxxx" // 修改当前git 用户邮箱

三、代码回滚技巧

1、强制恢复到一个指定版本号gitID对应的版本

git reset --hard gitID // 强制恢复到指定git id

git reset --hard HEAD //强制恢复到当前git id

2、删除云端指定文件或目录

git rm -r --cached fileName/folderName

3、git add 之后撤销

git reset HEAD //撤销所有add文件

git reset HEAD fileName //撤销指定文件

4、git revert

git revert commitID //回滚指定的commit

revert和reset区别:revert有迭代作用,会作为一次新的commit提交;reset会移除远端指定的commitID。建议使用revert,防止错误回滚还可以再次回滚回来,也可以方便追踪。

5、git commit之后撤销--push之前

git reset --soft HEAD^

通过git commit -m之后,用git log就可以看到本地仓库已经有commit ID了,但是没有推送到远端,所以可以使用git reset --soft HEAD^方式回滚到git add之前,注意这里是HEAD^而不是HEAD,因为你commit之后已经产生了新的commitID,所以你要回滚必须是上一个commitID。

四、分支管理技巧:

1、创建一个新的分支

git branch newBranchName //从当前分支,当前commitID创建分支

git branch [branchNema] [commit] //从当前分支指定commitID创建分支

git checkout -b branchName //创建并切换分支

2、将新建的分支推送到远端服务器

git branch newBranchName

git push origin newBranchName

3、删除本地仓库的分支

git branch testBranch -d

Deleted branch newBranchName (was 2357943).

4、删除远端仓库的分支

git branch testBranch -D

git push origin --delete testBranch

git branch -dr [remote/branch]

5、从远端check出一个分支

git checkout remoteBranchName

6、将分支A合并到分支B-先切换到分支B再合并

git checkout branchB git merge branchA

7、分支查看

git branch -r //查看远程分支

git branch //查看本地分支

git branch -a //查看本地+远程分支

8、分支切换

git checkout branchName //切换到指定的分支

git checkout - //切换到上一个分支

五、Patch管理技巧

1.1、用diff命令生成patch

git diff > patchName

git diff --cached > patchName

git diff branchName --cached > patchName

1.2、引用用diff生成的patch

git apply patch

git apply --check // 查看补丁是否干净顺利的应用到当前分支,如果运行后没有任何输出,表示已经顺利采用该补丁

git apply --summary --stat --apply --whitespace=nowarn --ignore-whitespace patchName.patch

2.1、使用标准git生成patch

git format-patch -1 commit_id //单个commit 生成的patch

git format-patch [-n] -M master //master分支最新n次提交生成补丁,如果不指定默认当前分支

git format-patch <r1> //从r1以来的patch,但不包含该commit

git format-patch -n //最近N次生成的patch

git format-patch HEAD^ //最近一次commit的patch

git format-patch HEAD^^ // 最近两次commit的patch

git format-patch HEAD^^^ //最近三次的commit的patch,同样以此类推

git format-patch <r1>..<r2> //r1commit和r2commit之间的版本,注意版本commitid之间只有..没有空格,不包含r1,包含r2

2.2、使用git am应用patch

git apply --s tat xxx.patch //先检查patch文件

git apply --check 0001xxxx.patch //检查能否应用

git am xxxxxx.patch //打补丁文件

git am -3 --whitespace=nowarn --ignore-whitespace patchName.patch

2.3、使用patch直接打补丁

patch -N -r - -p1 < 0001-xxx.patch

        使用前面的git 方式打补丁,本地的git会跟着生长,直接使用Patch方式只是把补丁打进去,但是git并不会跟着生长。比如我有一个针对某一个特殊案例的修改,但是我又不想让它提交到git上影响其他的工程,那就可以用这种用的时候打补丁的方式来执行。 

冲突解决

当我们打补丁出现冲突的时候,这个时候需要我们手动解决冲突。

第一步:首先,执行以下命令,自动合入 patch 中不冲突的代码,同时保留冲突的部分

git apply --reject xxxx.patch

同时会生成后缀为 .rej 的文件,保存没有合并进去的部分的内容,可以参考这个进行冲突解决。

第二步:2、解决完冲突后删除后缀为 .rej 的文件,并执行 并执行git add.添加改动到缓存区

第三步: 执行git am --resolved,最后 push 上去。

3、使用cherry-pick打补丁

你在分支A的commit1,如果想打分支B的commit2,先在分支pull到最新,更新本地仓库缓存

git pull

git cherry-pick commitID

六、查看工具技巧

1、只想看某一个Author的提交记录

git log --author=authorName

2、想查看每一笔提交修改了哪些文件

git log --name-only

3、查找摸一个关键词修改log记录

git log --grep='test' [path/file]

4、查看某一个文件每一行修改git 记录

git blame fileName

5、显示某一个commit 修改详情

git show commitID

6、查看某一个人提交简介

git log --pretty=oneline --abbrev-commit --author="James"

7、查看所有分支更新

git fetch -p

七、git log相关命令与实例

1、git log 格式化输出

选项

说明

%H

提交对象的完整哈希字符串

%h

提交对象简短哈希串

%T

树对象完整哈希字串

%t

树对象简短哈希字串

%P

父对象完整哈希字串

%p

父对象简短哈希字串

%an

作者名称author name

%ae

作者地址author Email

%ad

作者修订日期(可用--date=选项指定格式)

%ar

作者修订日期,按多久以前方式显示

%cn

提交者名字commit Name

%ce

提交者邮件commit Email

%cd

提交者日期commit date

%cr

提交日期,按多久以前显示

%s

提交说明

2、使用git log --help 查看详细说明

%H: commit hash

%h: abbreviated commit hash

%T: tree hash %t: abbreviated tree hash

%P: parent hashes

%p: abbreviated parent hashes

%an: author name

%aN: author name (respecting .mailmap, see git-shortlog(1) or git-blame(1))

%ae: author email %aE: author email (respecting .mailmap, see git-shortlog(1) or git-blame(1))

%ad: author date (format respects --date= option)

%aD: author date, RFC2822 style

%ar: author date, relative %at: author date, UNIX timestamp

%ai: author date, ISO 8601 format

%cn: committer name

%cN: committer name (respecting .mailmap, see git-shortlog(1) or git-blame(1))

%ce: committer email

%cE: committer email (respecting .mailmap, see git-shortlog(1) or git-blame(1))

%cd: committer date

%cD: committer date, RFC2822 style

%cr: committer date, relative

%ct: committer date, UNIX timestamp

%ci: committer date, ISO 8601 format

%d: ref names, like the --decorate option of git-log(1)

%e: encoding

%s: subject

%f: sanitized subject line, suitable for a filename

%b: body

%B: raw body (unwrapped subject and body)

%N: commit notes

%gD: reflog selector, e.g., refs/stash@{1}

%gd: shortened reflog selector, e.g., stash@{1}

%gn: reflog identity name

%gN: reflog identity name (respecting .mailmap, see git-shortlog(1) or git-blame(1))

%ge: reflog identity email

%gE: reflog identity email (respecting .mailmap, see git-shortlog(1) or git-blame(1))

%gs: reflog subject %Cred: switch color to red

%Cgreen: switch color to green

%Cblue: switch color to blue

%Creset: reset color %C(...): color specification, as described in color.branch.* config option %m: left, right or boundary mark

%n: newline

%%: a raw %

%x00: print a byte from a hex code

%w([<w>[,<i1>[,<i2>]]]): switch line wrapping, like the -w option of git-shortlog(1).

3、日期相关格式化输出

--date=(relative|local|default|iso|rfc|short|raw) Only takes effect for dates shown in human-readable format, such as when using "--pretty". log.date config variable sets a default value for log command’s

--date option. --date=relative shows dates relative to the current time, e.g. "2 hours ago".

--date=local shows timestamps in user’s local timezone.

--date=iso (or --date=iso8601) shows timestamps in ISO 8601 format.

--date=rfc (or --date=rfc2822) shows timestamps in RFC 2822 format, often found in E-mail messages.

--date=short shows only date but not time, in YYYY-MM-DD format.

--date=raw shows the date in the internal raw git format %s %z format.

--date=default shows timestamps in the original timezone (either committer’s or author’s).

4、【实例】

4.1、格式化输出

git log --pretty=format:"ID:%h %cd %cn: %s"

git log --pretty=format:"%Creset %CredID:%h %Cblue[%cn] %Cgreen%cd : %Cblue%s"

git log --date=short --pretty=format:"%Cred %cd %cn %Cgreen %s %Cblue %d"

4.2、统计某一个分支所有提交者贡献commit 数

git log | grep "^Author" | awk -F" " '{print$2}' | sort | uniq -c | sort -nr

5、git log支持常用命令

选项

说明

-p

按补丁格式显示每个更新之间差异

--word-diff

按word diff格式显示差异

--stat

显示每次更新修改的统计信息

--shortstat

显示--stat中最后的行数修改添加移除统计

--name-only

仅在提交信息之后显示修改的文件清单

--name-status

显示新增、修改、删除的文件清单

--abbrev-commit

仅显示SHA-1的前几个字符,而非所有40个字符

--relative-date

使用简短的相对时间显示(如:2 weeks ago)

--graph

显示ASCII图形表示的分支合并历史

--pretty

使用其他格式显示提交者信息,可以用包括oneline、short、full、fuller、formate(指定格式)

--oneline

--pretty=oneline --abbrev-commit的简化用法

--decorate

6、git log常用条件搜索

选项

说明

-n

仅显示最近n条提交

--since,--after

仅显示指定时间之后的提交

--until,--before

仅显示指定时间之前的提交

--author

仅显示指定作者的提交

--committer

仅显示指定提交者的提交

7、一套完整的git操作步骤

git status | grep : // 查看修改了哪些文件

git add fileName... // 将修改的需要提交的文添加到暂存区

git commit -m "xxxxx" // 增加说明并提交到本地仓库,此时本地仓库的git id已经更新

git push orignial branchName // 提交到远端指定的branchName分支

8、好用的alias

在Linux用户的~/.gitconfig 添加

[alias] lg1 = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all lg3 = log --graph --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' br = branch mtr = branch master ck = checkout ss = status -uno logme = log --no-merges --stat --author=YOUR_ACCOUNT --name-only --pretty=format:'Hash:%h Date:%cd\nAuthor:%an \nComment:%s' logwho = "!sh -c \"git log --no-merges --stat --author=$1 --name-only --pretty=format:'Hash:%h Date:%cd Author:%an \nComment:%s'\"" logfile = log --name-status lgmaster = log --no-merges --stat --graph --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all

设定好之后,我们可以在终端输入

git lg1

这个时候就可以看到我们自定义的格式化输出。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值