git合并历史提交

简介:

一些简单的命令准备

合并分支的命令是rebase,除此之外,其他的一些命令也应该知晓。

查看commit历史

git log

查看当前状态

git status

添加所有文件

git add .

提交修改

git commit -m "本次提交添加了xxxx"

vim的简单指令:

参阅vim的简单使用

准备一个测试repo

git init test-rebasecd test-rebase

提交一个文件多次:

vim test.txt
//输入第一次提交。   

git add test.txt
git commit -m "1"vim test.txt
//输入第2次提交。   

git add test.txt
git commit -m "2"vim test.txt
//输入第3次提交。   

git add test.txt
git commit -m "3"

查看log:

git log//////commit 0353373749d72e53a34c7bdda86d77d7bb3ca6feAuthor: ryan <v-rmiao@expedia.com>Date:   Wed Jul 19 13:23:18 2017 +0800

    3commit acf6d24adc2097fda82d29064e8edfef6355d01dAuthor: ryan <v-rmiao@expedia.com>Date:   Wed Jul 19 13:20:37 2017 +0800

    2commit 2169bc5e20386951b19aff32143e74f2da683df2Author: ryan <v-rmiao@expedia.com>Date:   Wed Jul 19 13:19:42 2017 +0800

    1

可以看到有三次提交了。现在我们想要把第2次和第3次提交的内容合并成一次提交。

开始rebase

1. 复制合并前的一次提交的hash

这里就是第一次提交的hash。即2169bc5e2

2. git rebase -i xxx

git rebase -i 2169bc5e2

进入历史提交的编辑页面,此时编辑方式为vim。

pick acf6d24 2pick 0353373 3# Rebase 2169bc5..0353373 onto 2169bc5 (2 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

可以看到第2次和第3次的提交消息,并且是从old->new来排序的。我们需要把第3次提交合并到第2次上。使用squash.

squash

修改第三次提交为squash,意思是和前一次(第二次)提交合并。
键盘按键j移动到第二行,然后按a开始编辑,删除pick,插入squash
如下:

pick acf6d24 2squash  0353373 3# Rebase 2169bc5..0353373 onto 2169bc5 (2 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

然后,按esc退出编辑,再按:,输入wq保存。
这时候会进入第二个vim页面,这里让我们再次修改commit message的。就是合并后的message。

# This is a combination of 2 commits.这是合并后的message,以下是之前合并的历史# This is the 1st commit message:2# This is the commit message #2:3# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.## Date:      Wed Jul 19 13:20:37 2017 +0800## interactive rebase in progress; onto 2169bc5# Last commands done (2 commands done):#    pick acf6d24 2#    squash 0353373 3# No commands remaining.# You are currently editing a commit while rebasing branch 'master' on '2169bc5'.## Changes to be committed:

还是和刚才一样,按o插入下一行,输入这次合并的message。然后按esc,按:, 输入wq保存并退出。

完事,再次查看log

git log

内容如下:

commit 8f54e6b5643ff26ac967a9e6e6cded68a6c50906Author: ryan <v-rmiao@expedia.com>Date:   Wed Jul 19 13:20:37 2017 +0800

    这是合并后的message,以下是之前合并的历史    2

    3commit 2169bc5e20386951b19aff32143e74f2da683df2Author: ryan <v-rmiao@expedia.com>Date:   Wed Jul 19 13:19:42 2017 +0800
本文转自  zddnd   51CTO博客,原文链接:http://blog.51cto.com/13013666/1949244
相关文章
|
5月前
|
开发工具 git
Git操作远程仓库及解决合并冲突
Git操作远程仓库及解决合并冲突
88 0
|
7月前
|
开发工具 git
IDEA中Git面板操作介绍 变基、合并、提取、拉取、签出
在IDEA的Git面板中,仓库会分为本地仓库和远程仓库,代码仓库里面放的是各个分支。
563 2
|
4月前
|
存储 开发工具 git
Pycharm git-创建本地仓库\创建分支\合并分支\回溯版本\加入git后文件颜色代表的含义
Pycharm git-创建本地仓库\创建分支\合并分支\回溯版本\加入git后文件颜色代表的含义
73 0
|
5月前
|
数据可视化 Go 开发工具
cggit 简化 Git 提交、合并、分支偏移小神器,提升开发、修BUG效率!
cggit 简化 Git 提交、合并、分支偏移小神器,提升开发、修BUG效率!
42 0
|
21天前
|
开发工具 git
git 拉取代码仓库代码报错(合并错误 refusing to merge unrelated histories)
git 拉取代码仓库代码报错(合并错误 refusing to merge unrelated histories)
25 0
|
6月前
|
开发工具 git Windows
Git分支新建与合并案例实操(结合IDEA讲解)
Git分支新建与合并案例实操(结合IDEA讲解)
167 0
|
2月前
|
Shell 开发工具 git
【问题篇】git创建分支后idea切换分支找不到以及合并问题
【问题篇】git创建分支后idea切换分支找不到以及合并问题
33 0
|
3月前
|
存储 开发工具 git
Git 教程:解密 .gitignore 文件、合并分支、解决冲突、及 Git 帮助
如果你忘记了命令或命令的选项,你可以使用 Git 帮助。 在命令行中,有几种不同的使用帮助命令的方式: git command -help - 查看特定命令的所有可用选项 git help --all - 查看所有可能的命令 让我们看看不同的命令。
214 3
|
3月前
|
开发工具 git
深入探索Git的高级技巧与神奇操作(分支,高效合并)
深入探索Git的高级技巧与神奇操作(分支,高效合并)
91 0

相关实验场景

更多