简体   繁体   English

Go 特定版本

[英]Go to particular revision

I cloned a git repository of a certain project.我克隆了某个项目的 git 仓库。 Can I turn the files to the initial state and when I review the files go to revision 2, 3, 4... most recent?我可以将文件转换为初始 state 并在我查看文件 go 时将其转换为修订版 2、3、4...最新吗? I'd like to have an overview of how the project was evolving.我想大致了解一下该项目的进展情况。

Before executing this command keep in mind that it will leave you in detached head status在执行此命令之前,请记住它会使您处于分离头状态

Use git checkout <sha1> to check out a particular commit.使用git checkout <sha1>来签出特定的提交。

Where <sha1> is the commit unique number that you can obtain with git log其中<sha1>是您可以使用git log获取的提交唯一编号

Here are some options after you are in detached head status:以下是处于分离头状态后的一些选项:

  • Copy the files or make the changes that you need to a folder outside your git folder, checkout the branch were you need them git checkout <existingBranch> and replace files复制文件或对 git 文件夹之外的文件夹进行所需的更改,在需要时签出分支git checkout <existingBranch>并替换文件
  • Create a new local branch git checkout -b <new_branch_name> <sha1>创建一个新的本地分支git checkout -b <new_branch_name> <sha1>

To go to a particular version/commit run following commands.到 go 到特定版本/提交运行以下命令。 HASH-CODE you can get from git log --oneline -n 10 HASH-CODE 你可以从git log --oneline -n 10得到

git reset --hard HASH-CODE

Note - After reset to particular version/commit you can run git pull --rebase , if you want to bring back all the commits which are discarded.注意- 在重置为特定版本/提交后,如果你想恢复所有被丢弃的提交,你可以运行git pull --rebase

You can get a graphical view of the project history with tools like gitk .您可以使用gitk等工具获得项目历史的图形视图。 Just run:赶紧跑:

gitk --all

If you want to checkout a specific branch:如果你想签出一个特定的分支:

git checkout <branch name>

For a specific commit, use the SHA1 hash instead of the branch name.对于特定的提交,使用 SHA1 hash 而不是分支名称。 (See Treeishes in the Git Community Book , which is a good read, to see other options for navigating your tree.) (请参阅Git Community Book中的 Treeishes,这是一本很好的读物,可以查看用于导航树的其他选项。)

git log has a whole set of options to display detailed or summary history too. git log也有一整套选项来显示详细或摘要历史记录。

I don't know of an easy way to move forward in a commit history.我不知道在提交历史中前进的简单方法。 Projects with a linear history are probably not all that common.具有线性历史的项目可能并不常见。 The idea of a "revision" like you'd have with SVN or CVS doesn't map all that well in Git.像 SVN 或 CVS 那样的“修订”的想法在 Git 中并不是那么好。

Using a commit's SHA1 key, you could do the following:使用提交的 SHA1 密钥,您可以执行以下操作:

  • First, find the commit you want for a specific file:首先,找到您想要的特定文件的提交:

    git log -n <# commits> <file-name>

    This, based on your <# commits> , will generate a list of commits for a specific file.这基于您的<# commits> ,将生成特定文件的提交列表。

    TIP: if you aren't sure what commit you are looking for, a good way to find out is using the following command: git diff <commit-SHA1>..HEAD <file-name> .提示:如果您不确定要查找的提交是什么,使用以下命令是一个很好的查找方式: git diff <commit-SHA1>..HEAD <file-name> This command will show the difference between the current version of a commit, and a previous version of a commit for a specific file.此命令将显示当前版本的提交与特定文件的先前版本的提交之间的差异。

    NOTE: a commit's SHA1 key is formatted in the git log -n 's list as:注意:提交的 SHA1 密钥在git log -n的列表中被格式化为:

commit <SHA1 id>提交<SHA1 id>

  • Second, checkout the desired version:其次,签出所需的版本:

    If you have found the desired commit/version you want, simply use the command: git checkout <desired-SHA1> <file-name>如果您找到了所需的提交/版本,只需使用以下命令: git checkout <desired-SHA1> <file-name>

    This will place the version of the file you specified in the staging area.这会将您指定的文件版本放置在暂存区中。 To take it out of the staging area simply use the command: reset HEAD <file-name>要将其移出暂存区,只需使用以下命令: reset HEAD <file-name>

To revert back to where the remote repository is pointed to, simply use the command: git checkout HEAD <file-name>要恢复到远程存储库指向的位置,只需使用以下命令: git checkout HEAD <file-name>

To get to a specific committed code, you need the hash code of that commit.要获取特定的提交代码,您需要该提交的 hash 代码 You can get that hash code in two ways:您可以通过两种方式获取 hash 代码:

  1. Get it from your github/gitlab/bitbucket account.从你的 github/gitlab/bitbucket 帐户获取它。 (It's on your commit url, ie: github.com/user/my_project/commit/ commit_hash_code ), or you can (它在你的提交 url 上,即: github.com/user/my_project/commit/commit_hash_code ),或者你可以
  2. git log and check your recent commits on that branch. git log并检查您最近在该分支上的提交。 It will show you the hash code of your commit and the message you leaved while you were committing your code.它将显示您提交的 hash 代码以及您在提交代码时留下的消息。 Just copy and then do git checkout commit_hash_code只需复制然后执行git checkout commit_hash_code

After moving to that code, if you want to work on it and make changes, you should make another branch with git checkout -b <new-branch-name> , otherwise, the changes will not be retained.移动到该代码后,如果您想对其进行处理并进行更改,您应该使用git checkout -b <new-branch-name>创建另一个分支,否则,更改将不会保留。

I was in a situation where we have a master branch, and then another branch called 17.0 and inside this 17.0 there was a commit hash no say "XYZ" .我当时的情况是我们有一个主分支,然后是另一个名为 17.0 的分支,在这个 17.0 中有一个提交 hash no say "XYZ" And customer is given a build till that XYZ revision.并为客户提供直到该 XYZ 修订版的构建。 Now we came across a bug and that needs to be solved for that customer.现在我们遇到了一个错误,需要为该客户解决。 So we need to create separate branch for that customer till that "xyz" hash. So here is how I did it.所以我们需要为那个客户创建单独的分支,直到那个“xyz”hash。所以我是这样做的。

First I created a folder with that customer name on my local machine.首先,我在本地计算机上创建了一个具有该客户名称的文件夹。 Say customer name is "AAA" once that folder is created issue following command inside this folder:创建该文件夹后,假设客户名称为“AAA”,在此文件夹内发出以下命令:

  1. git init git 初始化
  2. git clone After this command you will be on master branch. git clone执行此命令后,您将位于 master 分支上。 So switch to desired branch所以切换到所需的分支
  3. git checkout 17.0 This will bring you to the branch where your commit is present git checkout 17.0这将带您到您的提交所在的分支
  4. git checkout This will take your repository till that hash commit. git checkout这将使您的存储库直到 hash 提交。 See the name of ur branch it got changed to that commit hash no.查看您的分支名称,它已更改为该提交 hash 号。 Now give a branch name to this hash现在给这个 hash 一个分支名称
  5. git branch ABC This will create a new branch on your local machine. git branch ABC这将在您的本地计算机上创建一个新分支。
  6. git checkout ABC git结帐ABC
  7. git push origin ABC This will push this branch to remote repository and create a branch on git server. git push origin ABC这会将此分支推送到远程存储库并在 git 服务器上创建一个分支。 You are done.你完成了。

One way would be to create all commits ever made to patches.一种方法是创建对补丁所做的所有提交。 checkout the initial commit and then apply the patches in order after reading.检查初始提交,然后在阅读后按顺序应用补丁。

use git format-patch <initial revision> and then git checkout <initial revision> .使用git format-patch <initial revision>然后git checkout <initial revision> you should get a pile of files in your director starting with four digits which are the patches.你应该在你的导演中得到一堆文件,这些文件以四位数字开头,它们是补丁。

when you are done reading your revision just do git apply <filename> which should look like git apply 0001-* and count.当你读完你的修订后,只需执行git apply <filename> ,它应该看起来像git apply 0001-*并计数。

But I really wonder why you wouldn't just want to read the patches itself instead?但我真的很想知道为什么你不想直接阅读补丁本身? Please post this in your comments because I'm curious.请将此张贴在您的评论中,因为我很好奇。

the git manual also gives me this: git 手册也给了我这个:

 git show next~10:Documentation/README

Shows the contents of the file Documentation/README as they were current in the 10th last commit of the branch next.显示文件 Documentation/README 的内容,因为它们在下一个分支的最后 10 次提交中是当前的。

you could also have a look at git blame filename which gives you a listing where each line is associated with a commit hash + author.您还可以查看git blame filename ,它为您提供了一个列表,其中每一行都与提交 hash + 作者相关联。

I cloned a git repository of a certain project.我克隆了某个项目的 git 存储库。 Can I turn the files to the initial state and when I review the files go to revision 2, 3, 4 ... most recent?我可以将文件转为初始状态,并在我查看文件时转到修订版 2、3、4...最新版本吗? I'd like to have an overview of how the project was evolving.我想大致了解该项目的发展情况。

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

相关问题 我可以使用修订号转到git中分支的旧版本吗? - Can I go to an older revision of the branch in git using revision number? 如何查找具有特定代码结构的修订 - How to find a revision with particular code structure Git:获取深度为1的git存储库的特定修订版 - Git: get a particular revision of a git repository with depth 1 Go:如何将 git 修订版添加到构建的二进制文件中? - Go: how to add git revision to binaries built? 安装私有 Go 模块:未知修订错误 - Installing private Go module: unknown revision error 在Mercurial中查看对特定文件进行更改的所有修订号 - View all revision numbers that made changes to a particular file in Mercurial 通过CMake签出Git仓库的特定版本 - Checking out a particular revision of a Git repo via CMake 返回存储库中的特定修订后提交并推送更改? - Commit and push changes after going back to a particular revision in the repository? 阅读 github.com/username/kit/go/database/go/database/go.mod 在修订版 go/database/v1.0.1:未知修订版 go/database/v1.0.1 - reading github.com/username/kit/go/database/go/database/go.mod at revision go/database/v1.0.1: unknown revision go/database/v1.0.1 如何在不丢失工作的情况下返回早期版本? - How to go back to an earlier revision without losing work done?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM