如何正确使用git rebase?(How to use git rebase properly?)

我目前使用git方式如下:

克隆,获取回购 为特定问题创建主题分支 做一些提交 结帐主分公司 将主题分支合并到master 推新主分公司

不幸的是,这时不时会变得混乱。 我想要做的是将一些较小的提交合并到一个较大的提交。

此外,将新分支中的所有提交合并到一个提交并将其合并到主服务器有时也是个好主意。

例:

git branch update-docs git checkout update-docs git add -A . git commit -m 'updated networking doc' git add -A . git commit -m 'updated manager doc' git checkout master git merge update-docs

如您所知,我将在主分支“更新网络文档”和“更新管理器文档”中进行两次提交。

如果我现在只想在master分支中有一个名为“updated docs”的提交,例如包含这两个提交的更改,我该怎么办?

我认为你必须使用git rebase来完成这类任务。 我只是不知道如何使用它。

我已经尝试过:

git checkout master git rebase update-docs

但是这些更改合并到master而不留下任何提交,现在repo搞砸了。

那么有人会分享你必须使用的命令吗?

解:

git checkout -b update-readme git commit -am 'rewritten readme' git commit -am 'added author name' git rebase -i master

打开文本编辑器:

pick 6dbcbf1 rewritten readme pick b815bbf added author name

现在你改成这个:

pick 6dbcbf1 rewritten readme squash b815bbf added author name

然后用“:w”和“:q”保存,如果使用vim,它将打开另一个编辑器,您可以在其中定义新的提交名称

rewritten readme

然后你合并回购:

git checkout master git merge update-readme

I currently use git the following way:

clone, fetch repo create topic branch for specific issue do some commits on it checkout master branch merge topic branch to master push new master branch

Unfortunately this is getting messy from time to time. What I would like to do is to merge some smaller commits to a bigger one.

Also it would be sometimes a good idea to merge all commits in a new branch to one commit and merge this to master.

example:

git branch update-docs git checkout update-docs git add -A . git commit -m 'updated networking doc' git add -A . git commit -m 'updated manager doc' git checkout master git merge update-docs

As you know now I would have two commits in master branch "updated networking doc" and "updated manager doc".

If I would now want only have one commit in master branch called "updated docs" for example which contains the changes of these two commits what do I have to do?

I think you have to use git rebase for this sort of task. I just do not know how to use it.

I already tried:

git checkout master git rebase update-docs

but that merged the changes to master without leaving any commits and now the repo is messed up.

So would somebody share the commands you have to use?

Solution:

git checkout -b update-readme git commit -am 'rewritten readme' git commit -am 'added author name' git rebase -i master

opens text editor:

pick 6dbcbf1 rewritten readme pick b815bbf added author name

now you change this to:

pick 6dbcbf1 rewritten readme squash b815bbf added author name

then you save with ":w" and ":q" if using vim and it will open another editor where you can define the new commit name

rewritten readme

then you merge the repo:

git checkout master git merge update-readme

最满意答案

在git merge update-docs ,执行git rebase -i origin/master 。 这将弹出一个交互式rebase编辑器 ,它允许你压缩提交,然后重新排列它们 。

After git merge update-docs, execute git rebase -i origin/master. This will pop up an interactive rebase editor, which allows you to squash commits and otherwise rearrange them.

更多推荐