I checked out a previous commit, using git checkout , I made changes, and committed them. However, when I do git status, it says HEAD detached from . How can I make commit that I've checked out the commit I want to keep going forward?
- 
                    Check out https://stackoverflow.com/questions/4114095/how-to-revert-git-repository-to-a-previous-commit and specifically https://github.com/blog/2019-how-to-undo-almost-anything-with-git – EvanM Aug 17 '17 at 05:17
- 
                    Possible duplicate of [How to revert Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-to-revert-git-repository-to-a-previous-commit) – EvanM Aug 17 '17 at 05:17
- 
                    @evan marshak I've looked at https://stackoverflow.com/questions/4114095/how-to-revert-git-repository-to-a-previous-commit, but I'm not sure which post I'm supposed to follow, or what command to do. The only one I see relevant is the first post jeffromi, but only because he mentions git checkout – TheRealFakeNews Aug 17 '17 at 05:25
- 
                    it depends on the state of your repo. [This github page](https://github.com/blog/2019-how-to-undo-almost-anything-with-git) is pretty thorough. If the changes are already pushed, and you can enumerate the changes you want to revert, try doing `git revert` on those commits. If you want to mass undo/redo, try `git rebase`. I'd try some kind of operation from HEAD that gets you to the state you want. – EvanM Aug 17 '17 at 05:29
- 
                    Also, if you *really* don't care about rolling forward, you should be able to `git push -f` (e.g., https://stackoverflow.com/questions/5509543/how-do-i-properly-force-a-git-push) – EvanM Aug 17 '17 at 05:30
3 Answers
Let's look at the situation step by step. Initially you were at the head of a branch, let's say it was master:
--A--B--C
        ^ master
       HEAD
I checked out a previous commit, using git checkout
git checkout HEAD~
Now you're here:
--A--B--C
     ^  ^ master
   HEAD
This situation is called "detached HEAD" because HEAD (the current pointer) is not at the head of any branch.
I made changes, and committed them.
Now you're here:
       --D < HEAD
      /
--A--B--C
        ^ master
What do you want to do now? Do you want to drop the commit C at the head of master and move master to where your HEAD is (to D)? Or do you want to move the commit D to master (preserving C)?
The first task can be solved with the following commands:
git branch tmp-master # create a new branch
       tmp-master
         v
       --D < HEAD
      /
--A--B--C
        ^ master
git checkout master
       tmp-master
         v
       --D
      /
--A--B--C < HEAD
        ^ master
git reset --hard tmp-master # move the branch
git branch -D tmp-master # remove the temporary branch
       master
         v HEAD
       --D
      /
--A--B--C
Or, to streamline the new master branch:
--A--B--D
      \
       C
The commit C becomes a dangling commit and sooner or later will be removed by garbage collector.
But if you want to do the second task (preserve both C and D) the steps are a bit different:
git branch tmp-master # create a new branch
       tmp-master
         v
       --D < HEAD
      /
--A--B--C
        ^ master
git checkout master
       tmp-master
         v
       --D
      /
--A--B--C < HEAD
        ^ master
git cherry-pick tmp-master
git branch -D tmp-master # remove the temporary branch
--A--B--C--D' < HEAD
           ^ master
 
    
    - 82,685
- 13
- 120
- 165
Push it to the remote with -f so that it erases history.
If your branch is called b1:
git push -f origin b1
Then delete the local branch and checkout the remote branch again. You're all set.
 
    
    - 2,792
- 1
- 26
- 32
 
    