📌  相关文章
📜  签出 git 特定提交 - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:41:08.692000             🧑  作者: Mango

签出 git 特定提交 - Shell-Bash

在进行开发时,我们通常需要回滚到之前的某一次提交或者查看特定提交的代码,这时我们需要使用 Git 命令来签出特定提交。在本篇文章中,我们将介绍如何使用 Shell-Bash 命令来签出 git 特定提交。

1. 查看 git 提交记录

首先,我们需要查看 git 提交记录,并获取需要签出的提交的 commit id。使用以下命令查看 git 提交记录:

git log

该命令会列出所有的 git 提交记录,从最新的开始列出。其中每一条记录都包括了 commit id、作者名称、提交时间和提交信息。

示例输出:

commit a59a15ea288e91bfc74620b3519e47c03974faf7
Author: John Doe <johndoe@example.com>
Date:   Mon Nov 2 10:00:00 2020 +0800

    Add new feature

commit 94a6b393fb6cfc15957b9a6f334c891df0e419e8
Author: John Doe <johndoe@example.com>
Date:   Sat Oct 31 10:00:00 2020 +0800

    Fix issue #1234

commit c7e80c59d4b1139ac5fabb03d1b5088c25288b4a
Author: John Doe <johndoe@example.com>
Date:   Mon Oct 26 10:00:00 2020 +0800

    Initial commit

在这个例子中,我们可以看到三个提交记录,并获取到需要签出的提交的 commit id(例如 a59a15ea288e91bfc74620b3519e47c03974faf7)。

2. 签出特定提交

我们可以使用以下命令来签出特定提交:

git checkout <commit-id>

其中,<commit-id> 是需要签出的提交的 commit id。

示例命令:

git checkout a59a15ea288e91bfc74620b3519e47c03974faf7

运行该命令后,git 会将代码库回滚到该提交时的状态,并显示以下内容:

Note: switching to 'a59a15ea288e91bfc74620b3519e47c03974faf7'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at a59a15e Add new feature

接下来,我们就可以查看需要签出的特定提交的代码并进行相应的操作。

3. 恢复到最新版本

一旦我们完成了在特定提交中的操作后,我们通常需要将代码回滚到最新版本。使用以下命令将代码恢复到最新版本:

git checkout -

运行该命令后,git 会将代码库回滚到最新版本,并显示以下内容:

Switched to branch 'main'
Your branch is up to date with 'origin/main'.
总结

本文介绍了如何使用 Shell-Bash 命令来签出 git 特定提交。通过查看提交记录,获取 commit id 并使用 git checkout 命令,我们可以签出特定提交以便进行代码操作。完成操作后,我们可以使用 git checkout - 命令将代码回滚到最新版本。这些命令是每个开发者必须掌握的基本技能之一。