git 转到特定版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7539130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:54:35  来源:igfitidea点击:

Go to particular revision

gitrevisionrevision-history

提问by xralf

I cloned a git repository of a certain project. Can I turn the files to the initial state and when I review the files go to revision 2, 3, 4 ... most recent? I'd like to have an overview of how the project was evolving.

我克隆了某个项目的 git 存储库。我可以将文件转为初始状态,并在我查看文件时转到修订版 2、3、4...最新版本吗?我想大致了解该项目的发展情况。

回答by Marcelo Cantos

Use git checkout <sha1>to check out a particular commit.

使用git checkout <sha1>检查出一个特定的提交。

回答by J4cK

To go to a particular version/commit run following commands. HASH-CODE you can get from git log --oneline -n 10

要转到特定版本/提交,请运行以下命令。您可以从中获得哈希码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,如果您想恢复所有被丢弃的提交。

回答by Mat

You can get a graphical view of the project history with tools like gitk. Just run:

您可以使用诸如gitk. 赶紧跑:

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. (See Treeishesin the Git Community Book, which is a good read, to see other options for navigating your tree.)

对于特定提交,请使用 SHA1 哈希而不是分支名称。(请参阅Git Community Book中的Treeishes,这是一本很好的读物,以查看导航树的其他选项。)

git loghas a whole set of options to display detailed or summary history too.

git log有一整套选项来显示详细或摘要历史记录。

I don't know of an easy way to move forwardin 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 中并不能很好地映射。

回答by Wizard

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.

    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>. 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:

  • 首先,找到您想要的特定文件的提交:

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

    <# commits>将根据您的生成特定文件的提交列表。

    提示:如果您不确定要查找的提交,一个很好的查找方法是使用以下命令:git diff <commit-SHA1>..HEAD <file-name>. 此命令将显示当前提交版本与特定文件提交的先前版本之间的差异。

    注意:提交的 SHA1 密钥在git log -n's 列表中的格式为:

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>

    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>

  • 其次,签出所需的版本:

    如果您找到了所需的提交/版本,只需使用以下命令: git checkout <desired-SHA1> <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>

回答by ashish

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". And customer is given a build till that XYZ revision. 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.

我处于这样一种情况,我们有一个 master 分支,然后是另一个名为 17.0 的分支,在这个 17.0 中有一个提交哈希,没有说"XYZ"。并且在 XYZ 修订之前为客户提供了一个构建。现在我们遇到了一个错误,需要为该客户解决。所以我们需要为那个客户创建单独的分支,直到那个“xyz”哈希。所以这就是我是如何做到的。

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

回答by Ianiv Hojman

To get to a specific committed code, you need the hash codeof that commit. You can get that hash code in two ways:

要获得特定提交的代码,您需要该提交的哈希码。您可以通过两种方式获取该哈希码:

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

回答by Alex

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>. you should get a pile of files in your director starting with four digits which are the patches.

使用git format-patch <initial revision>然后git checkout <initial revision>。你应该在你的导演中得到一堆文件,以四位数字开头,这是补丁。

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.

git show next~10:Documentation/README

显示文件 Documentation/README 的内容,因为它们在下一个分支的第 10 次最后提交中是当前的。

you could also have a look at git blame filenamewhich gives you a listing where each line is associated with a commit hash + author.

你也可以看看git blame filename它为你提供了一个列表,其中每一行都与提交哈希+作者相关联。

回答by Jeremy

To check a commit out (nb you are looking at the past!).

检查提交(注意您正在查看过去!)。

  • git checkout "commmitHash"
  • git 结帐“commmitHash”

To brutally restart from a commit and delete those later branches that you probably messed up.

从提交中粗暴地重新启动并删除那些您可能搞砸的后续分支。

  • git reset --hard "commmitHash"
  • git reset --hard "commmitHash"