wrkbrs

github push 후에 commit 메세지 변경 본문

Git

github push 후에 commit 메세지 변경

zcarc 2019. 2. 2. 12:22
777

I have made a git commit and subsequent push. I would like to change the commit message. If I understand correctly, this is not advisable because someone might have pulled from the remote repository before I make such changes. What if I know that no one has pulled?

Is there a way to do this?

1129

Changing history

If it is the most recent commit, you can simply do this:

git commit --amend

This brings up the editor with the last commit message and lets you edit the message. (You can use -m if you want to wipe out the old message and use a new one.)

Pushing

And then when you push, do this:

git push --force-with-lease <repository> <branch>

Or you can use "+":

git push <repository> +<branch>

Or you can use --force:

git push --force <repository> <branch>

Be careful when using these commands.

  • If someone else pushed changes to the same branch, you probably want to avoid destroying those changes. The --force-with-lease option is the safest, because it will abort if there are any upstream changes (

  • If you don't specify the branch explicitly, Git will use the default push settings. If your default push setting is "matching", then you may destroy changes on several branches at the same time.

Pulling / fetching afterwards

Anyone who already pulled will now get an error message, and they will need to update (assuming they aren't making any changes themselves) by doing something like this:

git fetch origin
git reset --hard origin/master # Loses local commits

Be careful when using reset --hard. If you have changes to the branch, those changes will be destroyed.

A note about modifying history

The destroyed data is really just the old commit message, but --force doesn't know that, and will happily delete other data too. So think of --force as "I want to destroy data, and I know for sure what data is being destroyed." But when the destroyed data is committed, you can often recover old commits from the reflog—the data is actually orphaned instead of destroyed (although orphaned commits are periodically deleted).

If you don't think you're destroying data, then stay away from --force... bad things might happen.

This is why --force-with-lease is somewhat safer.

  • 11
    Be careful with that "fix", as if they have any local, unpushed commits they will be "lost" (lost truly meaning orphaned, but recovering them is non-obvious). – Andrew Marshall Jan 24 '12 at 2:07
  • 1
    you probably want to specify the branch name when you push --force, otherwise you may push more than you expected. – user693960 Mar 12 '13 at 0:25
  • 1
    @user693960: Git will only push what you configure it to push. – Dietrich Epp Mar 12 '13 at 2:37
  • @Leniel Macaferi: Using -m here doesn't let you edit the old message, it only lets you specify the new message. – Dietrich Epp Oct 29 '13 at 0:38
  • 9
    Simply git push --force without <repository> and <branch> options works too, if you have your upstream set up. – ahnbizcad Apr 29 '14 at 9:16 



https://stackoverflow.com/questions/8981194/changing-git-commit-message-after-push-given-that-no-one-pulled-from-remote