Git commit: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(17 intermediate revisions by the same user not shown)
Line 6: Line 6:


=Overview=
=Overview=
{{Internal|Git_Concepts#Commit|Git Concepts | Commit}}
=Listing Commits=
{{Internal|Git log|git log}}


=Apply Extra Changes to the Last Commit=
=Apply Extra Changes to the Last Commit=


If you are in the situation where you committed changes on your current branch, but then you worked a little bit more and want to include these latest changes into the last commit, you can use an "amend commit". This is equivalent with committing one more time and squashing the last two commits with [[Git Squashing Commits#Procedure|git rebase -i HEAD~1]]:
If you are in the situation where you committed changes on your current branch, but then you worked a little bit more and want to include these latest changes into the last commit, you can use an "amend commit". An amend commit rplaces the tip of the current branch by creating a new commit. The recorded tree is prepared as usual and the message from the original commit is used as the starting point, instead of an empty message, when no other message is specified from the command line via -m. The new commit has the same parents and author as the current one. This is equivalent with committing one more time and squashing the last two commits with [[Git Squashing Commits#Procedure|git rebase -i HEAD~1]]:


  git add .
  git add .
  git commit --amend
  git commit --amend [ --no-edit]
 
By default, without  --no-edit, an interactive editor will be launched to give the committer the possibility to amend the commit message as well. To avoid starting an editor, use " --no-edit".


{{Warn|If the last, existing commit that has been amended was pushed in a remote repository, you will need to overwrite it in the remote repository - thus rewriting history - with:
'''Possible History Rewrite''':
<span id='History_Rewrite'></span>
{{Warn|If the last, existing commit that has been amended was pushed in a remote repository, you will need to overwrite it in the remote repository - thus [[Git_Concepts#Rewriting_History|rewriting history]] - with:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
  git push --force
  git push --force
</syntaxhighlight>
</syntaxhighlight>
This is not necessary if the last commit was not pushed yet.
  This is not necessary if the last commit was not pushed yet.
}}
}}


=Update a Commit Message that Has Not Been Pushed Yet=
=Update a Commit Message that Has Not Been Pushed Yet=
<syntaxhighlight lang='bash'>
git commit --amend
</syntaxhighlight>
It will start an interactive editor.
To reset the author:
<syntaxhighlight lang='bash'>
git commit --amend --reset-author
</syntaxhighlight>
To rewrite the author:
<syntaxhighlight lang='bash'>
git commit --amend --author 'Ovidiu Feodorov <ovidiu@example.com>'
</syntaxhighlight>
=Update the Commit Message of a Specific Commit=
git rebase -i "<''commit-id''>^"
Note the caret at the end, this is needed because you will need to rebase back to the commit '''before''' the one you wish to modify.
In the interactive editor, replace "pick" with "e" ("edit") for the commit you wish to change.
The git rebase procedure will exit and allow you to:
<syntaxhighlight lang='text'>
You can amend the commit now, with
  git commit --amend
Once you are satisfied with your changes, run
  git rebase --continue
</syntaxhighlight>
Execute:


  git commit --amend
  git commit --amend


It will start an interactive editor.
interactively update the comment, then
 
git rebase --continue
 
=Revert Individual Files to a Specific Past Commit=
{{Internal|Git_checkout#Reverting_Individual_File.28s.29_to_a_Specific_Past_Commit|Revert Individual Files to a Specific Past Commit}}


=Deleting Commits from History=
=Deleting Commits from History=
{{Internal|Git Deleting Commits from History|Deleting Commits from History}}
{{Internal|Git Deleting Commits from History|Deleting Commits from History}}
=Unstage a Local Commit=
<syntaxhighlight lang='bash'>
git reset --soft HEAD~1
</syntaxhighlight>
=Adding an Empty Commit=
<syntaxhighlight lang='bash'>
git commit -m 'Some commit message' --allow-empty
</syntaxhighlight>

Latest revision as of 23:07, 26 September 2024

External

Internal

Overview

Git Concepts | Commit

Listing Commits

git log

Apply Extra Changes to the Last Commit

If you are in the situation where you committed changes on your current branch, but then you worked a little bit more and want to include these latest changes into the last commit, you can use an "amend commit". An amend commit rplaces the tip of the current branch by creating a new commit. The recorded tree is prepared as usual and the message from the original commit is used as the starting point, instead of an empty message, when no other message is specified from the command line via -m. The new commit has the same parents and author as the current one. This is equivalent with committing one more time and squashing the last two commits with git rebase -i HEAD~1:

git add .
git commit --amend [ --no-edit]

By default, without --no-edit, an interactive editor will be launched to give the committer the possibility to amend the commit message as well. To avoid starting an editor, use " --no-edit".

Possible History Rewrite:


If the last, existing commit that has been amended was pushed in a remote repository, you will need to overwrite it in the remote repository - thus rewriting history - with:
 git push --force

This is not necessary if the last commit was not pushed yet.

Update a Commit Message that Has Not Been Pushed Yet

git commit --amend

It will start an interactive editor.

To reset the author:

git commit --amend --reset-author

To rewrite the author:

git commit --amend --author 'Ovidiu Feodorov <ovidiu@example.com>'

Update the Commit Message of a Specific Commit

git rebase -i "<commit-id>^"

Note the caret at the end, this is needed because you will need to rebase back to the commit before the one you wish to modify.

In the interactive editor, replace "pick" with "e" ("edit") for the commit you wish to change.

The git rebase procedure will exit and allow you to:

You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue

Execute:

git commit --amend

interactively update the comment, then

git rebase --continue

Revert Individual Files to a Specific Past Commit

Revert Individual Files to a Specific Past Commit

Deleting Commits from History

Deleting Commits from History

Unstage a Local Commit

git reset --soft HEAD~1

Adding an Empty Commit

git commit -m 'Some commit message' --allow-empty