0

I use Git to store my changes every day when I finish my job:

git add --all
git commit -m 'some comments'
git push origin master

But today, I need my Git to revert some files to track some bug. So, I decided to go back 3 days ago by using this command:

git checkout 203914

Then, after I found the cause of the bug, I changed back to latest commit by using checkout:

git checkout 981291 << this is my latest commit, about 3 hours ago

Now, after I edit 981291 commit, I want to create a new commit using same way as I do every day:

git add --all
git commit -m 'minor fix'
git push origin master

It says:

HEAD detached from 981291
nothing to commit, working directory clean
Everything up-to-date

Then I checked my BitBucket account, I couldn't find 'minor fix' commit. it seems that after I checkout into previous commit and make some changes, I cannot make new commit into my remote server again.

How can I make a new commit after this checkout?

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
Saint Robson
  • 5,475
  • 18
  • 71
  • 118
  • 2
    You will need to do `git checkout master` so that you are no longer in a "detached HEAD" state, and then create your new commit. – Scott Weldon Mar 26 '16 at 17:52
  • 2
    If you already created a commit while not on a branch, then you may be able to find it using `git reflog`, see e.g. [here](http://stackoverflow.com/q/9984223/2747593). – Scott Weldon Mar 26 '16 at 17:58
  • 1
    it works bro! don't you want to make your comment as an answer? I will accept it as an answer below... – Saint Robson Mar 26 '16 at 17:59
  • 1
    @ScottWeldon : thank you very much for your help. I really appreciate it – Saint Robson Mar 26 '16 at 17:59

1 Answers1

3

By doing git checkout 203914, you have left the branch master, and are now in what is known as a "detached HEAD" state.

In order to keep your changes, you will need to do git checkout master, and then create your new commit.

If you already created a commit while not on a branch, then you may be able to find it using the reflog sub-command:

$ git reflog
...
3aa60dd HEAD@{1}: commit: this commit is missing from master
...

If you find the commit in the reflog, then you can apply it directly to master:

git checkout master
git cherry-pick 3aa60dd

A couple other notes about how you use Git:

  1. I recommend that you commit early, commit often, rather than once a day when you are done with work.

  2. Instead of manually checking out specific commits to track down a bug, take a look at git bisect, which automates the process for you.

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
  • A "detached HEAD" may or may not be on the same commit as a known branch - the connection is not considered. – Thorbjørn Ravn Andersen Mar 26 '16 at 21:00
  • 1
    Regarding when to commit - if you are on a major branch (one watched by a CI engine) it is generally a good idea not to commit unless all unit tests pass. – Thorbjørn Ravn Andersen Mar 26 '16 at 21:02
  • @ThorbjørnRavnAndersen: Alternatively, you can continue to commit, but not push the branch until your tests pass. Or, if you use [Git Flow](http://nvie.com/posts/a-successful-git-branching-model/), then the major branches will contain nothing but merge commits. – Scott Weldon Mar 26 '16 at 21:15
  • 1
    I guess the reason for "commiting when going home for the day" is to ensure that the work is not lost if the computer breaks down or is stolen. If you do not push, you do not get this. – Thorbjørn Ravn Andersen Mar 26 '16 at 21:47
  • That's true. I usually push without regard for whether tests are passing: if they aren't, then the CI server will tell me that and I can fix them later. (Again, I use Git Flow, so this will only happen on e.g. a feature branch.) – Scott Weldon Mar 26 '16 at 21:49