Reputation: 25
I've been following a tutorial on how to build a blog in Rails, and now I've gotten stuck and want to access a previous commit on GitHub. I'm new to GitHub, and assume that every time during the tutorial when the developer wants to save his progress to GitHub, he invokes
$ git status
$ git add .
$ git commit -am "some info about the current commit"
to save his project at a certain state. Well, I've been doing this and now I've gotten to the point where my project broke and would like to restore it to my last commit when it was working. I've invoked:
$ git log
commit 7f6dd520095ead774a8e612048f52ee11fdfe154
Author: danny rosenfeld <[email protected]>
Date: Sat Sep 19 17:21:56 2015 +0300
Add pages
commit b84c3a601aeacdacbd2a8c71b85e1236554a1275
Author: danny rosenfeld <[email protected]>
Date: Sat Sep 19 16:49:24 2015 +0300
add comments
commit 5822d7c90d15696536d30d531f424863d58de6c7
Author: danny rosenfeld <[email protected]>
Date: Sat Sep 19 16:15:33 2015 +0300
Edit and delete posts
and it's showing my commits exist. Now how do I access them? I don't believe I've created a specific repository, but apparently these commits exist.
Upvotes: 0
Views: 47
Reputation: 2883
You can reset your files to the last commit with
git reset --hard HEAD
All your changes since last commit to tracked files will be lost.
Upvotes: 0
Reputation: 18803
You can visit any state with
git checkout <sha>
Where <sha>
is one of your commit hashes. Git will find any unique prefix, so you'll typically see people just use the first 6 or 7 characters, eg. 7f6dd52
. If you want to come back to where you were, use
git checkout <branch>
where <branch>
is probably master
, unless you've checked out a different one.
If you want to reset your repository to that state, check out the branch you want to change, and run
git reset <sha>
By default reset
uses the --mixed
option, which sets the branch back to that commit, but leaves your file contents alone. From there, you can re-commit pieces you want. If you want to throw out your current state, specify --hard
and Git will make your repository look exactly like it was at the SHA you specify.
If you only want to throw out code you haven't committed yet, the simplest way is
git checkout .
This resets each tracked file to its state in the last commit.
Once you've checked out the commit you want, you can make a copy or the directory just like you would any other directory. Your copy will include the .git/
folder, so it will effectively be another clone of the repository. If you want to totally divorce it from the repository, delete .git/
in the copy.
Since you mention "I don't believe I've created a specific repository", remember that Git is a tool independent of GitHub. As soon as you git init
, you have a repository, and later you can add a remote
on GitHub that you push
to. Until you push to a remote, your repository exists locally and has full history. Having a remote like GitHub is useful for redundancy and as an always-available server to collaborate with others on.
Some relevant documentation you might enjoy:
Upvotes: 1