gitmergegit-resetgit-revert

Why do I still show local files from a branch after a git revert of the merge?


I am a newcomer to Git and am probably missing something basic.

My desired practice is to create a branch from main, work on a given release in that branch, and merge to main when release code is stable, tag that merge commit, and create a branch based on the tag for the next development effort.

In the process of testing I've come across a need to undo the merge to main and try again. When I made my final commit in the branch, checked out main, and merged, I saw all the files I had added in the branch populate in my local directory, as I expected.

Following some suggestions here How do I revert a Git repository to a previous commit? and here How do I revert a Git repository to a previous commit? I have tried

git revert -m 1 HEAD

git reset --hard HEAD^

Both looked successful, sort of- the reset returned HEAD is now at 83d5e80 Last commit before committing to main

That commit was to the branch, not main, and HEAD is now at main, which puzzles me. But not as much as the fact that files which were only in the branch prior to the merge (which I thought I had reverted and reset) are still in my working directory. What am I missing?

Adding transcript of my attempts and graph output:

$ git revert -m 1 HEAD
On branch main
Your branch is up to date with 'origin/main'.

Revert currently in progress.
  (run "git revert --continue" to continue)
  (use "git revert --skip" to skip this patch)
  (use "git revert --abort" to cancel the revert operation)

nothing to commit, working tree clean

AzureAD+PatSantucci@psantuccasei MINGW64 /c/Devs/Success_Way-Boardroom_121 (main|REVERTING)
$ git revert --continue

AzureAD+PatSantucci@psantuccasei MINGW64 /c/Devs/Success_Way-Boardroom_121 (main|REVERTING)
$ git reset --hard HEAD^
HEAD is now at 83d5e80 Last commit before committing to main

AzureAD+PatSantucci@psantuccasei MINGW64 /c/Devs/Success_Way-Boardroom_121 (main)
$

And the graph:

$ git log main --graph
* commit 83d5e80449621fb21742120b983985abea5bd969 (HEAD -> main)
| Author: Pat Santucci <patsantucci@thing.thing>
| Date:   Wed May 21 16:31:41 2025 -0400
|
|     Last commit before committing to main
|
* commit 605a5e549dee4650aaa8f59d5eff4eddd337d1d5
| Author: Pat Santucci <patsantucci@thing.thing>
| Date:   Tue May 20 15:33:29 2025 -0400
|
|     Baseline at the start of development
|
* commit 357367c1fd88413c6ab2bcbf73bcf6b69e7f414d
| Author: Pat Santucci <patsantucci@thing.thing>
| Date:   Fri May 16 18:53:24 2025 -0400
|
|     Establishing baseline with boilerplate files
|
* commit 228ff88ec4a5d5a3749bd94527f4498aa431fc43
| Author: Pat Santucci <patsantucci@thing.thing>
| Date:   Fri May 16 19:49:24 2025 +0000
|
|     Updated README.md
:

Solution

  • git revert -m 1 HEAD adds a new commit R that reverts the last commit 83d5e80. So, you get

    P - 83d5e80 - R
                  ^
                  HEAD
    

    HEAD points to the file state same as in P.

    git reset --hard HEAD^ moves the head back to 83d5e80 and R is lost.

    P - 83d5e80 - R
           ^
           HEAD
    

    Thus, you get the initial state where you start after applying the both commands. You should use either of those commands but not both for getting HEAD to point to R or P with the equal file states.