---
title: "Git合并两个commit"
author: "weistar"
date: "2015-03-06T11:04:19Z"
tags: []
categories: ["开源软件"]
slug: "git-commit"
draft: true
lastmod: "2015-03-06T11:04:19Z"
id: 2189293
url: "https://weistar.iteye.com/blog/2189293"
---

一个同事让我把他的repository若干个commit变成一个，我用git rebase -i合并到最后发现剩下只有两个commit的时候，git rebase -i不再起作用，于是我求助了git maillist,果然很快有人给出了答案:

$ git reset --soft HEAD^1   只回退commit,保留change和index

$ git commit -a --amend

$ git push origin --force

 

    **git reset --mixed**：此为默认方式，不带任何参数的git reset，即这种方式，它回退到某个版本，**只保留源码，回退commit和index信息， 即保留的文件为Untracked状态。**  
    git reset --soft：回退到某个版本，**只回退了commit的信息**，不会恢复到index file一级， 即保留文件为**Tracked**状态。如果还要提交，直接commit即可

    git reset  --hard：彻底回退到某个版本，本地的源码也会变为上一个版本的内容，此命令 慎用！

    **HEAD^ 上一次提交 HEAD^^ 上两次提交**

     **HEAD~1 上一次提交 HEAD~2 上两次提交**

 

git rebase 命令的功能很强大，之前一直用它解决patch冲突。

交互式rebase(git rebase -i)提供了一个简单易用的途径让你在和别人分享提交之前对你的提交进行分割、合并或者重排序。

有时间再研究下。