如何压缩git提交?

huangapple go评论57阅读模式
英文:

How do I squash git commits?

问题

我有4个提交记录已经提交到GitHub。我想将它们合并成1个提交。

我尝试了以下命令:git rebase -i HEAD~4

然而,这个命令给我返回了一个包含我的4个提交以外的大量提交记录的列表。换句话说,这个列表包括不属于我的提交记录。列表看起来像这样:

pick 其他提交记录
pick 我的提交记录 1
pick 其他提交记录
pick 其他提交记录
pick 我的提交记录 2
pick 我的提交记录 3
pick 其他提交记录
pick 我的提交记录 4

我如何只合并我的4个提交记录?

英文:

I have 4 commits that I have committed to github. I would like to squash them into 1 commit.

I have tried the following command: git rebase -i HEAD~4

This however gives me a large list of commits in addition to my 4. In other words, the list includes commits that are not mine. The list looks something like:

pick some other commit
pick my commit 1
pick some other commit
pick some other commit
pick my commit 2
pick my commit 3
pick some other commit
pick my commit 4

How do I squash only my 4 commits?

答案1

得分: 0

有两种基本方法。

第一种方法是:重新排列你的行,使它们都相邻,并将 'pick' 替换为 'squash',对于你的四个提交中的后三个。然后保存并退出文件。如果没有其他提交与你的工作冲突,这可能是最简单的方法。

第二种方法是:从主分支创建一个新分支,将你的所有提交都挑选到这个新分支上,然后重新执行 rebase。现在你的所有提交应该都是相邻的,这样更容易压缩(将 'pick' 替换为 'squash',对于你的四个提交中的后三个)。

英文:

There are two basic ways.

The first is: reorder your lines so that they are all adjacent, and replace 'pick' with 'squash' for the bottom three of your four commits. Then save and exit the file. This may be easiest, if none of the other commits conflict with your work.

The second is: create a new branch from master, cherry-pick all of your commits onto this new branch, and then redo the rebase. All of your commits should be adjacent now, making them easier to squash (replace the word 'pick' with 'squash' for the bottom three of your four commits).

答案2

得分: 0

将后续提交的 "pick" 更改为 "squash" 应该可以正常工作

squash my commit 1
squash my commit 2
squash my commit 3
squash my commit 4
英文:

Change pick to squash for later commits should work

pick my commit 1
squash my commit 2
squash my commit 3
squash my commit 4

答案3

得分: 0

我已经在主分支上进行了更改。为了解决这个问题,我使用以下命令从远程仓库还原了主分支。

1. git checkout master

2. git pull -r

3. git fetch presto

4. git reset --hard presto/master

5. git push origin master --force

然后我检出了一个新的分支并重新进行了我的更改。

git checkout -b [分支名称]

要推送后续更改,我使用了修订提交。

1. git add .
2. git commit --amend -m "[提交描述]"
3. git push -f origin [分支名称]
英文:

I had made my changes on the master branch. To resolve the issue, I restored the master branch from the remote repository using the below commands

1. git checkout master

2. git pull -r

3. git fetch presto

4. git reset --hard presto/master

5. git push origin master --force

I then checked out a new branch and re-did my changes.

git checkout -b [branch name]

To push subsequent changes I used amend

1. git add .
2. git commit --amend -m "[commit description]" 
3. git push -f origin [branch name]

huangapple
  • 本文由 发表于 2023年7月7日 07:28:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76633082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定