最新消息:天气越来越冷,记得加一件厚衣裳

Git合并远程提交记录Commit

基础应用 w3sun 2195浏览 0评论

背景

通过GitLab或者GitHub进行代码版本管理已经在各个公司已经非常普及,代码开发过程中为了保证分支代码一直处于较新状态很多公司都要求当天开发代码必须在下班前完成提交这就会导致commit过多不仅不美观同时也容易阅读,此时我们可以通过合并多个commit保证一个功能只有一条记录以保持变更记录的简洁。

查看历史提交

首先查看提交历史记录:

git log --oneline

c1d0de3 (HEAD -> master, origin/master) this is fifth time commit
a4ee05a this is forth time commit
0349106 this is third time commit
7104d43 this is second time commit
9d497fe this is first time commit

squash历史提交

可以看到git上按提交时间从新到旧有5次提交,假设需要把second ~ fifth共4次提交进行合并,操作如下:

git rebase -i 9d497fe

需要注意的是[kbd] git rebase[/kbd] 区间选取是新闭旧开,所以要合并 commit second 到 fifth需要要选择commit first作为基准。执行上述命令后进入修改页面

pick 7104d43 this is second time commit
pick 0349106 this is third time commit
pick a4ee05a this is forth time commit
pick c1d0de3 this is fifth time commit

# Rebase 9d497fe..c1d0de3 onto 9d497fe (4 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

与git log查看的commit记录不同,执行完[kbd]git rebase -i 9d497fe[/kbd]后看到的排序方式是从旧到新。选择需要合并的commit将pick更改为squash或者s然后保存即可,注意需要从最新提交向历史提交方向进行squash。

pick 7104d43 this is second time commit
squash 0349106 this is third time commit
squash a4ee05a this is forth time commit
squash c1d0de3 this is fifth time commit

更改历史comment

点击保存后会自动弹出comment修改窗口:

# This is a combination of 4 commits.
# This is the 1st commit message:
merge 4 commit one time

#this is second time commit

# This is the commit message #2:

#this is third time commit

# This is the commit message #3:

#this is forth time commit

# This is the commit message #4:

#this is fifth time commit

将不需要的comment注释掉换成新的注释即可通过git log –oneline查看历史log发现已经将多个历史commit进行了合并。

deade4c (HEAD -> master) merge 4 commit one time
9d497fe this is first time commit

此时更改推到远程仓库即可

git push -f
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (7/7), 496 bytes | 496.00 KiB/s, done.
Total 7 (delta 0), reused 0 (delta 0)
To https://github.com/bilpam/snap.git
 + c1d0de3...deade4c master -> master (forced update)

转载请注明:雪后西塘 » Git合并远程提交记录Commit

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址