Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git checkout <SHA> and Heroku

I created a local Git repo on my laptop and then pushed the source to Heroku creating a remote branch. After a few days of commits and pushes, I need to rollback to an earlier commit. Here's what I did.

cd <app root>
git checkout 35fbd894eef3e114c814cc3c7ac7bb50b28f6b73 

Someone told me that doing the checkout created a new working tree and not the branch itself, so when I pushed the rollback changes to Heroku, it said everything is up to date and nothing was pushed. How do I fix this situation? Thanks for your help in advance.

like image 283
Bob Avatar asked Mar 18 '10 03:03

Bob


People also ask

What does git checkout Hash do?

This file contains the hash of the last commit on that branch, meaning the tip of the branch . These two files so far inform Git on which branch should be advanced and what is the last commit on that branch. By running git checkout <branch name> , the HEAD file is updated to contain that branch's name.


2 Answers

When you checkout a direct commit name (using the SHA-1 hash of the commit object) instead of checking out a branch name, you end up with a “detached HEAD”. HEAD is the “ref” that keeps track of what is currently checked out. It becomes detached when you directly checkout a commit instead of a branch (it is not attached to any branch). No branches are updated when you detach a repository's HEAD. You might think of the detached head state as if you had an anonymous branch checked out.


To reattach your repository's HEAD, you will want to save the current HEAD as a branch and check that branch out:

  1. To save the current HEAD in a new branch do this:

    git branch <new-branch-name>
    
  2. To overwrite an existing branch you need to use --force:

    git branch --force <existing-branch-name>
    
  3. Then, reattach your repository's HEAD by checking out the new/updated branch:

    git checkout <branch-name>
    

    (where <branch-name> is the same as <new-branch-name> or <existing-branch-name>, depending on which of the above two commands you used)

This sequence (git branch to make a ref point to the current HEAD commit, then git checkout that updated branch) will carry forward any uncommitted content that you might have in your working index and/or tree.


In the future, if you want to ‘roll back’ the current branch to some previous commit, you should use this instead of detaching your repository's HEAD:

git reset --hard <commit>

This will reset the current branch (or your detached HEAD, if it is already detached) to the named commit, and make the index and the working tree reflect that commit (i.e. it throws away any commits since the specified commit along with any uncommitted content).

The detached HEAD state is useful for revisiting old states, and sometimes for short-term work that you are not sure you will keep. Other than that you probably want to avoid it.

like image 135
Chris Johnsen Avatar answered Oct 22 '22 21:10

Chris Johnsen


You want to reset:

git reset --hard 35fbd894eef3e114c814cc3c7ac7bb50b28f6b73
like image 25
Chris Avatar answered Oct 22 '22 21:10

Chris