简体   繁体   English

如何加入Git提交历史

[英]How to join Git commits back in the history

I have the following ugly history (the repeated messages come from my very beginning use of github). 我有以下丑陋的历史(重复的消息来自我对github的最开始的使用)。

ba7e5ec PPath begins...
be327e8 Update README.md
585b586 Update README.md
4d2ed26 Update README.md
834b3b6 Update README.md
6c7da1a Update README.md
9d89b1b First version.

I would like to have something like this. 我想要这样的东西。

xxxxxxx PPath begins...
yyyyyyy Update README.md
9d89b1b First version.

Is it possible ? 可能吗 ? The idea is to squash some commits but not necessarily the last ones. 想法是压缩一些提交,但不一定是最后一个。

The following instructions will help you make a step-by-step changes leading just to the desired result. 以下说明将帮助您逐步进行更改,以达到所需的结果。

There are two ways to achieve the result. 有两种方法可以达到目的。

With git rebase -i 使用git rebase -i

This is faster but requires using a text editor. 这更快,但是需要使用文本编辑器。

git checkout master
git branch backup
#pick the sha1 of a commit before the one that will consume further commits.
git rebase -i 9d89b1b

This opens an editor with the text. 这将打开带有文本的编辑器。 Change it this way: 以这种方式更改它:

pick 6c7da1a Update README.md
squash 834b3b6 Update README.md
squash 4d2ed26 Update README.md
squash 585b586 Update README.md
squash be327e8 Update README.md
pick ba7e5ec PPath begins...
...

Now save and exit. 现在保存并退出。 If you're in vim, that's how to exit . 如果您在vim中, 那是退出的方法 A new editor opens with commit message. 将打开一个新的编辑器,并带有提交消息。 You can leave it intact or change to whatever you think is good. 您可以保持原样,也可以更改为您认为不错的任何东西。

# This is a combination of 3 commits.
# The first commit's message is:
Update README.md
...

Save and exit again. 保存并再次退出。 Job's done. 工作完成了。

With git reset --soft. 使用git reset --soft。

This is longer but doesn't require side tools. 这是更长的时间,但不需要辅助工具。

#start with a backup
git checkout master
git branch backup

#this returns head to be327e8 and brings the difference into index.
git reset --soft be327e8

# save that index for future use
git stash save

#same trick again
git reset --soft 9d89b1b

#recommit our changes
git commit -m'Update readme.md'

# now use the stashed changes
git stash apply
git commit -m'PPath begins...'

# check the result
git log --oneline

# now if everything is ABSOLUTELY FINE, you can delete the backup
git stash drop

A few links to explain what's going on: 一些链接来说明发生了什么:
Can you explain what "git reset" does in plain english? 您能用简单的英语解释“ git reset”是什么吗?
Git cheatsheet Git备忘单

In any case, make sure you understand the consequences of history rewriting (read eg The perils of rebasing ). 在任何情况下,请确保您了解历史记录重写的后果(请阅读例如变基的危险 )。

As an alternative to the other answer using git reset --soft , you may consider using git rebase -i . 作为使用git reset --soft的其他答案的替代方案,您可以考虑使用git rebase -i In its simplest form, run the command without other arguments, you'll get a todo-list in a text editor, and mark as fixup the commits you want to squash into their predecessor. 以最简单的形式,在不使用其他参数的情况下运行命令,您将在文本编辑器中获得待办事项列表,并将要压入其前身的提交标记为fixup Save and exit, git will do the rest. 保存并退出,其余的将由git完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM