git中将多次commit合并为一次commit

有的时候我们提交了多次commit,但是这些历史记录我们没有必要都要放到远程服务器上,在推送到远端时,需要在合并的时候先合并一下

多次提交

首先我们在master分支上创建一个新分支,叫dev

hui.qian@HUIQIANPC /e/testerhome/AndroidTestScrpits (master)
$ git checkout -b dev
Switched to a new branch 'dev'

然后我们在该分支上提交三次更新,分别取名为first commit 、second commit、third commit。

commit f1d89aaba5b3436301a271310332290c8db3ccd0
Author: unknown .qian@HuiQianPC.spreadtrum.com>
Date:   Tue Mar 3 14:13:18 2015 +0800

    third commit

commit dfa93ec3079ed2083d1c30bf9d858bcb9e8901e3
Author: unknown .qian@HuiQianPC.spreadtrum.com>
Date:   Tue Mar 3 14:12:54 2015 +0800

    second commit

commit 8d402953d95f166f86a5e4b91ab3fa8bc5cf540c
Author: unknown .qian@HuiQianPC.spreadtrum.com>
Date:   Tue Mar 3 14:12:17 2015 +0800

    first commit

合并为一次提交

首先切换到master分支:

$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

然后要合并dev分支的修改到master分支,但是此时还要加上–squash参数.

hui.qian@HUIQIANPC /e/testerhome/AndroidTestScrpits (master)
$ git merge dev --squash
Updating edb35e7..f1d89aa
Fast-forward
Squash commit -- not updating HEAD
 README.md | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

hui.qian@HUIQIANPC /e/testerhome/AndroidTestScrpits (master)
$ git commit -m "update readme.md"
[master 557d109] update readme.md
 Committer: unknown 
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email [email protected]

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+), 3 deletions(-)

hui.qian@HUIQIANPC /e/testerhome/AndroidTestScrpits (master)
$ git log
commit 557d1095b610b0cbb26d679528af551a6bcdb10f
Author: unknown 
Date:   Tue Mar 3 14:24:29 2015 +0800

    update readme.md

这个地方一定要注意,git merge后一定要commit一下。

你可能感兴趣的:(git中将多次commit合并为一次commit)