How to reverse the most recent submissions in git?

Asked

Viewed 55 times

0

I accidentally sent the wrong files in Git, but haven’t put them on the server yet.

How can I undo these Git appointments?

  • Use the Git Reset git reset <id-do-último-commit-correto>. To capture the id, just use git log

1 answer

0

$ git commit -m "Enviado por engano"                        (1)
$ git reset HEAD~                                           (2)
<< edite os arquivos como for necessário >>                 (3)
$ git add ...                                               (4)
$ git commit -c ORIG_HEAD                                   (5) 
  1. This is what you want to undo
  2. This leaves your work tree (the state of your files on disk) unchanged, but undoes the commit and leaves the changes that you have committed unregistered (then they will appear as "Unrealized changes to confirm" in git status and you need to add them again before confirming). If you want just add more changes to the previous confirmation, or change the confirmation message 1, you could use git reset --soft HEAD ~ instead, which is like git reset HEAD ~ (where HEAD ~ is the even HEAD ~ 1) but leaves its existing changes.
  3. Make corrections to the files in the file tree
  4. git add anything you want to include in your new shipment
  5. Send changes by reusing the old send message. reset copied the old header to .git / ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the previous confirmation and allows you to edit it. If you don’t need to edit the message, you can use the -C option.

1 Note, however, that you do not need to reset to a previous submission if you just made an error in your confirmation message. The easiest option is git reset (to move up the changes you’ve made since then) and then, git commit --amend, which will open your default confirmation message editor pre-filled with the last confirmation message.

Be careful though, if you added new changes to the index, use commit - Amend will add them to your previous commit.

  • Observation 2: both the question and the answer were drawn from the stackoverflow official, and translated with the aim of contributing to the SOPT community

Browser other questions tagged

You are not signed in. Login or sign up in order to post.