PlatformIO Community

Irritation after git: Undo Last Commit

After made some staged and commited changes I like to undo the very last one. Call SCR click the elipse (…) then Commit and Undo Last Commit. SCR now shows one Unstaged Change. Deleting/confirming this by SCR „-„ and „back arrow“ and „Discard Changes“ in SCR works fine.
However after made an other change stage and commit it the place for the commit message, normaly located in the uppermost line has changed to the second line not responding to pressing ENTER. Back tot he Editor view Timline dosen’t shows Undo Last Commit wasn’t executed.
As a newbie I’m somewhat confused. What ist the correct seqence for Undo Last Commit?
Thank-you

Git isn’t part of PlatformIo. In your description, I’m assuming it’s git from within VSCode you are having problems with?

If you are having problems with VSCode’s git integration, you might need to raise an issue with VSCode, however…

I almost never use version control from an IDE, instead, I much prefer the command line.

If you open a shell/command/terminal session to your project directory and execute git log do you see the commit message, for the undone commit, listed as the first/most recent commit message?

If so, it has not been undone.

Git Revert | Atlassian Git Tutorial shows details on the git revert command, which undoes the last commit, but then adds a new commit message leaving the commit message for the reverted commit present.

This means that if VSCode’s undo last commit command simply did a git revert then you will still see that message in the log/timeline.

The question has to be, did the undo actually change your code files back as you expected?

Cheers,
Norm.

Thank-you so much for taking time and giving advice to me.
I’ve recently changed from Arduino IDE to VScode/pio so I’m a really newbie on both. I even don’t know the differences between them by heart.
So I try to avoid using just another environment like git native although it’s easy to switch and use the terminal to employ git capabilities within VSCode.
But staying within VSCode seems to be the easy way. Just goto SRC … COMMIT UNDO LAST COMMIT. But that’s not all. There remains staged changes in SCR. Trouble begins on how to deal with those un/staged changes and - more serious to me: trying this UNDO COMMIT later a second time after other changes are made and commitet successful.
I’m so confused … maybe using native git commands is the way to go.

Norman I’ll go to all your suggestions and report - just beeing busy for now.
Cheers Joschen


If so, it has not been undone.

source code still show the uncommited change
now goto SRC with the pending change msg, stage with + (but ther is also an - sign in main.cpp the line above) and commit
source code still hold the “uncommitted” change, TIMELINE 2nd commit msg still present
question how to “commit” the “uncommit”? in VSCode

Git Revert | Atlassian Git Tutorial shows details on the git revert command, which undoes the last commit, but then adds a new commit message leaving the commit message for the reverted commit present.

as non native speaker I wonder what is the difference between reset, revert et all the lingo?

This means that if VSCode’s undo last commit command simply did a git revert then you will still see that message in the log/timeline.

The question has to be, did the undo actually change your code files back as you expected?

no unless I do some magic reset/undo/do the undo at stage/commit level …
=======================================================================

the good news: I can do undo to where ever I like by copy the Commit ID by right clicking target in TIMELINE open a terminal lsession and do a git reset -->hard “ID” >>touch down at native git

Norman thank-you so much for your patience

after uncommit
before_uncommit

TL;DR:

  • Make your changes, stage and commit.
  • Make more changes, stage and commit.
  • Optional: Make more changes. Don’t stage or commit yet.
  • Select “Undo last commit”.
  • All committed changes become uncommitted, however, remain staged.
  • Beside all affected files, in the git sidebar, select “Unstage changes” icon for that file only. Repeat for all files, if necessary.
  • For each affected file, in the git sidebar, click “discard changes” icon to revert back to file as it was just before the second commit.

Detail:

In VSCode, I did this:

  • Opened a folder, which was completely blank.

  • Selected “Initialise Repository” on the git sidebar.

  • Went to git sidebar, clicked …, and chose “Show git output”. I could see plenty of git commands, including my git init command.

  • File->New File, added some text, saved as fred.txt. Git icon in sidebar shows (1) change. Fred.txt shows ‘U’ for unstaged changes.

  • Went to git sidebar, chose ‘+’ beside fred.txt to stage changes. Staged. Git output shows git add -A -- /home/norman/temp/gitTest/fred.txt. I also now have the option to unstage changes in the git sidebar beside fred’s name.

  • Added a commit message (“First commit”), and clicked commit icon in git sidebar. Git output panel shows git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -.

  • Added more text to fred.txt, staged and committed changes. Commit message “Second Commit”. Again, git output panel shows git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file - command having been executed.

  • Right-click in git output. Select Clear Output.

  • In git sidebar, click … at the top, select Commit → Undo Last Commit. Sidebar shows changes not yet staged icon. fred.txt reappears under “Changes” as being Modified (M). Git output shows git reset --soft HEAD~. “Second Commit” reappears in Commit Message at top, and is editable.

  • Click ‘-’ option beside fred.txt in git sidebar to “Unstage changes”. Git output shows git reset -q HEAD -- /home/norman/temp/gitTest/fred.txt.

The file, fred.txt is still showing the text added for the second commit, which has been rolled back. The git icon in the sidebar is still showing modifications (1).

  • In the git sidebar, on the fred.txt line, click the icon to discard changes. Fred.txt changes to delete the text added for the second commit. The git icon no longer shows changes. Fred.txt has reverted back to how it was immediately after the first commit. The git output shows git checkout -q -- /home/norman/temp/gitTest/fred.txt which basically has checked out the first committed version.

Summary.

If you attempt to undo last commit, and:

  • If no commits have yet taken place, you will get an error;
  • If only one commit has taken place so far, then the file will be deleted.
  • If ‘N’ commits have taken place, undoing the last will roll back to how the file was committed for commit “N-1”.

You must:

  • Undo Last Commit;
  • Unstage Changes for any files you wish to revert, others appear to be able to be left alone;
  • Discard changes for any file you wish to revert, others can be left alone.

Any new commits at this point will commit staged changes for all then files you didn’t unstage.

HTH

Cheers,
Norm.

I’m steamrolled 😊

Thank-you for your time and effort. This will help to understand VSCode&integrated git indeed.

Maybe one or another question will arise. However I’m now confident I will succeed.

Yes it will help a lot.

Totally agree with your motto 😊

Joschen

1 Like