【问题标题】:view project at specific commit in git [duplicate]在 git 中查看特定提交的项目 [重复]
【发布时间】:2016-07-25 01:00:53
【问题描述】:

我被告知要在提交的特定时间点查看我们的项目。我有一个 20 多个字符长的特定提交代码/哈希(位于 URL 末尾的那个,不确定术语是否正确)。

我可以在存储库中看到提交更改,但是如何在我的桌面上打开项目并实际处理提交时存在的项目?

有人告诉我 git checkout head 可以提供帮助,但我尝试了 git checkout head < commit code/hash here > 并在终端中收到一条消息,提示“错误,pathspec 与 git 已知的任何文件都不匹配。”

【问题讨论】:

标签: git


【解决方案1】:

您首先需要将您的存储库重置为给定的提交。如果您想从那里开始工作,在该提交处创建一个分支可能会有所帮助:

git checkout -b mybranch <commit>

如果你不创建一个分支,你最终会得到一个detached state,这通常会让你很难使用(如果你碰巧忘记了实际的提交ID,很容易丢失你的工作......)。

如果您使用图形存储库历史浏览器(如 gitk),您还可以:

  • 导航到有问题的提交
  • 在提交时创建一个新分支(在gitk右键单击提交消息 -> 创建一个新分支
  • 签出分支(在gitk右击分支标签 -> “签出这个分支

【讨论】:

    【解决方案2】:

    可以在此处找到有关不同方式的完整分析器:
    What is HEAD and to checkout specpfic commit ore reset the repository

    git checkout

    git checkout
    git checkout <commit_id>
    git checkout -b <new branch> <commit_id>
    git checkout HEAD~X // x is the number of commits t go back
    

    这将签出指向所需提交的新分支。 此命令将检出给定的提交。 此时您可以创建一个分支并从这一点开始工作。

    # Checkout a given commit. 
    # Doing so will result in a `detached HEAD` which mean that the `HEAD`
    # is not pointing to the latest so you will need to checkout branch
    #in order to be able to update the code.
    git checkout <commit-id>
    
    # create a new branch forked to the given commit
    git checkout -b <branch name>
    

    git reset HEAD --hard &lt;commit_id&gt;

    “移动”你的头回到所需的提交。

    # This will destroy any local modifications.
    # Don't do it if you have uncommitted work you want to keep.
    git reset --hard 0d1d7fc32
    

    如何查看不影响当前分支的特定提交?

    来自 git v2.5

    git worktree add <new_path>
    

    git worktree 将创建 2 个相互独立的工作文件夹,同时指向同一个存储库。

    这将允许您对 enw 工作树上的任何实验进行操作,而不会对存储库本身产生任何影响。

    这是一个关于如何创建新工作树及其结果的示例:

    【讨论】:

      猜你喜欢
      • 2013-11-15
      • 1970-01-01
      • 2017-05-16
      • 2018-04-05
      • 2021-02-27
      • 2014-01-28
      • 2010-11-13
      • 2015-05-08
      • 2016-04-20
      相关资源
      最近更新 更多