I accidentally commited 10 files with one commit. And I wanted 10 different commits - one commit per file. How can I get the files from the commit and re-commit already one by one? But with this, the code needs to be saved, because the changes themselves are good. The commit turned out bad. I use the program SmartGit, but I can and through cmd.

  • 2
    git reset --soft HEAD~ - rolls back to the commit that precedes the current one, with all changes remaining at the site hostingadvice.com/how-to/git-undo-commit - etki
  • @Etki, wow! Thank you very much! - Alexander Elizarov

1 answer 1

In any incomprehensible situation, make a new branch.

First, provide a “escape route” in case everything goes wrong:

 git branch backup 

If things go badly, in the backup branch, the changes will definitely remain. Then this branch can be deleted.

Now move the branch to the commit back, leaving the changes in place:

 git reset --mixed "HEAD^" 

Why mixed ?

  • hard flushes everything
  • soft leaves the index and files (everything will look like it was git add )
  • mixed will reset the index (supposedly git add not), but will not touch files
  • Thanks, already managed to do as advised @Etki) Everything turned out. But now I will do git branch backup just in case. And if you simply create a new branch and then roll back the commit, then the code will not remain either new or old? That is, backup is a special branch like this? - Alexander Elizarov
  • @ AleksandrElizarov No, the name is from the ceiling. It can be called otherwise. - D-side
  • Aah, I understood) it means I followed your advice too) That is, to make a new branch. If something went wrong, the code will remain in it, if everything is fine, just delete it. - Alexander Elizarov
  • @ AleksandrElizarov That's it. Any doubtful actions are best accompanied by a backup branch. And if you forget, it can save git reflog . - D-side
  • Better yet, any dubious things to do on a local clone of the experimental repo. - zed