gpt4 book ai didi

git - 在 github 中的 pull 请求之前,将所有提交压缩为一个

转载 作者:太空狗 更新时间:2023-10-29 12:44:36 24 4
gpt4 key购买 nike

我需要将所有提交放在一个称为初始快照的提交中,以便在 github 存储库中发布它,我知道为此我遵循 this

我的第一个问题是我想与外部存储库共享它,这说:

A word of caution: Only do this on commits that haven’t been pushed an external repository. If others have based work off of the commits that you’re going to delete, plenty of conflicts can occur. Just don’t rewrite your history if it’s been shared with others.

我想将所有提交放在一个提交中会发生什么,如何做到这一点:

我还发现了这个:

# Switch to the master branch and make sure you are up to date.
git checkout master
git fetch # this may be necessary (depending on your git config) to receive updates on origin/master
git pull

# Merge the feature branch into the master branch.
git merge feature_branch

# Reset the master branch to origin's state.
git reset origin/master

# Git now considers all changes as unstaged changes.
# We can add these changes as one commit.
# Adding . will also add untracked files.
git add --all
git commit

但什么也没发生:

$ git reset  origin/master
$ git add --all
$ git commit -m "initial snapshot"
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

最佳答案

最简单的方法是使用 rebase 命令。

假设您有这个存储库:

$> git log --oneline
af28aeb Another test
a680317 Try something
d93792b Edit both files
f23cdbd Second commit add b
6f456bc First commit add a

所以你已经完成了一些测试,提交 af28aeb 另一个测试a680317 Try something。我们想在 d93792b 编辑这两个文件 以清理存储库之后压缩它们。

要做到这一点,命令将是git rebase -i d93792b

其中-i表示进入交互模式,d93792b是我们要吸收前一个的commit hash。

注意:如果您想像第一个一样压缩所有提交,则必须使用 git rebase --root -i

该命令将向您显示:

pick a680317 Try something
pick af28aeb Another test

# Rebase d93792b..af28aeb onto d93792b ( 2 TODO item(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

你必须告诉 rebase 命令你想做什么。在这种情况下,我建议您改写第一个提交并压缩第二个提交,如下所示:

reword a680317 Try something
squash af28aeb Another test

# Rebase d93792b..af28aeb onto d93792b ( 2 TODO item(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

然后您的文本编辑将被打开以设置新的提交消息。

Fix bug

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Tue Jul 28 08:40:04 2015 +0200
#
# rebase in progress; onto d93792b
# You are currently editing a commit while rebasing branch 'master' on 'd93792b'.
#
# Changes to be committed:
# new file: c
#

现在你必须 git commit --amendgit rebase --continue 来完成这个过程。

您的存储库将显示如下:

$> git log --oneline
5f98806 Fix bug
d93792b Edit both files
f23cdbd Second commit add b
6f456bc First commit add a

关于git - 在 github 中的 pull 请求之前,将所有提交压缩为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31668794/

24 4 0
文章推荐: android - 如何使用表面 View 删除标题栏以在 android 中添加相机覆盖
文章推荐: javascript - 如何使 superfish 下拉菜单响应?
文章推荐: Android Galaxy S4 使用 values-large?
文章推荐: javascript - jQuery:在