How does git commit –amend work, exactly?

I have seen GIT commit —amend in detached HEAD state. The question requires the answer to be more complex than needs be. I’d like to understand just how git commit --amend works in a normal HEAD situation.

  • Git: How to edit/reword a merge commit's message?
  • git commit --amend without asking for message
  • How to modify existing, unpushed commits?
  • Why does git call me “clever” when I reword the last commit message?
  • Changing the code of previous commits with git rebase -i
  • What are the differences between 'revert', 'amend,' 'rollback', and 'undo' a commit?
  • How to push my changes back to the source code in git
  • git commands not working in git bash (windows 7 x64)
  • How to merge two branches without a common ancestor?
  • Identifying branch of commits in Git after deleting a branch
  • using git rebase in the master branch
  • Is it possible to use GitHub and GitLab on one machine?
  • 2 Solutions collect form web for “How does git commit –amend work, exactly?”

    Assume that you’re in a clean working state and that your repo looks as follows:

    enter image description here

    If you then run

    git commit --amend

    write a commit message, save and quit your editor, the following happens:

    1. Your staging area—which, if you haven’t staged any new changes, will be identical commit f42c5—is used to create a new commit: 31b8e. Its parent(s) will be the same as that(those) of the commit you’re amending: f42c5.
    2. The master branch reference is moved to point to that new commit (31b8e).
    3. The HEAD reference, which already points to master, is moved along with it.

    enter image description here

    Note that the amended commit (f42c5) is now unreachable from any reference in your repo (hence its “transparent” style on my graph). It still lives in your repository’s object database, but will eventually be deleted for good, when Git runs its periodic housekeeping, or if you trigger it explicitly by running git gc (garbage collection).

    Addendum (based on Jason Baker’s comment): Note that, as long as the amended commit, f42c5, still exists in your repo and you have a way of finding out its commit ID (for example, by fishing it out of the master branch’s reflog), you can still check it out. Running

    git checkout master # just to be sure that master is the current branch
    git reset --hard f42c5

    or (assuming you haven’t, in the meantime, made any new commit on master, reset master, or otherwise moved the master branch reference)

    git checkout master # just to be sure that master is the current branch
    git reset --hard master@{1}

    would put you in the following situation:

    enter image description here

    But now, commit 31b8e would become unreachable.

    Say you just committed “B”

    ... --- A --- B

    Amending “B” will create a parallel commit which becomes the new branch head.

            +---- B
    ... --- A --- B'

    B’ is the commit resulting from a combination of the changes from B plus the changes you had staged when you issued the git commit --amend.

    Git Baby is a git and github fan, let's start git clone.