git将多个commit合并成一个commit提交

使用场景:在自己的开发分支上开发一个功能提交多次,有多个commit,想将多次提交的commit合并成一个commit,方便代码管理。

例如将最近四次提交的commit合并成一个

1、查看提交历史记录

git log

2、回退到第5个提交,将后面4次提交的内容变为未提交状态

git reset commitID(第五个commit的ID)  //git reset 等同于git reset --mixed

3、提交修改内容

git add .
git commit -m "将前四个commit合并成一个"

4、将代码提交到远程分支

$ git push origin dev_branch
To [email protected]:rubik-x/rubik-project.git
 ! [rejected]        dev_branch -> dev_branch (non-fast-forward)
error: failed to push some refs to '[email protected]:rubik-x/rubik-project.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

5、无法提交,因为本地没有远端的4个commit,导致不一致无法推送,因此需要强行推上去

git push -f origin dev_branch

到此完成,是不是很方便。

你可能感兴趣的:(Linux,git)