If I push a fixed commit, will it create a new commit?

I already sent the commit to the remote branch and now I want to change its contents, so I tried git amend .

If you do git status , it says that the two branches have 1 and 1 different commits, respectively.

Now, if I send a corrected commit with the same commit message, will it add a new commit or will it change the last commit I issued?

+7
source share
3 answers

git commit --amend , like git rebase , will create a new commit object. This object is based on a pre-existing commit, but it is still a new commit and completely replaces the old one.

Looking at the story, it might look like this:

  master ↓ * --- * --- * --- A 

Given that A is the original latch. If we change this commit now, we get the following result:

 * --- * --- * --- A \ --- A' ↑ master 

So, we get another A' commit object with a different hash, and the branch we were on (here: master) is updated to indicate this.

Now, if we add a remote repository to this view, and we pressed A earlier on the remote, then it looks like this:

  origin/master ↓ * --- * --- * --- A \ --- A' ↑ master 

So the remote object still points to the original commit A , but our local branch points to the modified A' . This is a problem because we cannot press A' and make origin/master point in A A' , as this will remove the already committed commit A from the history.

We can force push with git push --force to force Git to update the remote branch and really remove A from the story. It is important to note that this will break any story that has already pulled A out of the console. If any other developer has A and now the deleted points in A' , then they have a conflict that they must correct manually. This often hurts, so there is a rule that you should always follow:

Never reinstall (or modify) previously published commits.

A better alternative is to add a new commit B that just makes corrections on A :

  origin/master ↓ * --- * --- * --- A --- B ↑ master 

Thus, the already published story is still compatible with the new story, so we can press B on the remote computer without conflict, and everyone is happy.

+13
source

TL; DR

Pressing a modified commit means pressing another commit.

Anatomy of ID Fixation

The unique commit identifier consists of the SHA-1 hash of its metadata. What is the metadata? One way to find out is to use the cat-file plumbing command:

 git cat-file -p HEAD 

After running this command, you will see a list containing these fields:

  • Wood
  • Parent
  • Author
  • committer
  • Message

If any of these fields changes, their SHA-1 hash will also change the provision of committing a whole new identifier.

Making changes to dubbing history

Therefore, if you change the commit, for example by changing its message, it will have a different identifier than before. You are effectively rewriting history .

Note that the commit parent ID is also included in the metadata. This means that after the commit command changes the ID, all its descendants will also change identifiers, like the domino effect.

+2
source

Once you clicked on the remote control, you should not modify any existing commits locally.

You essentially created a new local commit (there is a new commit id) and replaced the old one, but the old one still exists on the remote control. This causes message 1 forward and 1 back.

To solve the problem, you need to create a new branch to save the changes, return the original branch, reset back before you make the amendment, change the change from the remote control. Then merge into your separate branch and push up.

+1
source

All Articles