我是否需要在 Git 中提交,何时应该这样做,以及如何恢复到旧版本?

发布于 2024-11-27 19:33:36 字数 296 浏览 7 评论 0原文

我一直在关注 Git 教程,但对一些具体问题有点困惑。我现在需要使用版本控制的主要原因只是为了在我犯了错误并且不知道如何消除错误时访问项目的以前版本。

  1. 只有执行提交语句后版本才会保存在 Git 中,这是真的吗?出于某种原因,我在想,每次你在项目中进行任何更改时,它都会自动添加为存储库中的版本,供你返回。

  2. 那么什么时候应该执行commit呢?是不是只有当你认为自己已经取得了很大进展的时候才执行?

  3. 如何在 Git 中实际恢复到以前的版本?即命令是什么?

I've been following a tutorial on Git and I'm a bit confused about a few specific issues. The main reason I need to use version control right now is simply to access previous versions of my project if I make a mistake and don't know how to get rid of an error.

  1. Is it true that a version is only saved in Git if you executed a commit statement? For some reason, I was thinking that every time you make any change in your project it would automatically be added as a version in your repository for you to go back to.

  2. So when should you execute commit? Is it only when you think you've made a good bit of progress?

  3. How do you actually revert to a previous version in Git? i.e. what's the command?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

和我恋爱吧 2024-12-04 19:33:36

1.使用 Git(与 SVN、Bazzar 或其他相同),您需要提交,以便保存版本 - 这样,git 只跟踪您认为足够好的版本

2. 当我得到一些可以正常工作的东西时,我倾向于做出承诺——至少在主分支上工作时。

如果我正在开发一个临时分支,专门为开发新功能/修复错误而创建,那么我几乎可以随时提交,以保存工作的当前状态。

3. 您应该查看以下页面: Git 中的撤销 - 重置、签出和恢复

1. With Git (same with SVN, or Bazzar, or others), you need to commit so a version is saved -- this way, git only keeps track of the versions you considered as good enough.

2. I tend to commit when I've got something that works OK -- at least when working on the main branch.

If I'm working on a temporary branch, created specifically to develop a new feature / fix a bug, then I commit pretty much whenever I want, to save the current state of my work.

3. You should take a look at the following page : Undoing in Git - Reset, Checkout and Revert

殊姿 2024-12-04 19:33:36
  1. 是的,你必须承诺坚持改变。但这在所有 VCS 中都是如此,而不仅仅是 Git。
  2. 当您拥有可以构建和运行的代码版本时,您应该提交。它应该是您希望能够撤消的操作。不太可能每次保存都如此。我发现我通常每天承诺 4 到 12 次,但如果我做了很多小改进,那么频率可能会更高。
  1. Yes, you have to commit to persist a change. This is true in all VCSes though, not just Git.
  2. You should commit when you have a version of your code that builds and runs. It should be something you would want to be able to undo to. It's unlikely that this is true on every save. I find I usually commit between 4 and 12 times a day, but it can be more frequent if I'm making a lot of small improvements.
过气美图社 2024-12-04 19:33:36
  1. 是的,仅当您运行提交时。
  2. 您应该根据需要\想要频繁地提交。每当你完成一些有用的事情时,在你改变一些事情之前,你担心的事情可能不会解决,或者你想确保你可以回到某个点。将这些改变推给其他人则是另一回事。只推送至少大部分有效且稳定的东西。
  3. 如果您有选择,并且需要基于文件的版本跟踪,您可能需要使用 Subversion (SVN)。否则使用 git checkout [commit-ref] [文件名] 。
    [commit-ref] 是您想要的版本的哈希值。
  1. Yes, only when you run a commit.
  2. You should commit as often as you need to\want to. Whenever you finish something useful, before you change something your worried might not work out, or a point were you want to make sure you can come back to. Pushing those changes to others is a different matter. Only ever push things that are at least mostly working and stable.
  3. If you have a choice, and need file based version tracking you might want to use Subversion (SVN). Otherwise use git checkout [commit-ref] [filename].
    The [commit-ref] is the hash value of the version you want.
温柔一刀 2024-12-04 19:33:36
  1. 是的。但请继续阅读...

  2. 特别是对于 git,每次您认为自己已经进行了一项独立的逻辑更改时都进行提交是一个好主意。因此,这种情况应该经常发生——通常每 5 到 30 分钟提交一次。例如,只需克隆 git 的 git 存储库,然后查看一些更有经验的人的工作。

在许多其他 vcs:es 中,您的提交对其他人立即可见,因此通常认为仅在构建或拥有完整功能时进行提交是一种良好的做法。在 git 中,这种粒度级别是由分支处理的,查看您的提交日志的人应该不仅能够看到您做了什么,还能够看到您是如何做到的,例如:

  • “新分支用于功能 frobnication”
  • “commit:foos 现在可以be frobnicated"
  • "commit: baz now 检查它所工作的 foos 是否正在被 frobnicated"
  • "commit: bar now can frobnicate foos"
  • "commit: 测试 frobnication"

这使得回顾源代码的工具对于调试更加有用。您当然应该尽量避免更改 100 多行的单次提交——它们会将您在源代码上完成的工作变成难以理解的不透明块。

  1. Yes. But read on...

  2. With git specifically, it's a good idea to commit every time you think you have made one self-contained logical change. So this should happen quite a lot -- typically commit between once in 5-30 minutes. For examples, just clone the git repo for git, and look at the work of some of the more experienced people.

In many other vcs:es, where your commits are immediately visible to other people, it's often considered good practise to only commit when it builds, or when you have a complete feature. In git, this level of granularity is handled by branches, and someone viewing your commit log should be able to see not just what you did, but how you did it, eg:

  • "new branch for feature frobnication"
  • "commit: foos now can be frobnicated"
  • "commit: baz now checks if foos it works on are being frobnicated"
  • "commit: bar now can frobnicate foos"
  • "commit: tests for frobnication"

This makes tools that look back on the source much more useful for debugging. You should certainly try to avoid single commits that change 100+ lines -- they turn work you have done on the source into opaque blocks that are hard to understand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文