简体   繁体   English

提交没有github上的存储库?

[英]Commits without repository on github?

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. 我一直在遵循有关如何在Rails中构建博客的教程,现在我陷入困境,想访问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 我是GitHub的新手,并假设在教程中每次开发人员要将进度保存到GitHub时,他都会调用

$ 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 <dannyseniorproject@gmail.com>
Date:   Sat Sep 19 17:21:56 2015 +0300

    Add pages

commit b84c3a601aeacdacbd2a8c71b85e1236554a1275
Author: danny rosenfeld <dannyseniorproject@gmail.com>
Date:   Sat Sep 19 16:49:24 2015 +0300

    add comments

commit 5822d7c90d15696536d30d531f424863d58de6c7
Author: danny rosenfeld <dannyseniorproject@gmail.com>
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. 我不相信我已经创建了特定的存储库,但是显然这些提交存在。

You can visit any state with 您可以访问任何州

git checkout <sha>

Where <sha> is one of your commit hashes. 其中<sha>是您的提交哈希之一。 Git will find any unique prefix, so you'll typically see people just use the first 6 or 7 characters, eg. Git会找到任何唯一的前缀,因此您通常会看到人们仅使用前6或7个字符。 7f6dd52 . 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. 其中<branch>可能是master ,除非您签出了另一个。

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. 默认情况下, reset使用--mixed选项,该选项将分支设置回该提交,但不--mixed文件内容。 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. 如果要放弃当前状态,请指定--hard ,Git将使您的存储库看起来与您指定的SHA完全相同。

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. 您的副本将包含.git/文件夹,因此它实际上将是存储库的另一个副本。 If you want to totally divorce it from the repository, delete .git/ in the copy. 如果要从存储库中完全离婚,请在副本中删除.git/

Since you mention "I don't believe I've created a specific repository", remember that Git is a tool independent of GitHub. 由于您提到“我不相信我已经创建了特定的存储库”,因此请记住,Git是独立于GitHub的工具。 As soon as you git init , you have a repository, and later you can add a remote on GitHub that you push to. 一旦使用git init ,便有了一个存储库,以后您可以在GitHub上添加要push送到的remote服务器。 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. 拥有像GitHub这样的远程服务器对于冗余和作为始终可用的服务器与他人协作非常有用。

Some relevant documentation you might enjoy: 您可能会喜欢的一些相关文档:

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. 自上次提交跟踪文件以来,您所做的所有更改都将丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM