Git Squashing Commits: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Git Operations =Overview=")
 
 
(11 intermediate revisions by the same user not shown)
Line 2: Line 2:


* [[Git Operations#Procedures|Git Operations]]
* [[Git Operations#Procedures|Git Operations]]
* [[Git_Rebasing#Squashing_Commits|Git Rebasing]]
* [[Git_Concepts#Rewriting_History|Rewriting History]]


=Overview=
=Procedure=
 
To squash the most 2 recent commits on the current branch:
 
git rebase -i HEAD~2
 
This will enter an interactive editor, that will allow to specify the commits to squash. In the editor, replace the words "pick" with "s" (or "squash") next to the commits you want to squash into the commit before it. The commits are listed from old to new, so "before" means the commit above the one we want to squash.
 
Do not edit comments yet, you will be given a chance later.
 
Upon applying repository changes, the Git client will give us a change to review and modify the comment associated with the commit. This will be done interactively in the editor:
 
<syntaxhighlight lang='text'>
# This is a combination of 2 commits.
# This is the 1st commit message:
 
Existing comment for commit 1
 
# This is the commit message #2:
 
Existing comment for commit 2
 
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
</syntaxhighlight>
 
After saving, Git might warn that the local branch and the upstream branch have diverged, and you have 'x' and 'y' different commits each respectively.
 
{{Warn|Do not pull - your squashed commits will be overwritten - push --force instead, as shown below.}}
 
To reconcile the local and the remote branch, push with "--force" (otherwise the push will be rejected because the tip of your current branch is behind its remote counterpart)
 
git push --force

Latest revision as of 00:26, 7 August 2019

Internal

Procedure

To squash the most 2 recent commits on the current branch:

git rebase -i HEAD~2

This will enter an interactive editor, that will allow to specify the commits to squash. In the editor, replace the words "pick" with "s" (or "squash") next to the commits you want to squash into the commit before it. The commits are listed from old to new, so "before" means the commit above the one we want to squash.

Do not edit comments yet, you will be given a chance later.

Upon applying repository changes, the Git client will give us a change to review and modify the comment associated with the commit. This will be done interactively in the editor:

# This is a combination of 2 commits.
# This is the 1st commit message:

Existing comment for commit 1

# This is the commit message #2:

Existing comment for commit 2

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

After saving, Git might warn that the local branch and the upstream branch have diverged, and you have 'x' and 'y' different commits each respectively.


Do not pull - your squashed commits will be overwritten - push --force instead, as shown below.

To reconcile the local and the remote branch, push with "--force" (otherwise the push will be rejected because the tip of your current branch is behind its remote counterpart)

git push --force