Search code examples
gitrepository

Git: How do I remove HEAD back to most recent commit?


Being a beginner in git, I somehow managed to spoil my repo in the way that it looks like that now:

PS C:\GitRepositories> git log --oneline

158e4c0 (HEAD -> 24d309, origin/24d309) Comment6
b4c156a Comment5
87bd801 Comment4
b24d309 (master) Comment3
f1ec7ee (origin/master, origin/HEAD) Comment2
6ce6a10 Comment1

Now, when I push the version of Comment2 is pushed, not the most recent one (Comment6). Could somebody help me?


Solution

  • blind guess :

    at some point, you tried to reach the state of your master branch (commit b24d309),
    and you tried to do so by running git checkout -b b24d309 (incidentally, you added a typo, but the issue in this case is actually using -b).

    If you use -b <something> : git will understand that your intention is to create a branch, using <something> as its branchname.
    If <something> happens to look like a commit hash (say: 24d309 or b24d309), well, you will end up with a branch which has a name that looks like a hash ...


    If your intention is :

    • to have master as your active branch
    • to move master forward to "Comment6"
    • to update origin/master to the same state
    • to remove the existence of the ill named 24d309 branch (both local and remote)

    you can do the following:

    git switch master
    git merge --ff-only 158e4c0
    git push origin master
    
    # remove local and remote branches:
    git branch -D 24d309    # -D (upper D) is the "forced" option for -d
                            # see 'git help branch' for more details
    git push origin -d 24d309